How To Perform Automation Testing with Cucumber and Selenium

Behavior Driven Development (BDD) Framework enables software testers to complete test scripting in simple, easy to understand English phrases. BDD mainly focuses on the predicted actions of the program and verify the user acceptance criteria. Cucumber is one of the best tools in the BDD Framework.

What is Cucumber?

Cucumber is an open-source testing approach that facilitates Behavior Driven Development. The tests are written in simple English text using Gherkin language explaining the expected actions of the web based application from consumer point of view.

What is Selenium?

Selenium is an open source automated testing framework for a range of tools and libraries to validate web applications across different browsers and platforms.

Why should we combine Cucumber with Selenium?

Selenium testing provides accuracy and speed to the development cycle, collaborating cucumber with selenium supplements an extra edge to the automation process by making it easy for the non-technical management stakeholders.

Cucumber framework comprises of three partsFeature File, Step Definitions, and the Test Runner File.

Feature File:

A single unit/ function in an application is called a feature which in turn will have scenarios that need to be tested with selenium aided by cucumber. As an automation test script a Feature File stores data about features, their descriptions, and the scenarios to be tested.

The Keywords used to define actions in Cucumber are called Annotations.

GIVEN- pre-condition of the test

WHEN- condition of the test

THEN-post-condition of the test

Step Definitions:

A feature file may have multiple steps, and each step will have a Step Definition associated.

For execution of a Step in a Scenario, Cucumber identifies a matching Step Definition and performs the execution. Step definition file maps each step of a scenario in the feature file(with annotations (Given/When/Then) to the code. The Step Definition file is identified with the help of the Glue code in Cucumber Options.

Test Runner File:

To run the test, one needs a Test Runner File, which is a JUnit Test Runner Class containing the Step Definition location and the other primary metadata required to run the test.

The Test Runner File uses @RunWith() annotation instructs the start of test execution and @CucmberOptions() annotation helps define properties such as like feature file, step definition and so on for the cucumber test

Below is the step-by-step approach for automation testing using Cucumber with Selenium.

Step 1) Create a new maven Project in Eclipse.

Step 2) Add maven dependencies: Cucumber Java, Cucumber Junit, Junit, Selenium Java

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->






<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->







<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->


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

<artifactId> selenium-java </artifactId>



<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->







Step 3) Install Cucumber plug-in from Eclipse marketplace

Step 4) Create a feature file:

· Create a new folder Features under src/test/resources

· Create a feature file with the extension “.feature” inside the feature folder.

· Add the required contents into the feature file

Feature: Feature to test password match functionality

Scenario: Check password entered in two fields match

Given user is in Login page

When User enters password and reenterpassword

And clicks on submit button

Then User is navigated to registration page

Step 5) Add a class file (Eg: passwordmatch) under src/test/java package to write the step definitions using Gherkin language

package StepDefinition;

import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;

public class passwordmatch {

@Given("user is in Login page")

public voiduser_is_in_login_page() {

System.out.println("User inside the login page");


@When("User enters password and reenterpassword")

public void user_enters_password_and_reenterpassword() {

System.out.println("User enters password in both the fields");


@When("clicks on submit button")

public void clicks_on_submit_button() {

System.out.println("User clicks on submit button");


@Then("User is navigated to registration page")

public void user_is_navigated_to_registration_page() {

System.out.println("User enters the registration page if both the

passwords are correct");


Step 6) Create a Runner class: Create new package(Eg: ‘TestRunner’ package) and a class file(‘Runner.java’) within it.

package StepDefinition;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

import io.cucumber.junit.CucumberOptions;


@CucumberOptions(features="src/test/resources/Features",glue= {"StepDefinition"})

public class runner {


Format Option can be used to specify different formatting options for the output reports. Various options that can be used as for-matters are: Pretty, HTML, JSON, Junit.

Step 7) Execute the runner class: Cucumber will run the script and the result will be shown in the left-hand side project explorer window in JUnit tab. The print statements in the step definitions file are printed in the console.


Cucumber can be used without Selenium, just as how the latter can be used without the former. However, when combining both we can automate a web application with clear and understandable tests that business stakeholders can understand. Thus, Selenium and Cucumber can be used together in most of the projects where agile methodology as Behavior Driven Development is followed.

72 views0 comments

Recent Posts

See All