Intermediate Chapter 12 · 13 min read

Cucumber + Selenium Integration

Build a complete BDD test suite with Selenium WebDriver — wire up browser automation, handle waits, and test real web applications.

Full Cucumber + Selenium Integration

This chapter ties everything together — feature files describing business behavior, step definitions driving Selenium, page objects encapsulating the UI, and hooks managing the browser lifecycle.

web_form.feature
Feature: Web Form Interaction
  Test the Selenium web form demo page

  Background:
    Given the user navigates to the web form page

  @smoke @ui
  Scenario: Fill and submit the form
    When the user enters "Cucumber Test" in the text field
    And the user enters "secret" in the password field
    And the user selects "Two" from the dropdown
    And the user clicks the submit button
    Then the success message should be displayed

  @ui
  Scenario: Verify form field states
    Then the text input should be enabled
    And the disabled input should not be editable
    And the readonly input should contain "Readonly input"

  @ui
  Scenario Outline: Submit form with various inputs
    When the user enters "<text>" in the text field
    And the user clicks the submit button
    Then the success message should be displayed

    Examples:
      | text                  |
      | Simple text           |
      | Text with numbers 123 |
      | Special chars !@#     |
WebFormSteps.java
package com.bdd.course.stepdefinitions;

import io.cucumber.java.en.*;
import com.bdd.course.context.TestContext;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import org.testng.Assert;
import java.time.Duration;

public class WebFormSteps {

    private final TestContext context;

    public WebFormSteps(TestContext context) {
        this.context = context;
    }

    @Given("the user navigates to the web form page")
    public void navigateToForm() {
        context.getDriver().get(
            "https://www.selenium.dev/selenium/web/web-form.html");
    }

    @When("the user enters {string} in the text field")
    public void enterTextField(String text) {
        WebElement input = context.getDriver()
            .findElement(By.name("my-text"));
        input.clear();
        input.sendKeys(text);
    }

    @When("the user enters {string} in the password field")
    public void enterPasswordField(String password) {
        WebElement input = context.getDriver()
            .findElement(By.name("my-password"));
        input.clear();
        input.sendKeys(password);
    }

    @When("the user selects {string} from the dropdown")
    public void selectDropdown(String option) {
        WebElement select = context.getDriver()
            .findElement(By.name("my-select"));
        new Select(select).selectByVisibleText(option);
    }

    @When("the user clicks the submit button")
    public void clickSubmit() {
        context.getDriver()
            .findElement(By.cssSelector("button[type='submit']"))
            .click();
    }

    @Then("the success message should be displayed")
    public void verifySuccess() {
        WebDriverWait wait = new WebDriverWait(
            context.getDriver(), Duration.ofSeconds(10));
        WebElement msg = wait.until(
            ExpectedConditions.visibilityOfElementLocated(
                By.id("message")));
        Assert.assertTrue(msg.isDisplayed());
    }

    @Then("the text input should be enabled")
    public void textInputEnabled() {
        WebElement input = context.getDriver()
            .findElement(By.name("my-text"));
        Assert.assertTrue(input.isEnabled());
    }

    @Then("the disabled input should not be editable")
    public void disabledInput() {
        WebElement input = context.getDriver()
            .findElement(By.name("my-disabled"));
        Assert.assertFalse(input.isEnabled());
    }

    @Then("the readonly input should contain {string}")
    public void readonlyInput(String expected) {
        WebElement input = context.getDriver()
            .findElement(By.name("my-readonly"));
        Assert.assertEquals(input.getAttribute("value"), expected);
    }
}
web_form.feature
Feature: Web Form Interaction
  Test the Selenium web form demo page

  Background:
    Given the user navigates to the web form page

  @smoke @ui
  Scenario: Fill and submit the form
    When the user enters "Cucumber Test" in the text field
    And the user enters "secret" in the password field
    And the user selects "Two" from the dropdown
    And the user clicks the submit button
    Then the success message should be displayed

  @ui
  Scenario: Verify form field states
    Then the text input should be enabled
    And the disabled input should not be editable
web_form_steps.py
# features/steps/web_form_steps.py
from behave import given, when, then
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

URL = "https://www.selenium.dev/selenium/web/web-form.html"

@given('the user navigates to the web form page')
def step_navigate(context):
    context.driver.get(URL)

@when('the user enters "{text}" in the text field')
def step_enter_text(context, text):
    el = context.driver.find_element(By.NAME, "my-text")
    el.clear()
    el.send_keys(text)

@when('the user enters "{pwd}" in the password field')
def step_enter_password(context, pwd):
    el = context.driver.find_element(By.NAME, "my-password")
    el.clear()
    el.send_keys(pwd)

@when('the user selects "{option}" from the dropdown')
def step_select_dropdown(context, option):
    select = Select(context.driver.find_element(By.NAME, "my-select"))
    select.select_by_visible_text(option)

@when('the user clicks the submit button')
def step_click_submit(context):
    context.driver.find_element(
        By.CSS_SELECTOR, "button[type='submit']").click()

@then('the success message should be displayed')
def step_verify_success(context):
    wait = WebDriverWait(context.driver, 10)
    msg = wait.until(EC.visibility_of_element_located((By.ID, "message")))
    assert msg.is_displayed()

@then('the text input should be enabled')
def step_text_enabled(context):
    el = context.driver.find_element(By.NAME, "my-text")
    assert el.is_enabled()

@then('the disabled input should not be editable')
def step_disabled(context):
    el = context.driver.find_element(By.NAME, "my-disabled")
    assert not el.is_enabled()

Cucumber BDD Intermediate Cucumber + Selenium Integration

Written by PV

© 2026 All Rights Reserved