Quick Answer: BDD with Cucumber uses Gherkin syntax (Given/When/Then) to write readable test scenarios. Steps: (1) write a .feature file in Gherkin, (2) create step definitions in Java/JavaScript, (3) run with JUnit or TestNG, (4) view the HTML report.
What is BDD?
Behavior-Driven Development writes test scenarios in natural language (Gherkin) that non-technical stakeholders can understand. Cucumber is the most popular BDD framework.
Write a Feature File
Feature: User Login
As a registered user
I want to log in with my credentials
Scenario: Successful login
Given I am on the login page
When I enter valid email "[email protected]"
And I enter valid password "SecurePass123"
And I click the login button
Then I should be redirected to the dashboard
Create Step Definitions
import io.cucumber.java.en.*;
public class LoginSteps {
@Given("I am on the login page")
public void iAmOnTheLoginPage() {
driver.get("https://example.com/login");
}
@When("I enter valid email {string}")
public void iEnterValidEmail(String email) {
driver.findElement(By.id("email")).sendKeys(email);
}
@Then("I should be redirected to the dashboard")
public void iShouldBeRedirected() {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.urlContains("/dashboard"));
}
}
Gherkin Syntax Reference
Keyword
Purpose
Feature
Describes the feature
Scenario
A single test case
Given
Precondition / initial state
When
Action performed
Then
Expected outcome
And
Additional steps
Benefits of BDD
Collaboration: PMs, developers, and testers all understand Gherkin
Living documentation: Feature files serve as up-to-date docs
Reusable steps: Step definitions shared across features