Contents

    Guides

    What Is Cucumber Framework in Selenium and How to Use It

    Published on

    September 22, 2025
    What Is Cucumber Framework in Selenium and How to Use It

    Cucumber is a testing framework that works on the Behavior-Driven Development (BDD) approach. It allows test cases to be written in a human-readable language, so developers, testers, and business teams can all understand what is being tested. 

    When combined with Selenium, Cucumber provides a way to describe browser-based automation in plain text while keeping the technical implementation separate. This makes test automation both collaborative and maintainable. 

    This article explains the essentials of the Cucumber framework in Selenium, its structure, advanced techniques, and how it can be effectively used in real projects.

    Why Cucumber Is Essential for Selenium Automation

    Selenium enables browser automation, but its scripts are written in code that is often inaccessible to non-technical stakeholders. Cucumber adds value by introducing a Behavior-Driven Development (BDD) layer, making tests both descriptive and executable.

    Here are the key reasons why it is essential:

    • Human-readable format: Gherkin syntax allows scenarios to be written in structured plain English. This makes feature files serve as both documentation and automation scripts.
    • Linking requirements with automation: Acceptance criteria from business teams can be directly mapped to executable scenarios. This ensures that what is tested always reflects the agreed functionality.
    • Separation of concerns: Feature files capture intent, step definitions hold Selenium code, and runners manage execution. This prevents overlap of responsibilities and keeps the framework maintainable.
    • Reusability: Common steps like login or navigation can be reused across different features. This reduces redundant work and makes updates faster when functionality changes.
    • Scalability: Tags, hooks, and parameterization enable targeted execution of specific test sets. This supports efficient testing even in large and complex projects.

    Key Features of the Cucumber Framework

    Cucumber provides a structured way to connect plain-text scenarios with Selenium automation. Its key features define how tests are written, executed, and maintained, which makes the framework practical for real-world projects.

    Here are the most important ones to understand:

    • Feature files: These files hold scenarios written in Gherkin language. They describe application behavior in plain English and act as living documentation that can be read by both technical and non-technical teams.
    • Step definitions: These files contain the code that executes each Gherkin step. They connect descriptive text to Selenium commands, so the scenario intent and automation logic remain separate.
    • Test runner files: The runner defines how and where the tests are executed. It specifies the feature file paths, step definition packages, reporting plugins, and tags for scenario selection.
    • Hooks: Hooks run setup or cleanup code before or after scenarios. They are useful for opening and closing browser sessions, creating test data, or resetting the state between test runs.
    • Tags: Tags group and filter scenarios for selective execution. They allow targeted runs such as smoke, regression, or functional subsets without changing the feature files themselves.

    Preparing Your Environment for Cucumber with Selenium

    Before writing tests, the environment must be configured to support both Selenium automation and Cucumber’s BDD structure. Preparation involves setting up the project, adding the required dependencies, and organizing files so feature files, step definitions, and runner classes can work together.

    Here is how to prepare the environment step by step:

    1. Install Java Development Kit (JDK)

    Download and install the latest stable JDK version. Configure the JAVA_HOME environment variable and verify the installation by running java -version in the terminal.

    2. Set up an IDE

    Install an Integrated Development Environment such as IntelliJ IDEA, Eclipse, or Visual Studio Code. These IDEs provide plugins for Cucumber, allowing navigation from feature steps to step definitions.

    3. Create a new Maven or Gradle project

    Start a new project in your IDE. Use Maven (pom.xml) or Gradle (build.gradle) for managing dependencies. This avoids manual downloads and ensures libraries stay updated.

    4. Add Selenium and Cucumber dependencies

    In Maven, update the pom.xml with the required libraries:

    <dependency>

      <groupId>org.seleniumhq.selenium</groupId>

      <artifactId>selenium-java</artifactId>

      <version>4.20.0</version>

    </dependency>

    <dependency>

      <groupId>io.cucumber</groupId>

      <artifactId>cucumber-java</artifactId>

      <version>7.14.0</version>

    </dependency>

    <dependency>

      <groupId>io.cucumber</groupId>

      <artifactId>cucumber-junit</artifactId>

      <version>7.14.0</version>

    </dependency>

    This links Selenium with Cucumber and enables execution with JUnit.

    5. Download browser drivers

    Install the driver for the browser you plan to test, such as ChromeDriver or GeckoDriver. Place the driver path in the system environment or configure it directly in the code.

    6. Set up project structure

    Create dedicated folders for:

    • src/test/resources/features: to store feature files
    • src/test/java/stepdefinitions: to store step definition classes
    • src/test/java/runners: to store runner files

    This organization keeps files clean and easy to navigate.

    7. Verify setup with a sample feature file

    Write a small feature such as opening a browser and verifying the page title. Link it to a step definition and execute it with the runner. A successful run confirms the environment is ready.

    Writing Effective Tests in Cucumber for Selenium

    Writing effective tests in Cucumber requires a balance between readability, maintainability, and reliability. Tests should clearly reflect business behavior while being structured for efficient automation. Poorly written scenarios can become hard to maintain, difficult to understand, and prone to breaking when the application changes.

    Below are key practices for writing effective Cucumber tests:

    • Focus on business behavior: Scenarios should describe what the application should do, not how the UI elements are manipulated. For example, use “User logs in successfully” rather than “User clicks login button.” This keeps tests understandable to all stakeholders.
    • Keep scenarios concise: Each scenario should test a single behavior or outcome. Avoid overloading a scenario with multiple validations. Concise scenarios are easier to read, maintain, and debug when failures occur.
    • Reuse step definitions: Common actions such as login, navigation, or data entry should be defined once and reused across multiple scenarios. This reduces duplication, improves consistency, and makes maintenance faster.
    • Use Scenario Outlines for parameterization: When testing the same flow with different data, Scenario Outlines with Examples tables avoid duplicating steps. This increases test coverage while keeping feature files clean.
    • Leverage Background sections: Define steps that are common to multiple scenarios in a Background section. This avoids repeating setup steps in every scenario and ensures consistency across tests.
    • Organize feature files logically: Group related functionality into feature files and maintain a clear folder structure. This makes it easier to locate tests, run specific modules, and scale the framework as the project grows.
    • Keep scenarios stable: Avoid hardcoding dynamic values or relying on fragile UI locators. Use reusable methods, wait strategies, and test data management to reduce flakiness in Selenium automation.

    Advanced Techniques in Cucumber Testing

    Once the basics of Cucumber testing are in place, advanced techniques help make tests more maintainable, reusable, and scalable. Features like parameterization, hooks, tags, and data-driven testing reduce duplication, control setup and teardown, and allow targeted execution of scenarios. 

    The following examples illustrate how these techniques can be applied in Selenium projects.

    1. Parameterization with Scenario Outlines

    Parameterization allows the same scenario to be executed with multiple sets of data. Instead of writing separate scenarios for each input, a single Scenario Outline can iterate through different values, reducing duplication and improving coverage.

    2. Hooks

    Hooks allow setup and teardown activities to run automatically before or after scenarios. This ensures that each scenario starts with the required environment and ends with proper cleanup.

    Example in Java:

    @Before

    public void setUp() {

        driver = new ChromeDriver();

        driver.manage().window().maximize();

    }

    @After

    public void tearDown() {

        driver.quit();

    }

    The @Before hook initializes the browser and maximizes the window before each scenario, while @After closes the browser and releases resources after the scenario completes.

    3. Tags for Selective Execution

    Tags provide a way to group scenarios and execute only specific subsets of tests. This is useful for running smoke tests, regression suites, or functional modules independently.

    Example feature file:

    @SmokeTest

    Scenario: Verify login functionality

      Given User is on the login page

      When User enters valid credentials

      Then User should be redirected to the dashboard

    Test runner configuration:

    @CucumberOptions(

        features = "src/test/resources/features",

        glue = "stepdefinitions",

        tags = "@SmokeTest"

    )

    public class TestRunner {}

    Only scenarios tagged with @SmokeTest will run. This allows efficient testing without modifying the feature files themselves.

    4. Data-Driven Testing with External Files

    Data-driven testing allows tests to read inputs from external sources like CSV, Excel, or JSON. This approach separates test data from test logic, making tests easier to maintain and flexible when input values change.

    Example step definition reading a CSV file:

    @When("User logs in with data from CSV")

    public void loginWithCsvData() throws IOException {

        List<String[]> testData = CSVReader.read("testdata.csv");

        for(String[] row : testData) {

            driver.findElement(By.id("username")).sendKeys(row[0]);

            driver.findElement(By.id("password")).sendKeys(row[1]);

            driver.findElement(By.id("loginButton")).click();

            driver.navigate().back();

        }

    }

    Here, multiple login scenarios are executed dynamically based on the CSV content, reducing the need to hardcode credentials in the feature file.

    5. Reusable Step Libraries

    Complex or repeated Selenium actions can be moved to reusable methods. This allows step definitions to remain concise and reduces duplication across scenarios.

    Example reusable method:

    public void login(String username, String password) {

        driver.findElement(By.id("username")).sendKeys(username);

        driver.findElement(By.id("password")).sendKeys(password);

        driver.findElement(By.id("loginButton")).click();

    }

    Step definitions can call this method in multiple scenarios, making updates faster when UI locators change.

    Integrating Cucumber Tests with Selenium in Real Projects

    Running isolated tests is only part of automation. In real projects, Cucumber tests must integrate with Selenium in a way that supports team collaboration, continuous integration, and consistent reporting. Proper integration ensures tests are reusable, maintainable, and can be executed across environments and browsers.

    1. Organizing Project Structure

    A well-defined project structure separates feature files, step definitions, and test runners. This improves readability and maintainability when the project scales.

    Keeping feature files, step definitions, and runners in separate folders prevents confusion and allows parallel development by multiple testers.

    2. Using Test Runners

    Cucumber test runners define which feature files and step definitions to execute, as well as reporting formats and tags. This enables selective execution of tests in CI/CD pipelines.

    Example runner configuration:

    @RunWith(Cucumber.class)

    @CucumberOptions(

        features = "src/test/resources/features",

        glue = "stepdefinitions",

        plugin = {"pretty", "html:target/cucumber-report.html"},

        tags = "@Regression"

    )

    public class TestRunner {}

    This configuration executes all scenarios tagged with @Regression and generates an HTML report for easy analysis.

    3. Cross-Browser Execution

    In real projects, tests must run across multiple browsers to ensure compatibility. Selenium WebDriver can be configured to run on different browsers using a parameter or a configuration file.

    Example cross-browser setup:

    public WebDriver initializeDriver(String browser) {

        if(browser.equalsIgnoreCase("chrome")) {

            return new ChromeDriver();

        } else if(browser.equalsIgnoreCase("firefox")) {

            return new FirefoxDriver();

        } else {

            throw new IllegalArgumentException("Unsupported browser: " + browser);

        }

    }

    This allows the same Cucumber tests to be executed on Chrome, Firefox, or any supported browser without changing the feature files.

    4. Integration with CI/CD

    Cucumber tests can be integrated into pipelines such as Jenkins or GitHub Actions. By running tests automatically on code commits or scheduled builds, teams get early feedback on potential issues.

    Example Jenkins command:

    mvn clean test -Dcucumber.options="--tags @Regression"

    This executes only the regression scenarios and generates reports automatically, enabling fast feedback and traceability.

    5. Reporting and Analysis

    Cucumber supports multiple reporting plugins. HTML, JSON, and advanced reports like ExtentReports provide clear insights into scenario execution, failures, and trends. Well-integrated reporting ensures stakeholders can track test results without reading raw logs.

    Example plugin configuration in runner:

    plugin = {"pretty", "html:target/cucumber-report.html", "json:target/cucumber.json"}

    This produces both a human-readable HTML report and a JSON report that can be used for dashboards or CI/CD analytics.

    Common Pitfalls and How to Avoid Them

    Even with a robust Cucumber and Selenium setup, teams often face challenges that reduce test reliability and maintainability. 

    Here are some common pitfalls and ways to avoid them:

    • Writing scenarios that are too technical: Focus on business behavior rather than Selenium actions. Use step definitions for technical execution.
    • Overloading scenarios: Test one behavior per scenario. Use Scenario Outlines with Examples for variations.
    • Poor step reusability: Create reusable step libraries for common actions to reduce maintenance overhead.
    • Hardcoding test data: Use parameterization or external data sources like CSV, Excel, or JSON files.
    • Ignoring waits and synchronization: Implement explicit or implicit waits to avoid flaky tests due to timing issues.
    • Skipping browser and environment variations: Run tests across multiple browsers and environments to ensure broader coverage.

    Conclusion

    Integrating Cucumber with Selenium offers a robust framework for behavior-driven development, enabling teams to write tests in natural language while automating browser interactions. This combination enhances collaboration between developers and non-technical stakeholders, ensuring that the application meets business requirements. 

    For scalable Selenium testing, BrowserStack provides access to over 3,500 real desktop and mobile browsers. It supports parallel test execution, local testing, and seamless integration with CI/CD pipelines, ensuring comprehensive cross-browser compatibility and faster feedback cycles without the need for maintaining an in-house infrastructure.

    Run Selenium Tests on Cloud

    Data-rich bug reports loved by everyone

    Get visual proof, steps to reproduce and technical logs with one click

    Make bug reporting 50% faster and 100% less painful

    Rating LogosStars
    4.6
    |
    Category leader

    Liked the article? Spread the word

    Put your knowledge to practice

    Try Bird on your next bug - you’ll love it

    “Game changer”

    Julie, Head of QA

    star-ratingstar-ratingstar-ratingstar-ratingstar-rating

    Overall rating: 4.7/5

    Try Bird later, from your desktop