This project serves as a skeleton for a BDD (Behavior-Driven Development) test automation framework using Playwright and behave. The purpose is to provide a sample project structure for writing end-to-end tests in Python with a focus on clear, human-readable feature files and reusable step definitions.
The project is composed of the following key files and directories:
requirements.txt: This file lists all the Python dependencies required for the project, includingbehave,playwright, andbehave-playwright.features/: This directory is where Gherkin.featurefiles are stored, as well as theenvironment.pyfile.login.feature: A sample feature file that defines a scenario for a "Successful login with valid credentials".environment.py: This file contains hooks that run at specific points in the test lifecycle, such as before and after the entire test session or before and after each scenario. This is where the Playwright browser is initialized and closed.
features/steps/: This directory contains the Python step definition files that implement the logic for the steps defined in the feature files.login_steps.py: Contains the@given,@when, and@thenfunctions that use the PlaywrightPageobject to interact with a web page and verify outcomes.
- Test Discovery:
behaveis configured to find and run tests defined by the Gherkin.featurefiles within thefeatures/directory. - Scenario to Steps: For each scenario found,
behavematches the steps (e.g., "Given I am on the login page") to the step definition functions (e.g.,@given('I am on the login page')) in thefeatures/steps/directory. - Test Execution: The step definition functions use Playwright's API to perform actions like navigating to a URL (
page.goto(...)), filling in form fields (page.get_by_label(...)), and clicking buttons (page.click()). The PlaywrightPageobject is shared across steps viabehave'scontextobject. - Setup and Teardown: The hooks in
environment.pyrun at specific points to manage the browser session and perform any necessary setup or cleanup tasks.
-
Install Dependencies: First, ensure you have Python and
pipinstalled. Navigate to the project root directory and install the required packages using therequirements.txtfile.pip install -r requirements.txt
-
Run the Tests: From the project root directory, execute the tests using
behave.behave features/
behavewill automatically discover thelogin.featurefile in thefeatures/directory and run the scenario using the step definitions fromlogin_steps.py. Theenvironment.pyfile will also run the setup and teardown hooks during the test session and for each scenario.