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:-
- Cucumber Java- 6.10.4
- Cucumber TestNG – 6.10.4
- Java 11
- TestNG – 7.4.0
- Maven – 3.8.1
- Selenium – 3.141.59

Steps to setup Cucumber Test Automation Framework with Selenium and TestNG
- Download and Install Java on system
- Download and setup Eclipse IDE on system
- Setup Maven
- Create a new Maven Project
- Create source folder – src/test/resources to create test scenarios in Feature file
- Add Selenium, TestNG and Cucumber dependencies to the project
- Add Maven Compiler Plugin
- Create a feature file under src/test/resources
- Create the Step Definition class or Glue Code
- Create a TestNG Cucumber Runner class
- Run the tests from TestNG Runner
- Run the tests from TestNG.xml
- Run the tests from Command Line
- Cucumber Report Generation
- 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.
<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.10.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>6.10.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
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
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
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
@ValidCredentials
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;
@Before
public void setUp() {
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\Vibha\\Software\\geckodriver-v0.26.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Given("User is on Home page")
public void userOnHomePage() {
driver.get("https://opensource-demo.orangehrmlive.com/");
}
@When("User enters username as {string}")
public void entersUsername(String userName) throws InterruptedException {
System.out.println("Username Entered");
driver.findElement(By.name("txtUsername")).sendKeys(userName);
}
@When("User enters password as {string}")
public void entersPassword(String passWord) throws InterruptedException {
System.out.println("Password Entered");
driver.findElement(By.name("txtPassword")).sendKeys(passWord);
driver.findElement(By.id("btnLogin")).submit();
}
@Then("User should be able to login sucessfully")
public void sucessfullLogin() throws InterruptedException {
String newPageText = driver.findElement(By.id("welcome")).getText();
System.out.println("newPageText :" + newPageText);
assertThat(newPageText, containsString("Welcome"));
}
@After
public void teardown() {
driver.quit();
}
}
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 "http://testng.org/testng-1.0.dtd">
<suite name = "Suite1">
<test name = "Test Demo">
<classes>
<class name = "com.example.Cucumber_TestNGDemo.runner.CucumberRunnerTest"/>
</classes>
</test>
</suite>
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 cucumber.properties under src/test/resources and add the below instruction in the file.
cucumber.publish.enabled=true
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!!
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 .
LikeLike
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.
LikeLike
Hi, what happens if I have several scenarios defined in cucumber but I only want to execute one of them using command line?
LikeLike
You can add tags to the scenarios and can run a particular scenario using = mvn test -Dcucumber.options=”–tags @ValidCredentials”
There is a tutorial which explains how to run cucumber tests in commandline in detail. https://qaautomation.expert/2021/03/26/run-cucumber-test-from-command-line/
LikeLike