Integration of Selenium WebDriver with Cucumber – Selenium Tutorial #31

In last tutorial we discussed Cucumber tool, its usage and different features.

Moving ahead in our free Selenium online training series, we will discuss how to set up a cucumber project and will discuss about integration of selenium WebDriver with Cucumber.

We will set up Cucumber project with Maven. To set up Maven in your system please refer this tutorial on Maven from the same series.

Cucumber Project Setup:

Step #1: Create New Maven Project:
Right Click -> New -> Others -> Maven -> Maven Project -> Next

Step #2: Now the project will look like this:

Cucumber Selenium 1

Step #3: Add below dependencies in pom.xml


Step #4: Create a sample.feature file under src/test/resources.

Feature: To test my cucumber test is running
I want to run a sample feature file.

Scenario: cucumber setup

Given sample feature file is ready
When I run the feature file
Then run should be successful

Step #5: Create a class under src/test/java which will implement all the steps.

public class stepDefinition {
       @Given("^sample feature file is ready$")
       public void givenStatment(){
              System.out.println("Given statement executed successfully");
       @When("^I run the feature file$")
       public void whenStatement(){
              System.out.println("When statement execueted successfully");
       @Then("^run should be successful$")
       public void thenStatment(){
              System.out.println("Then statement executed successfully");

Step #6: Create a Junit runner to run the test.

@Cucumber.Options(format={"pretty","html:reports/test-report"},tags= "@smokeTest")
public class CucumberRunner {

Provide the path of the report as given here. The reports will store in ‘test-report’ folder under project folder and “pretty” format specifies the type of report.

Step #7: Junit Result and Test Report:

Below is the report when the cucumber test is successful. Green bar in Junit describes the test is passed. Similarly red bar describes that test has failed.

Cucumber Selenium 2


If we want to use default reporting then navigate the path mentioned in Junit Runner. In this case we have given path as reports->test-reports->index.html.

Open this report in Internet Explorer or in Firefox to verify the result. Below is the sample of the report:

Cucumber Selenium 3

Cucumber and Selenium WebDriver:

Cucumber framework can be used to test the web based applications along with Selenium WebDriver. The test cases are written in simple feature files which are easily understood by managers, non-technical stake holders and business analysts. And those feature file steps are implemented in step definition file. If you are using maven then you have to add dependencies for Cucumber and WebDriver.

So here is the sample test case we have implemented using Cucumber and WebDriver. As given below, the scenario in feature file is self-explanatory.

Feature: Login Feature File
Scenario: Login scenario test for Gmail

Given navigate to gmail page
When user logged in using username as “userA” and password as “password”
Then home page should be displayed

WebDriver Implementation in Cucumber stepDefinitions:

public class stepDefinition {
WebDriver dr;
@Given("^navigate to gmail page$")
public void navigate(){
       dr=new FirefoxDriver();
@When ("^user logged in using username as \"(.*)\" and password as \"(.*)\"$")
public void login(String username,String password){
       dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
@Then("^home page should be displayed$")
public void verifySuccessful(){
      String expectedText="Gmail";
      String actualText=         dr.findElement(By.xpath("//*[@id='gbq1']/div/a/span")).getText();
      Assert.assertTrue("Login not successful",expectedText.equals(actualText));

In this test we have used the Firefox as the browser to test the Gmail login functionality.
Clearly WebDriver object is a class variable and used across the class.

Given statement initializes the browser and navigates to the page.
When statement logs into the application using username as “userA” and password as “password”. Both the values ‘username’ and ‘password’ are passed from feature file and both the values to be used in the same order.
Then Statement only validates the conditions after logging into the application.

This is a sample test describing the usage of Cucumber and Selenium. You can create multilayer architecture depending upon your project requirement.


In this Tutorial we have covered most of the Cucumber concepts which includes Cucumber features and its usage along with WebDriver.

This reduces the complexity of code which is written to design the traditional frameworks like Keyword Driven and Hybrid Framework. Cucumber is used in most of the project where people follow agile methodology as Behavior Driven Development is an Agile Software practice.

Next Tutorial #32: We have now completed all technical tutorials from this Selenium training series. Next, we will post about few important general topics like ‘effort estimation for Selenium projects’ and ‘Selenium interview questions with answers’.

Please post your queries regarding to Cucumber.


#1 Summit

Hi very useful tool.
waiting for selenium interview questions tutorial.

#2 Rahul

Use below dependencies





public class stepDefinition {
@Given(“^sample feature file is ready$”)
public void givenStatment(){
System.out.println(“Given statement executed successfully”);
@When(“^I run the feature file$”)
public void whenStatement(){
System.out.println(“When statement execueted successfully”);
@Then(“^run should be successful$”)
public void thenStatment(){
System.out.println(“Then statement executed successfully”);

And for runner please provide feature path features=”src/test/resources”

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

@Cucumber.Options(format={“pretty”,”html:reports/test-report”}, features=”src/test/resources”,tags= “@smokeTest”)
public class CucumberRunner {

Happy coding

#3 Anbu


Really it was very useful and easy understand as well as i have learned a lot through this… thanks a lot..

#4 Huned Mun


It is really Awesome !!!

#5 Xuefei Hu

In Junit runner, we should give @CucumberOptions, @Cucumber.Options is not working….

tags= “@smokeTest”)

#6 Mallikarjun

sir is it supports java to work on calabash and cucumber together?????????? pls take class on that from ur side it will very useful

#7 Vibhav


Can we use @test, @before junit annotations along with cucumber annotations like @given etc

for example, can we write like below? if so, how will be the junit report? pls reply.

@Given(“^user is on homepage$”)
public void launchhomepage()

@When(“^user enters username and password$”)
@And(“^user clicks login button$”)
public void doLogin()

#8 Charlie

You say to create a junit runner to run the test but you dont say where to create it? What file? What folder? Where?

Step #6: Create a Junit runner to run the test.
@Cucumber.Options(format={“pretty”,”html:reports/test-report”},tags= “@smokeTest”)
public class CucumberRunner {

#9 John P.

Thank you so much for taking your personal time and effort to write this tutorial. It’s indeed absolutely useful for those starting in the Cucumber/SeleniumWebDriver world. Much appreciated.

#10 manimozhi

i followed the same steps but it shows ” no feature found “

#11 raja

This is very good tutorial to start working with Selenium and it depends on individual, how efficiently use this tutorial and develop framework for his project requirements.

#12 Krishna

If you found problem in using regular expression at When statement like @When(“^user logged in using username as \”(.*)\” and password as \”(.*)\”$”) just made change as follows @When (“^user logged in using username as (.*) and password as (.*)$”)

#13 Srini

I am test professional working on BI/ETL
have anyone tried automation testing for BI/ETL projects if so, can you please share some perspective about automation in ETL/BI projects. i am planning to come up with strategy for performance testing for regression suite

#14 Suni

HI , Thanks for the valuable efforts you put.Excellent work.
Every time when i execute this script new report is overridden on to old one. So what is the source code to see all the reports instead of single one.
awaiting your reply!!!!!
Thanks :)

#15 minnu kumari

where we should create runner class

#16 naveen

very good tutorial, appreciate!!!

#17 Shivani

Can cucumber use to run window based application or interfaces?

#18 Syed

Can we write multiple scenarios in a single .feature file?

#19 Harshika Jain

@Syed yes u can create multiple scenarios in one feature file.
User Background for common set of pre-conditions.
User multiple scenarios – when then conditions in one feature file.

#20 Lini

Could you please provide an example for cross browser testing using selenium and cucumber?

#21 Charles Radley

I have the same question as Charlie and minnu kumari, i.e. where do we put the runner class ? Is Junit a dependency for Cucmuber ? Or will it run without Junit ?

#22 Ravi Teja Pendyala

Hi Charles, you can create a new Junit test case inside the src/test/java with below code & run as Junit test

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
import cucumber.junit.Cucumber.Options;

@Options(format={“pretty”,”html:reports/test-report”},features = “Feature\\seleniumsample.feature”)
public class CucumberRunner


#23 Jul Balgue

You could use this as a reference.


#24 Peter Chun

Hi . I wan’t to get handler of diglog when click [@id=’pop’]”)).click(); show popup. But i only get handler of parent window. Please help me !!!!!!
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
System.out.println(“So1 “+parentWindowHandler);
Set handles = driver.getWindowHandles(); // get all window handles
Iterator iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler =;
System.out.println(“So2 “+subWindowHandler);

#25 Himadri

Cannot Run the programs. facing below issues

1. sample.feature File Warning “Step does not have a matching glue code”

2. “No tests found with Test runner Junit 4.”

Leave a Comment