$npx -y skills add LambdaTest/agent-skills --skill behat-skillGenerates Behat BDD tests for PHP with Gherkin feature files and MinkContext for browser testing. Use when user mentions "Behat", "PHP BDD", "Mink", "behat.yml". Triggers on: "Behat", "PHP BDD", "Mink", "behat.yml", "FeatureContext PHP".
| 1 | # Behat BDD Skill |
| 2 | |
| 3 | ## Core Patterns |
| 4 | |
| 5 | ### Feature File (features/login.feature) |
| 6 | |
| 7 | ```gherkin |
| 8 | Feature: User Login |
| 9 | As a user I want to log in |
| 10 | |
| 11 | Scenario: Successful login |
| 12 | Given I am on "/login" |
| 13 | When I fill in "email" with "user@test.com" |
| 14 | And I fill in "password" with "password123" |
| 15 | And I press "Login" |
| 16 | Then I should see "Dashboard" |
| 17 | And I should be on "/dashboard" |
| 18 | |
| 19 | Scenario: Invalid credentials |
| 20 | Given I am on "/login" |
| 21 | When I fill in "email" with "wrong@test.com" |
| 22 | And I fill in "password" with "wrong" |
| 23 | And I press "Login" |
| 24 | Then I should see "Invalid credentials" |
| 25 | ``` |
| 26 | |
| 27 | ### Custom Context (features/bootstrap/LoginContext.php) |
| 28 | |
| 29 | ```php |
| 30 | <?php |
| 31 | use Behat\MinkExtension\Context\MinkContext; |
| 32 | use Behat\Behat\Context\Context; |
| 33 | |
| 34 | class LoginContext extends MinkContext implements Context |
| 35 | { |
| 36 | /** |
| 37 | * @When I login as :email with password :password |
| 38 | */ |
| 39 | public function iLoginAs(string $email, string $password): void |
| 40 | { |
| 41 | $this->visit('/login'); |
| 42 | $this->fillField('email', $email); |
| 43 | $this->fillField('password', $password); |
| 44 | $this->pressButton('Login'); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @Then I should see the dashboard |
| 49 | */ |
| 50 | public function iShouldSeeTheDashboard(): void |
| 51 | { |
| 52 | $this->assertSession()->addressEquals('/dashboard'); |
| 53 | $this->assertSession()->pageTextContains('Welcome'); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @Then the response time should be under :ms milliseconds |
| 58 | */ |
| 59 | public function responseUnder(int $ms): void |
| 60 | { |
| 61 | // Custom performance assertion |
| 62 | } |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | ### Built-in MinkContext Steps |
| 67 | |
| 68 | ```gherkin |
| 69 | # Navigation |
| 70 | Given I am on "/path" |
| 71 | When I go to "/path" |
| 72 | When I reload the page |
| 73 | |
| 74 | # Forms |
| 75 | When I fill in "field" with "value" |
| 76 | When I select "option" from "select" |
| 77 | When I check "checkbox" |
| 78 | When I uncheck "checkbox" |
| 79 | When I press "button" |
| 80 | When I attach the file "path" to "field" |
| 81 | |
| 82 | # Assertions |
| 83 | Then I should see "text" |
| 84 | Then I should not see "text" |
| 85 | Then I should be on "/path" |
| 86 | Then the response status code should be 200 |
| 87 | Then the "field" field should contain "value" |
| 88 | Then I should see an "css-selector" element |
| 89 | Then print current URL |
| 90 | ``` |
| 91 | |
| 92 | ### behat.yml |
| 93 | |
| 94 | ```yaml |
| 95 | default: |
| 96 | suites: |
| 97 | default: |
| 98 | contexts: |
| 99 | - LoginContext |
| 100 | - Behat\MinkExtension\Context\MinkContext |
| 101 | extensions: |
| 102 | Behat\MinkExtension: |
| 103 | base_url: 'http://localhost:3000' |
| 104 | sessions: |
| 105 | default: |
| 106 | selenium2: |
| 107 | browser: chrome |
| 108 | wd_host: 'http://localhost:4444/wd/hub' |
| 109 | ``` |
| 110 | |
| 111 | ### Tags |
| 112 | |
| 113 | ```bash |
| 114 | ./vendor/bin/behat --tags=@smoke |
| 115 | ./vendor/bin/behat --tags="@smoke&&~@slow" |
| 116 | ``` |
| 117 | |
| 118 | ## Setup: `composer require --dev behat/behat behat/mink-extension behat/mink-selenium2-driver` |
| 119 | ## Init: `./vendor/bin/behat --init` |
| 120 | |
| 121 | ### Cloud Execution on TestMu AI |
| 122 | |
| 123 | Set environment variables: `LT_USERNAME`, `LT_ACCESS_KEY` |
| 124 | |
| 125 | ```yaml |
| 126 | # behat.yml |
| 127 | default: |
| 128 | extensions: |
| 129 | Behat\MinkExtension: |
| 130 | base_url: 'https://your-app.com' |
| 131 | selenium2: |
| 132 | wd_host: 'https://hub.lambdatest.com/wd/hub' |
| 133 | capabilities: |
| 134 | browser: 'chrome' |
| 135 | extra_capabilities: |
| 136 | 'LT:Options': |
| 137 | user: '%env(LT_USERNAME)%' |
| 138 | accessKey: '%env(LT_ACCESS_KEY)%' |
| 139 | build: 'Behat Build' |
| 140 | name: 'Behat Test' |
| 141 | platformName: 'Windows 11' |
| 142 | video: true |
| 143 | console: true |
| 144 | network: true |
| 145 | ``` |
| 146 | ## Run: `./vendor/bin/behat` or `./vendor/bin/behat features/login.feature` |
| 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 | behat.yml with suites, Mink extension, profiles, project structure | |
| 155 | | §2 Feature Files | Gherkin with Scenario Outline, Background, TableNode data | |
| 156 | | §3 Context Classes | Step definitions, dependency injection, API context, assertions | |
| 157 | | §4 Hooks | BeforeSuite/Scenario/Step, screenshot on failure, transaction rollback | |
| 158 | | §5 Page Objects | Page Object pattern with elements map, reusable components | |
| 159 | | §6 LambdaTest Integration | Remote Selenium config, cloud browser profiles | |
| 160 | | §7 Custom Formatters | HTML report formatter, result collection | |
| 161 | | §8 CI/CD Integration | GitHub Actions with MySQL, Selenium, JUnit reports | |
| 162 | | §9 Debugging Table | 12 common problems with causes and fixes | |
| 163 | | §10 Best Practices | 14-item BDD testing checklist | |