$npx -y skills add LambdaTest/agent-skills --skill cucumber-skillGenerates Cucumber BDD tests with Gherkin feature files and step definitions in Java, JavaScript, or Ruby. Use when user mentions "Cucumber", "Gherkin", "Feature/Scenario", "Given/When/Then", "BDD". Triggers on: "Cucumber", "Gherkin", "BDD", "Feature file", "Given/When/Then", "st
| 1 | # Cucumber BDD Skill |
| 2 | |
| 3 | ## Core Patterns |
| 4 | |
| 5 | ### Feature File (Gherkin) |
| 6 | |
| 7 | ```gherkin |
| 8 | Feature: User Login |
| 9 | As a registered user |
| 10 | I want to log into the application |
| 11 | So that I can access my dashboard |
| 12 | |
| 13 | Background: |
| 14 | Given I am on the login page |
| 15 | |
| 16 | Scenario: Successful login |
| 17 | When I enter "user@test.com" in the email field |
| 18 | And I enter "password123" in the password field |
| 19 | And I click the login button |
| 20 | Then I should be redirected to the dashboard |
| 21 | And I should see "Welcome" on the page |
| 22 | |
| 23 | Scenario: Invalid credentials |
| 24 | When I enter "wrong@test.com" in the email field |
| 25 | And I enter "wrongpass" in the password field |
| 26 | And I click the login button |
| 27 | Then I should see an error message "Invalid credentials" |
| 28 | |
| 29 | Scenario Outline: Login with various users |
| 30 | When I enter "<email>" in the email field |
| 31 | And I enter "<password>" in the password field |
| 32 | And I click the login button |
| 33 | Then I should see "<result>" |
| 34 | |
| 35 | Examples: |
| 36 | | email | password | result | |
| 37 | | admin@test.com | admin123 | Dashboard | |
| 38 | | user@test.com | password | Dashboard | |
| 39 | | bad@test.com | wrong | Error | |
| 40 | ``` |
| 41 | |
| 42 | ### Step Definitions — Java |
| 43 | |
| 44 | ```java |
| 45 | import io.cucumber.java.en.*; |
| 46 | import static org.junit.jupiter.api.Assertions.*; |
| 47 | |
| 48 | public class LoginSteps { |
| 49 | private LoginPage loginPage; |
| 50 | private DashboardPage dashboardPage; |
| 51 | |
| 52 | @Given("I am on the login page") |
| 53 | public void iAmOnTheLoginPage() { |
| 54 | loginPage = new LoginPage(driver); |
| 55 | loginPage.navigate(); |
| 56 | } |
| 57 | |
| 58 | @When("I enter {string} in the email field") |
| 59 | public void iEnterEmail(String email) { |
| 60 | loginPage.enterEmail(email); |
| 61 | } |
| 62 | |
| 63 | @When("I enter {string} in the password field") |
| 64 | public void iEnterPassword(String password) { |
| 65 | loginPage.enterPassword(password); |
| 66 | } |
| 67 | |
| 68 | @When("I click the login button") |
| 69 | public void iClickLogin() { |
| 70 | dashboardPage = loginPage.clickLogin(); |
| 71 | } |
| 72 | |
| 73 | @Then("I should be redirected to the dashboard") |
| 74 | public void iShouldBeOnDashboard() { |
| 75 | assertTrue(driver.getCurrentUrl().contains("/dashboard")); |
| 76 | } |
| 77 | |
| 78 | @Then("I should see {string} on the page") |
| 79 | public void iShouldSeeText(String text) { |
| 80 | assertTrue(dashboardPage.getPageSource().contains(text)); |
| 81 | } |
| 82 | } |
| 83 | ``` |
| 84 | |
| 85 | ### Step Definitions — JavaScript |
| 86 | |
| 87 | ```javascript |
| 88 | const { Given, When, Then } = require('@cucumber/cucumber'); |
| 89 | const { expect } = require('chai'); |
| 90 | |
| 91 | Given('I am on the login page', async function() { |
| 92 | await this.page.goto('/login'); |
| 93 | }); |
| 94 | |
| 95 | When('I enter {string} in the email field', async function(email) { |
| 96 | await this.page.fill('#email', email); |
| 97 | }); |
| 98 | |
| 99 | When('I click the login button', async function() { |
| 100 | await this.page.click('button[type="submit"]'); |
| 101 | }); |
| 102 | |
| 103 | Then('I should see {string} on the page', async function(text) { |
| 104 | const content = await this.page.textContent('body'); |
| 105 | expect(content).to.include(text); |
| 106 | }); |
| 107 | ``` |
| 108 | |
| 109 | ### Hooks |
| 110 | |
| 111 | ```java |
| 112 | import io.cucumber.java.*; |
| 113 | |
| 114 | public class Hooks { |
| 115 | @Before |
| 116 | public void setUp(Scenario scenario) { |
| 117 | driver = new ChromeDriver(); |
| 118 | } |
| 119 | |
| 120 | @After |
| 121 | public void tearDown(Scenario scenario) { |
| 122 | if (scenario.isFailed()) { |
| 123 | byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); |
| 124 | scenario.attach(screenshot, "image/png", "failure-screenshot"); |
| 125 | } |
| 126 | driver.quit(); |
| 127 | } |
| 128 | } |
| 129 | ``` |
| 130 | |
| 131 | ### Tags |
| 132 | |
| 133 | ```gherkin |
| 134 | @smoke |
| 135 | Feature: Login |
| 136 | @critical @fast |
| 137 | Scenario: Quick login |
| 138 | ... |
| 139 | |
| 140 | @slow @regression |
| 141 | Scenario: Full login flow |
| 142 | ... |
| 143 | ``` |
| 144 | |
| 145 | ```bash |
| 146 | # Run by tag |
| 147 | mvn test -Dcucumber.filter.tags="@smoke" |
| 148 | mvn test -Dcucumber.filter.tags="@smoke and not @slow" |
| 149 | ``` |
| 150 | |
| 151 | ### Anti-Patterns |
| 152 | |
| 153 | | Bad | Good | Why | |
| 154 | |-----|------|-----| |
| 155 | | UI details in Gherkin | Business language | Readability | |
| 156 | | One step per line of code | Meaningful business steps | Abstraction | |
| 157 | | No Background for shared steps | Use Background | DRY | |
| 158 | | Imperative steps | Declarative steps | Maintainable | |
| 159 | |
| 160 | |
| 161 | ### Cloud Execution on TestMu AI |
| 162 | |
| 163 | Set environment variables: `LT_USERNAME`, `LT_ACCESS_KEY` |
| 164 | |
| 165 | **Java:** |
| 166 | ```java |
| 167 | // CucumberHooks.java |
| 168 | ChromeOptions browserOptions = new ChromeOptions(); |
| 169 | HashMap<String, Object> ltOptions = new HashMap<>(); |
| 170 | ltOptions.put("user", System.getenv("LT_USERNAME")); |
| 171 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); |
| 172 | ltOptions.put("build", "Cucumber Build"); |
| 173 | ltOptions.put("name", scenario.getName()); |
| 174 | ltOptions.put("platformName", "Windows 11"); |
| 175 | ltOptions.put("video", true); |
| 176 | browserOption |