$npx -y skills add LambdaTest/agent-skills --skill behave-skillGenerates Behave BDD tests for Python with Gherkin feature files and step implementations. Use when user mentions "Behave", "Python BDD", "Python Gherkin". Triggers on: "Behave", "Python BDD", "behave test", "Python feature file".
| 1 | # Behave BDD Skill |
| 2 | |
| 3 | ## Core Patterns |
| 4 | |
| 5 | ### Feature File (features/login.feature) |
| 6 | |
| 7 | ```gherkin |
| 8 | Feature: User Login |
| 9 | As a registered user |
| 10 | I want to log into the application |
| 11 | |
| 12 | Background: |
| 13 | Given I am on the login page |
| 14 | |
| 15 | Scenario: Successful login |
| 16 | When I enter "user@test.com" as email |
| 17 | And I enter "password123" as password |
| 18 | And I click login |
| 19 | Then I should see the dashboard |
| 20 | And the welcome message should say "Welcome" |
| 21 | |
| 22 | Scenario: Invalid credentials |
| 23 | When I enter "wrong@test.com" as email |
| 24 | And I enter "wrong" as password |
| 25 | And I click login |
| 26 | Then I should see error "Invalid credentials" |
| 27 | |
| 28 | Scenario Outline: Login with various users |
| 29 | When I enter "<email>" as email |
| 30 | And I enter "<password>" as password |
| 31 | And I click login |
| 32 | Then I should see "<result>" |
| 33 | |
| 34 | Examples: |
| 35 | | email | password | result | |
| 36 | | admin@test.com | admin123 | Dashboard | |
| 37 | | bad@test.com | wrong | Error | |
| 38 | ``` |
| 39 | |
| 40 | ### Step Definitions (features/steps/login_steps.py) |
| 41 | |
| 42 | ```python |
| 43 | from behave import given, when, then |
| 44 | from selenium import webdriver |
| 45 | from selenium.webdriver.common.by import By |
| 46 | from selenium.webdriver.support.ui import WebDriverWait |
| 47 | from selenium.webdriver.support import expected_conditions as EC |
| 48 | |
| 49 | @given('I am on the login page') |
| 50 | def step_on_login(context): |
| 51 | context.browser.get(context.base_url + '/login') |
| 52 | |
| 53 | @when('I enter "{text}" as email') |
| 54 | def step_enter_email(context, text): |
| 55 | el = context.browser.find_element(By.ID, 'email') |
| 56 | el.clear() |
| 57 | el.send_keys(text) |
| 58 | |
| 59 | @when('I enter "{text}" as password') |
| 60 | def step_enter_password(context, text): |
| 61 | el = context.browser.find_element(By.ID, 'password') |
| 62 | el.clear() |
| 63 | el.send_keys(text) |
| 64 | |
| 65 | @when('I click login') |
| 66 | def step_click_login(context): |
| 67 | context.browser.find_element(By.CSS_SELECTOR, 'button[type="submit"]').click() |
| 68 | |
| 69 | @then('I should see the dashboard') |
| 70 | def step_see_dashboard(context): |
| 71 | WebDriverWait(context.browser, 10).until( |
| 72 | EC.url_contains('/dashboard') |
| 73 | ) |
| 74 | assert '/dashboard' in context.browser.current_url |
| 75 | |
| 76 | @then('I should see error "{msg}"') |
| 77 | def step_see_error(context, msg): |
| 78 | error = WebDriverWait(context.browser, 5).until( |
| 79 | EC.visibility_of_element_located((By.CSS_SELECTOR, '.error')) |
| 80 | ) |
| 81 | assert msg in error.text |
| 82 | ``` |
| 83 | |
| 84 | ### Environment Hooks (features/environment.py) |
| 85 | |
| 86 | ```python |
| 87 | from selenium import webdriver |
| 88 | |
| 89 | def before_all(context): |
| 90 | context.base_url = 'http://localhost:3000' |
| 91 | |
| 92 | def before_scenario(context, scenario): |
| 93 | context.browser = webdriver.Chrome() |
| 94 | context.browser.implicitly_wait(10) |
| 95 | |
| 96 | def after_scenario(context, scenario): |
| 97 | if scenario.status == 'failed': |
| 98 | context.browser.save_screenshot(f'screenshots/{scenario.name}.png') |
| 99 | context.browser.quit() |
| 100 | ``` |
| 101 | |
| 102 | ### Tags |
| 103 | |
| 104 | ```gherkin |
| 105 | @smoke |
| 106 | Feature: Login |
| 107 | @critical |
| 108 | Scenario: ... |
| 109 | ``` |
| 110 | |
| 111 | ```bash |
| 112 | behave --tags=@smoke |
| 113 | behave --tags="@smoke and not @slow" |
| 114 | ``` |
| 115 | |
| 116 | ## Setup: `pip install behave selenium` |
| 117 | ## Run: `behave` or `behave features/login.feature` |
| 118 | |
| 119 | ### Cloud Execution on TestMu AI |
| 120 | |
| 121 | Set environment variables: `LT_USERNAME`, `LT_ACCESS_KEY` |
| 122 | |
| 123 | ```python |
| 124 | # environment.py |
| 125 | from selenium import webdriver |
| 126 | import os |
| 127 | |
| 128 | def before_scenario(context, scenario): |
| 129 | lt_options = { |
| 130 | "user": os.environ["LT_USERNAME"], |
| 131 | "accessKey": os.environ["LT_ACCESS_KEY"], |
| 132 | "build": "Behave Build", |
| 133 | "name": scenario.name, |
| 134 | "platformName": "Windows 11", |
| 135 | "video": True, |
| 136 | "console": True, |
| 137 | "network": True, |
| 138 | } |
| 139 | options = webdriver.ChromeOptions() |
| 140 | options.set_capability("LT:Options", lt_options) |
| 141 | context.driver = webdriver.Remote( |
| 142 | command_executor=f"https://{os.environ['LT_USERNAME']}:{os.environ['LT_ACCESS_KEY']}@hub.lambdatest.com/wd/hub", |
| 143 | options=options, |
| 144 | ) |
| 145 | ``` |
| 146 | ## Report: `behave --format json -o report.json` |
| 147 | |
| 148 | ## Deep Patterns |
| 149 | |
| 150 | See `reference/playbook.md` for production-grade patterns: |
| 151 | |
| 152 | | Section | What You Get | |
| 153 | |---------|-------------| |
| 154 | | §1 Project Setup | behave.ini, project structure, dependencies | |
| 155 | | §2 Feature Files | Gherkin with Scenario Outline, data tables, Background | |
| 156 | | §3 Step Definitions | Type registration, API steps, common steps with PyHamcrest | |
| 157 | | §4 Environment Hooks | before_all/scenario/feature, screenshot on failure, DB isolation | |
| 158 | | §5 Page Objects | BasePage with waits, LoginPage, reusable components | |
| 159 | | §6 Fixtures & Test Data | DatabaseHelper, transaction rollback, JSON data loader | |
| 160 | | §7 LambdaTest Integration | Remote browser creation, cloud capabilities | |
| 161 | | §8 CI/CD Integration | GitHub Actions with Postgres, Selenium, Allure reports |