$npx -y skills add PramodDutta/qaskills --skill behave-bdd-testingPython BDD testing with Behave framework using Gherkin feature files, step definitions, environment hooks, and Selenium integration for behavior-driven acceptance testing.
| 1 | # Behave BDD Testing |
| 2 | |
| 3 | You are an expert QA engineer specializing in Behave, the Python BDD testing framework. When the user asks you to write, review, debug, or set up Behave tests, follow these detailed instructions. You understand the Behave ecosystem deeply including Gherkin feature files, step definitions, environment hooks, context management, fixtures, tag-based filtering, and integration with Selenium, Requests, and other Python libraries. |
| 4 | |
| 5 | ## Core Principles |
| 6 | |
| 7 | 1. **Business-Readable Scenarios** — Write Gherkin scenarios in plain language that stakeholders can understand. Avoid technical implementation details in feature files. |
| 8 | 2. **Reusable Step Definitions** — Design steps to be generic and composable. Use parameterized steps with regex patterns to maximize reuse across features. |
| 9 | 3. **Context as Communication** — Use `context` object to pass data between steps cleanly. Store page objects, API clients, and test data on context in hooks. |
| 10 | 4. **Environment Hooks for Lifecycle** — Manage browser setup, database seeding, and cleanup in `environment.py` hooks rather than in step definitions. |
| 11 | 5. **Tag-Based Organization** — Use tags (`@smoke`, `@regression`, `@wip`) to organize and filter test execution. Tags drive fixture selection and reporting. |
| 12 | 6. **Page Object Pattern** — Separate page interactions from step logic. Step definitions call page object methods; page objects encapsulate selectors and browser interactions. |
| 13 | 7. **Fail Fast with Clarity** — Assertions should produce clear, descriptive failure messages. Capture screenshots and logs on failure for debugging. |
| 14 | |
| 15 | ## Project Structure |
| 16 | |
| 17 | ``` |
| 18 | project-root/ |
| 19 | ├── features/ |
| 20 | │ ├── auth/ |
| 21 | │ │ ├── login.feature |
| 22 | │ │ ├── signup.feature |
| 23 | │ │ └── password_reset.feature |
| 24 | │ ├── shopping/ |
| 25 | │ │ ├── cart.feature |
| 26 | │ │ └── checkout.feature |
| 27 | │ ├── steps/ |
| 28 | │ │ ├── auth_steps.py |
| 29 | │ │ ├── shopping_steps.py |
| 30 | │ │ ├── navigation_steps.py |
| 31 | │ │ └── common_steps.py |
| 32 | │ ├── pages/ |
| 33 | │ │ ├── base_page.py |
| 34 | │ │ ├── login_page.py |
| 35 | │ │ ├── dashboard_page.py |
| 36 | │ │ └── cart_page.py |
| 37 | │ ├── fixtures/ |
| 38 | │ │ ├── browser.py |
| 39 | │ │ ├── database.py |
| 40 | │ │ └── api_client.py |
| 41 | │ └── environment.py |
| 42 | ├── reports/ |
| 43 | │ ├── screenshots/ |
| 44 | │ └── allure-results/ |
| 45 | ├── config/ |
| 46 | │ ├── dev.ini |
| 47 | │ ├── staging.ini |
| 48 | │ └── prod.ini |
| 49 | ├── behave.ini |
| 50 | ├── requirements.txt |
| 51 | └── pytest.ini |
| 52 | ``` |
| 53 | |
| 54 | ## Detailed Code Examples |
| 55 | |
| 56 | ### Feature File (Gherkin) |
| 57 | |
| 58 | ```gherkin |
| 59 | # features/auth/login.feature |
| 60 | @auth |
| 61 | Feature: User Authentication |
| 62 | As a registered user |
| 63 | I want to login to the application |
| 64 | So that I can access my dashboard |
| 65 | |
| 66 | Background: |
| 67 | Given the application is running |
| 68 | And I am on the login page |
| 69 | |
| 70 | @smoke @positive |
| 71 | Scenario: Successful login with valid credentials |
| 72 | When I enter "user@example.com" as email |
| 73 | And I enter "SecurePass123" as password |
| 74 | And I click the login button |
| 75 | Then I should be redirected to the dashboard |
| 76 | And I should see a welcome message containing "Welcome" |
| 77 | |
| 78 | @negative |
| 79 | Scenario: Login fails with invalid password |
| 80 | When I enter "user@example.com" as email |
| 81 | And I enter "wrongpassword" as password |
| 82 | And I click the login button |
| 83 | Then I should see an error message "Invalid credentials" |
| 84 | And I should remain on the login page |
| 85 | |
| 86 | @negative |
| 87 | Scenario Outline: Login fails with invalid inputs |
| 88 | When I enter "<email>" as email |
| 89 | And I enter "<password>" as password |
| 90 | And I click the login button |
| 91 | Then I should see an error message "<error>" |
| 92 | |
| 93 | Examples: |
| 94 | | email | password | error | |
| 95 | | | SecurePass123 | Email is required | |
| 96 | | user@example.com | | Password is required | |
| 97 | | invalid-email | SecurePass123 | Invalid email format | |
| 98 | | nonexist@test.com | SecurePass123 | Account not found | |
| 99 | |
| 100 | @data-driven |
| 101 | Scenario: Login with multiple user roles |
| 102 | Given the following users exist: |
| 103 | | name | email | role | |
| 104 | | Admin | admin@example.com | admin | |
| 105 | | Editor | editor@example.com | editor | |
| 106 | | Viewer | viewer@example.com | viewer | |
| 107 | When I login as "admin@example.com" with password "AdminPass123" |
| 108 | Then I should see the admin panel |
| 109 | ``` |
| 110 | |
| 111 | ### Step Definitions |
| 112 | |
| 113 | ```python |
| 114 | # features/steps/auth_steps.py |
| 115 | from behave import given, when, then, step |
| 116 | from selenium.webdriver.suppor |