Selenium with Cucumber (BDD Framework Tutorial)

โšก Smart Summary

Selenium with Cucumber forms a powerful Behavior Driven Development (BDD) framework that combines plain-English Gherkin syntax with Selenium WebDriver automation. This tutorial explains the prerequisites, walks through three hands-on scenarios, and shows how feature files, step definitions, and a TestRunner class integrate to execute browser tests.

  • ๐Ÿฅ’ Core Definition: Cucumber is a BDD testing tool that describes application behavior in plain English using Gherkin, and Selenium drives the browser actions underneath.
  • ๐Ÿค Stakeholder Bridge: Cucumber connects business analysts, manual testers, automation engineers, and developers through a shared, readable test specification.
  • ๐Ÿ“ฆ Required Jars: Selenium standalone, Cucumber-core, Cucumber-java, Cucumber-junit, Gherkin, JUnit, and Hamcrest-core form the minimum dependency set.
  • ๐Ÿงฑ Three Project Files: A .feature file with scenarios, a Steps.java step-definition class, and a Runner.java TestRunner with @CucumberOptions glue.
  • ๐Ÿ” Data-Driven Tests: Use Scenario Outline plus Examples tables to run the same flow against multiple sets of credentials in one execution.

Selenium with Cucumber (BDD Framework Tutorial)

In this tutorial, you will learn how to integrate Cucumber with Selenium WebDriver to build a Behavior Driven Development (BDD) framework that is readable, maintainable, and data-driven.

What is Cucumber?

Cucumber is a testing tool that supports Behavior Driven Development (BDD). It describes the behavior of an application in simple English text using the Gherkin language, which lets non-developers contribute to test specifications without learning a programming language. Learn more in our Cucumber tutorial.

What is Selenium?

Selenium is an automation tool used for Functional Testing of web-based applications. Selenium supports multiple programming languages including Java, Ruby, Python, and C#, which makes it a natural fit for teams with mixed technical backgrounds. Learn more in our Selenium tutorial.

Why Use Cucumber with Selenium?

Cucumber and Selenium are two of the most widely adopted testing technologies. Most organizations already use Selenium for functional testing and want to layer Cucumber on top because Cucumber makes it easy to read and understand the application flow.

Cucumber is based on the Behavior Driven Development framework that acts as a bridge between the following roles:

  1. Software Engineer and Business Analyst.
  2. Manual Tester and Automation Tester.
  3. Manual Tester and Developers.

The Cucumber BDD framework also helps clients understand the application code because it uses the Gherkin language, which is in plain text. Anyone in the organization can follow the behavior of the software. The Gherkin syntax is in simple readable English, which keeps the specification accessible to non-technical readers.

Cucumber with Selenium BDD framework overview

Prerequisites for Using Cucumber with Selenium

Before we start the Cucumber-Selenium integration, you need the following items:

Selenium jar files:

  • Selenium-server-standalone

You can download them at https://www.selenium.dev/downloads/.

Selenium downloads page for jar files

Jar files for Cucumber:

  • Cucumber-core
  • Cucumber-html
  • Cobertura code coverage
  • Cucumber-java
  • Cucumber-junit
  • Cucumber-jvm-deps
  • Cucumber-reporting
  • Hamcrest-core
  • Gherkin
  • JUnit

You can download them at https://mvnrepository.com/search?q=Cucumber. You will need to search for each jar and download them one by one.

For example, here is how to download one of the jar files, the “Cucumber-core” library.

Click the download link above. It redirects to the Maven Repository site. Now search for the specific jar (in this case “Cucumber Core”) as shown in the screenshot below:

Searching for Cucumber Core jar in Maven Repository

On the next page, click version 1.2.2:

Select Cucumber Core version 1.2.2 in Maven

On the next screen, click Download to grab the Cucumber Core jar file:

Download the Cucumber Core jar file

Note: For convenience, we have bundled the Maven jar files required here. These jars may become outdated and incompatible over time, so we recommend downloading them yourself using the method described above.

Automation Testing Using Cucumber with Selenium

