Integration of Cucumber with Selenium and TestNG


Cucumber is a BDD Tool and Selenium Webdriver is use for the automation of web application. Imagine we need to build a test framework which can be used by business to understand the test scenarios and as well can test the web application. This can be achieved by integrating Cucumber with Selenium. I’m going to use TestNG as the Test Automation tool for assertions.

In this tutorial, I’ll create a BDD Framework for the testing of web applications using Selenium Webdriver with TestNG . This framework consists of:-

  1. Cucumber Java- 6.10.4
  2. Cucumber TestNG – 6.10.4
  3. Java 11
  4. TestNG – 7.4.0
  5. Maven – 3.8.1
  6. Selenium – 3.141.59

Steps to setup Cucumber Test Automation Framework with Selenium and TestNG

  1. Download and Install Java on system
  2. Download and setup Eclipse IDE on system
  3. Setup Maven
  4. Create a new Maven Project
  5. Create source folder – src/test/resources to create test scenarios in Feature file
  6. Add Selenium, TestNG and Cucumber dependencies to the project
  7. Add Maven Compiler Plugin
  8. Create a feature file under src/test/resources
  9. Create the Step Definition class or Glue Code
  10. Create a TestNG Cucumber Runner class
  11. Run the tests from TestNG Runner
  12. Run the tests from TestNG.xml
  13. Run the tests from Command Line
  14. Cucumber Report Generation
  15. TestNG Report Generation

Step 1- Download and Install Java

Cucumber and Selenium needs Java to be installed on the system to run the tests. Click here to know How to install Java.

Step 2 – Download and setup Eclipse IDE on system

The Eclipse IDE (integrated development environment) provides strong support for Java developer which is needed to write Java code. Click here to know How to install Eclipse.

Step 3 – Setup Maven

To build a test framework, we need to add a number of dependencies to the project. It is very tedious and cumbersome process to add each dependency manually. So, to overcome this problem, we use a build management tool. Maven is a build management tool which is used to define project structure, dependencies, build, and test management. Click here to know How to install Maven.

Step 4 – Create a new Maven Project

Click here to know How to create a Maven project

Below is the Maven project structure. Here,

Group Id – com.example
Artifact Id – Cucumber_TestNGDemo
Version – 0.0.1-SNAPSHOT
Package – com. example. Cucumber_TestNGDemo

Step 5 – Create source folder src/test/resources to create test scenarios in Feature file

When a new Maven Project is created, it has 2 folders – src/main/java and src/test/java as shown in below image. To create test scenarios, we need a new source folder called – src/test/resources. To create this folder, right click on your maven project ->select New ->Java and then Source Folder.

Step 6 – Add Selenium, TestNG and Cucumber dependencies to the project

Add below mentioned Selenium, TestNG, Hamcrest and Cucumber dependencies to the project.

  <!-- -->

<!-- -->
<!-- -->

<!-- -->

<!-- -->

Step 7 – Add Maven Compiler Plugin

The compiler plugin is used to compile the source code of a Maven project. This plugin has two goals, which are already bound to specific phases of the default lifecycle:

  • compile – compile main source files
  • testCompile – compile test source files

Step 8 – Create a feature file (LoginPage.feature) containing all the test scenerios under src/test/resources/features.

Feature file should be saved as an extension of .feature. The test scenarios in Feature file are written in Gherkins language. Add the test scenarios in this feature file. I have added sample test scenarios.

Feature: Login to HRM Application 
   Scenario: Login with valid credentials
    Given User is on Home page
    When User enters username as "Admin"
    And User enters password as "admin123"
    Then User should be able to login sucessfully

Step 9 – Create the stepdefinition class corresponding to the feature file to test the scenarios

public class LoginDefinition {

	WebDriver driver;

	public void setUp() {

		driver = new FirefoxDriver();

		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

	@Given("User is on Home page")
	public void userOnHomePage() {


	@When("User enters username as {string}")
	public void entersUsername(String userName) throws InterruptedException {

		System.out.println("Username Entered");


	@When("User enters password as {string}")
	public void entersPassword(String passWord) throws InterruptedException {

		System.out.println("Password Entered");


	@Then("User should be able to login sucessfully")
	public void sucessfullLogin() throws InterruptedException {

		String newPageText = driver.findElement("welcome")).getText();
		System.out.println("newPageText :" + newPageText);
		assertThat(newPageText, containsString("Welcome"));


	public void teardown() {



assertThat() and containsString are imported from package:-

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

Step 10 – Create a TestNG Cucumber Runner class to execute the test scenarios

We need to create a class called Runner class to run the tests. This class will use the TestNG annotation @RunWith(), which tells TestNG what is the test runner class.

package com.example.Cucumber_TestNGDemo.runner;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(tags = "", features = "src/test/resources/features/Login.feature", glue = "com.example.Cucumber_TestNGDemo.definitions")

public class CucumberRunnerTest extends AbstractTestNGCucumberTests {


  • AbstractTestNGCucumberTests – Runs each cucumber scenario found in the features as separated test.

Step 11 – Test Execution through TestNG

Go to Runner class and right click Run As TestNG Test. The tests will run as TestNG tests.

This is how the execution console will look like.

Step 12 – Run the tests from TestNG.xml

Create a TestNG.xml as shown below and run the tests as TestNG.

<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name = "Suite1">
  <test name = "Test Demo">
          <class name = "com.example.Cucumber_TestNGDemo.runner.CucumberRunnerTest"/>

Step 13 – Run the tests from Command Line

Run the below command in command prompt to run t he tests and to get the test execution report.

mvn test

The execution screen looks like something as shown below.

Step 14 – Cucumber Report Generation

Add under src/test/resources and add the below instruction in the file.


Below is the image of the Cucumber Report generated using Cucumber Service.

Step 15 – TestNG Report Generation

TestNG generates various type of reports under test-output folder like emailable-report.html, index.html, testng-results.xml.

We are interested in ’emailable-report.html’ report. Open ’emailable-report.html’, as this is a html report open it with browser. Below image shows emailable-report.html.

TestNG also produce “index.html” report and it resides under test-output folder. Below image shows index.html report.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

4 thoughts on “Integration of Cucumber with Selenium and TestNG

  1. HI while click a right click on runner class I cant able to see run as TestNG Test. so that i cant able to run a program Please suggest me .


    1. Hi, you can create testng.xml and should be able to run the tests. If still you are unable to run the tests, then I’m assuming that TestNG is not installed. Let me know if you are able to run the tests from testng.xml or not.


  2. Hi, what happens if I have several scenarios defined in cucumber but I only want to execute one of them using command line?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s