# Cucumber BDD — Advanced Implementation Playbook
## §1 — Project Setup & Configuration
```xml
io.cucumber
cucumber-java
7.15.0
test
io.cucumber
cucumber-testng
7.15.0
test
io.cucumber
cucumber-picocontainer
7.15.0
test
org.seleniumhq.selenium
selenium-java
4.18.1
org.apache.maven.plugins
maven-surefire-plugin
3.2.5
dataproviderthreadcount
4
```
### Runner Configuration
```java
@CucumberOptions(
features = "src/test/resources/features",
glue = {"stepdefs", "hooks"},
plugin = {
"pretty",
"html:target/cucumber-reports/cucumber.html",
"json:target/cucumber-reports/cucumber.json",
"io.qameta.allure.cucumber7jvm.AllureCucumber7Jvm",
"rerun:target/rerun.txt"
},
tags = "@smoke and not @wip",
monochrome = true,
dryRun = false
)
public class TestRunner extends AbstractTestNGCucumberTests {
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
// Rerun failed tests runner
@CucumberOptions(
features = "@target/rerun.txt",
glue = {"stepdefs", "hooks"},
plugin = {"pretty", "html:target/rerun-report.html"}
)
public class RerunRunner extends AbstractTestNGCucumberTests {}
```
## §2 — Feature Writing Patterns
```gherkin
@smoke @auth
Feature: User Authentication
As a registered user
I want to log into my account
So that I can access my dashboard
Background:
Given the application is running
And the test database is seeded
@positive
Scenario: Successful login with valid credentials
Given I am on the login page
When I enter email "admin@test.com" and password "admin123"
And I click the login button
Then I should be redirected to the dashboard
And I should see welcome message "Hello, Admin"
@negative
Scenario Outline: Login validation
Given I am on the login page
When I enter email "" and password ""
And I click the login button
Then I should see error message ""
Examples:
| email | password | error |
| invalid | pass123 | Invalid email format |
| admin@test.com | | Password is required |
| | pass123 | Email is required |
| wrong@test.com | wrong | Invalid credentials |
@data-driven
Scenario: Create multiple users
Given I am logged in as admin
When I create users with the following details:
| name | email | role |
| Alice | alice@test.com | editor |
| Bob | bob@test.com | viewer |
| Charlie | charlie@test.com | admin |
Then all users should be visible in the user list
@api
Scenario: API-created data appears in UI
Given a product "Widget" exists via API
When I navigate to the products page
Then I should see "Widget" in the product list
```
## §3 — Step Definitions
```java
public class LoginSteps {
private final ScenarioContext ctx;
private LoginPage loginPage;
private DashboardPage dashboardPage;
// PicoContainer DI
public LoginSteps(ScenarioContext ctx) {
this.ctx = ctx;
}
@Given("I am on the login page")
public void navigateToLogin() {
loginPage = new LoginPage(ctx.getDriver());
loginPage.navigate();
}
@When("I enter email {string} and password {string}")
public void enterCredentials(String email, String password) {
loginPage.enterEmail(email);
loginPage.enterPassword(password);
}
@When("I click the login button")
public void clickLogin() {
dashboardPage = loginPage.clickLogin();
}
@Then("I should be redirected to the dashboard")
public void verifyDashboard() {
assertTrue(dashboardPage.isLoaded(), "Dashboard should be loaded");
}
@Then("I should see welcome message {string}")
public void verifyWelcome(String expectedMessage) {
assertEquals(expectedMessage, dashboardPage.getWelcomeMessage());
}
@Then("I should see error message {string}")
public void verifyError(String expectedError) {
assertEquals(expectedError, loginPage.getErrorMessage());
}
}
// DataTable step
public class UserSteps {
@When("I create users with the following details:")
public void createUsers(DataTable dataTable) {
List