Let us walk through how to use Cucumber with Selenium step by step. Here we will cover three scenarios:

  • Scenario 1: Print text in the console.
  • Scenario 2: Enter login credentials and reset the value.
  • Scenario 3: Enter login credentials on Guru99 and reset the value โ€” repeated for 3 sets of data.

Scenario 1: Print Text in the Console

In this scenario, we simply print text in the console using Cucumber.

Step 1) Create a project in Eclipse.

Create a Java project with the name “CucumberWithSelenium” as shown in the screenshot below.

Create CucumberWithSelenium Java project in Eclipse

Java project name configuration in Eclipse

Step 2) Add the jar files to the project.

Right-click the project > Properties > Java Build Path. Add all the libraries you downloaded earlier.

Add Cucumber and Selenium jar files to Java Build Path

Step 3) Create a feature file.

To create a feature file, first create a Features folder as shown in the screenshot below.

Create Features folder in Eclipse project

Enter the folder name “Features” and click Finish.

Configure Features folder name in Eclipse

Features folder visible in project explorer

Now create a feature file inside the Features folder named MyTest.feature. The process is similar to creating a folder.

Create MyTest.feature file in Features folder

Note: You may need to install the Cucumber Eclipse Plugin first. Go to Help โ†’ Install New Software, paste the link http://cucumber.github.io/cucumber-eclipse/update-site/, and install it.

Step 4) Write the scenarios.

The lines below are written in MyTest.feature using the Gherkin language:

Feature: Reset functionality on login page of Application

Scenario: Verification of Reset button

Given Open the Firefox and launch the application

When Enter the Username and Password

Then Reset the credential

Code Explanation

Line 1) Defines the business functionality.

Line 2) Defines a scenario to test.

Line 3) Defines the precondition (Given).

Line 4) Defines the action to perform (When).

Line 5) Defines the expected outcome or result (Then).

Step 5) Write the Selenium TestRunner script for the Cucumber framework design.

Create a TestRunner package and then a Runner.java class file under it.

package TestRunner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = {"StepDefinition"})
public class Runner
{

}

In the above Cucumber Java example, we run the cucumber test using the following annotations:

@RunWith() tells JUnit about the test runner class that starts executing our tests.

@CucumberOptions() sets test properties such as the feature file path and step-definition glue package.

Screenshot of the TestRunner file:

Runner.java TestRunner script for Cucumber

Step 6) Create the Step Definition script.

Now create a StepDefinition package and then a Steps.java script file under it. Inside Steps.java we write the Selenium logic that maps to each Gherkin step.

package StepDefinition;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class Steps {

    @Given("^Open the Firefox and launch the application$")
    public void open_the_Firefox_and_launch_the_application() throws Throwable
    {
        System.out.println("This Step open the Firefox and launch the application.");
    }

    @When("^Enter the Username and Password$")
    public void enter_the_Username_and_Password() throws Throwable
    {
        System.out.println("This step enter the Username and Password on the login page.");
    }

    @Then("^Reset the credential$")
    public void Reset_the_credential() throws Throwable
    {
        System.out.println("This step click on the Reset button.");
    }
}

The class is named Steps, and Cucumber annotations map each method to a feature-file step:

@Given defines the method to open Firefox and launch the application.

@When defines the method to enter the username and password.

@Then defines the method to reset the credentials.

Each method simply prints a message to the console.

The screenshot below shows the Steps.java script and the project tree:

Steps.java script and project tree in Eclipse

Note: A step definition is the code you want Cucumber to execute when it matches a feature-file step.

Step 7) Execute the script.

Run this script from the TestRunner file (Runner.java) as shown below.

Execute Runner.java to run Cucumber test

Step 8) Analyze the output.

On executing Runner.java, Cucumber prints the same text defined in Steps.java to the console.

Console output from Cucumber test execution

Scenario 2: Enter Login Credentials and Reset the Value

Here we enter credentials on the Guru99 demo login page and reset the value.

For Scenario 2 we only need to update Steps.java. Here we add the real Selenium code. First, make sure the Selenium jar file is added to the project.

Confirm Selenium jar file is on project classpath

Step 1) Update the Steps.java script as shown below.

package StepDefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class Steps {

    WebDriver driver;

    @Given("^Open the Firefox and launch the application$")
    public void open_the_Firefox_and_launch_the_application() throws Throwable
    {
        System.setProperty("webdriver.gecko.driver", "E://Selenium//Selenium_Jars//geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://demo.guru99.com/v4");
    }

    @When("^Enter the Username and Password$")
    public void enter_the_Username_and_Password() throws Throwable
    {
        driver.findElement(By.name("uid")).sendKeys("username12");
        driver.findElement(By.name("password")).sendKeys("password12");
    }

    @Then("^Reset the credential$")
    public void Reset_the_credential() throws Throwable
    {
        driver.findElement(By.name("btnReset")).click();
    }
}

Screenshot of the above Selenium script:

Scenario 2 Steps.java with Selenium logic

Step 2) Execute the script.

After updating Steps.java, run Runner.java.

Step 3) Analyze the output. In the output you will see the following:

  • Browser launched.
  • Guru99 bank demo site opens.
  • Username and password are entered on the login page.
  • The Reset button clears the values.

Scenario 2 animation showing Cucumber Selenium test run

Scenario 3: Run the Login and Reset Flow with 3 Sets of Data

Here we need to update both Steps.java and the feature file to drive the same flow with three sets of credentials.

Step 1) Update the feature file as shown below.

We use the Scenario Outline and Examples syntax so Cucumber loops the same scenario across each data row.

Feature: Reset functionality on login page of Application

Scenario Outline: Verification of reset button with multiple credentials

Given Open the Firefox and launch the application

When Enter the Username <username> and Password <password>

Then Reset the credential

Examples:
    | username | password  |
    | User1    | password1 |
    | User2    | password2 |
    | User3    | password3 |

Feature file with Scenario Outline and Examples table

Step 2) Update the Steps.java script.

Here we update the method signatures so they accept parameters from the Examples table.

package StepDefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class Steps {

    WebDriver driver;

    @Given("^Open the Firefox and launch the application$")
    public void open_the_Firefox_and_launch_the_application() throws Throwable
    {
        System.setProperty("webdriver.gecko.driver", "E://Selenium//Selenium_Jars//geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://demo.guru99.com/v4");
    }

    @When("^Enter the Username \"(.*)\" and Password \"(.*)\"$")
    public void enter_the_Username_and_Password(String username, String password) throws Throwable
    {
        driver.findElement(By.name("uid")).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
    }

    @Then("^Reset the credential$")
    public void Reset_the_credential() throws Throwable
    {
        driver.findElement(By.name("btnReset")).click();
    }
}

Updated Steps.java with parameterized credentials

Step 3) Execute the updated script.

The screen below shows a successful execution and the time taken by each data set.

Successful execution of Scenario 3 with three data sets

Step 4) Analyze the output. The following steps repeat for each of the 3 sets of data:

  • Browser launched.
  • Guru99 bank demo site opens.
  • Username and password are entered on the login page.
  • The Reset button clears the values.

Scenario 3 animation showing parameterized Cucumber test run

FAQs

Cucumber is a BDD framework that runs scenarios written in Gherkin. Selenium is a browser-automation library that drives web pages. Cucumber does not interact with browsers directly โ€” it calls Selenium step definitions to perform the actual UI actions.

Cucumber feature files use Gherkin, a plain-English domain-specific language that organizes scenarios with keywords such as Feature, Scenario, Given, When, Then, And, But, Scenario Outline, and Examples. Gherkin is readable by both technical and non-technical stakeholders.

Use the Scenario Outline keyword with an Examples table. Cucumber executes the same scenario once per row, substituting placeholders such as <username> and <password> with the values from each row. This is the standard data-driven pattern in Cucumber.

AI generates Gherkin scenarios from user stories, recommends step definitions, heals broken locators in Selenium, and suggests Examples data. AI assistants also flag duplicate scenarios, weak assertions, and stale tests, which keeps the BDD suite maintainable as the application changes.

Yes. AI coding assistants read Gherkin feature files and generate matching Java or Kotlin step definitions, including Selenium WebDriver calls, locator strategies, and parameter binding. Developers then refine the generated code, which dramatically reduces boilerplate when bootstrapping new BDD suites.

Summarize this post with: