Integration of Cucumber7 with Selenium and JUnit5


I have created a lot of tutorials on creating Test Frameworks by integrating JUnit4 with Selenium, Cucumber, Serenity, Rest API, Springboot. This tutorial explain the steps to Integrate Cucumber7 with JUnit5.

JUnit 5 is composed of several different modules from three different sub-projects.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

We can use the JUnit Platform to execute Cucumber scenarios.

Add the cucumber-junit-platform-engine dependency to your pom.xml:


This will allow IntelliJ IDEA, Eclipse, Maven, Gradle, etc, to discover, select and execute Cucumber scenarios.


  1. Java Version 11 installed
  2. Eclipse or IntelliJ installed
  3. Maven or Gradle installed and setup
  4. Cucumber Eclipse Plugin installed

Project Structure

Implementation Steps

Step 1- Download and Install Java

Cucumber and Selenium need 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 the system

The Eclipse IDE (integrated development environment) provides strong support for Java developers, 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 a 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 that is used to define project structure, dependencies, build, and test management. Click here to know How to install Maven.

Step 4 – Install Cucumber Eclipse Plugin (Only for Eclipse IDE)

The Cucumber Eclipse plugin is a plugin that allows eclipse to understand the Gherkin syntax. The Cucumber Eclipse Plugin highlights the keywords present in Feature File. Click here to know more – Install Cucumber Eclipse Plugin.

Step 5 – 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 – Cucumber7JUnit5Demo
Version – 0.0.1-SNAPSHOT
Package – com. example. Cucumber7JUnit5Demo

Step 6 – Add Maven dependencies to the POM

Add the dependencies to the POM.xml.

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">







		<!-- JUnit Platform -->



		<!-- Selenium -->

		<!-- Web Driver Manager -->

		<!-- Apache Common -->




Step 7 – Create a feature file in src/test/resources

Below is a sample feature file. I have also added a failed scenario in @FaceBookLink. Feature file should be saved as an extension of .feature. Add the test scenarios in this feature file. I have added sample test scenarios. The test scenarios are written in Gherkins language.

Feature: Login to HRM Application 

    Given User is on HRMLogin page ""
   Scenario: Login with valid credentials
    When User enters username as "Admin" and password as "admin123"
    Then User should be able to login sucessfully and new page open
   Scenario Outline: Login with invalid credentials
    When User enters username as "<username>" and password as "<password>"
    Then User should be able to see error message "<errorMessage>"
  | username   | password  | errorMessage                      |
  | Admin      | admin12$$ | Invalid credentials               |
  | admin$$    | admin123  | Invalid credentials               |
  Scenario: Verify FaceBook Icon on Login Page
    Then User should be able to see FaceBook Icon
  Scenario: Verify LinkedIn Icon on Login Page
    Then User should be able to see LinkedIn Icon  
   Scenario: Verify ForgetPassword link on Login Page
    When User clicks on Forgot your Password Link
    Then User should navigate to a new page

Step 8 – Create file in src/test/resources

We need to create the file in the src/test/resources folder. Using a property file for reporting is quite helpful if you want to define several different properties.


Step 9 – Create a Helper class in src/main/java

We have used Page Object Model with Cucumber and TestNG. Create a Helper class where we are initializing the web driver, initializing the web driver wait, defining the timeouts, and creating a private constructor of the class, within it will declare the web driver, so whenever we create an object of this class, a new web browser is invoked. 

import java.time.Duration;
import org.openqa.selenium.WebDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class HelperClass {
	private static HelperClass helperClass;
	private static WebDriver driver;
	private static WebDriverWait wait;
    public final static int TIMEOUT = 10;
	 private HelperClass() {
	    	driver = new ChromeDriver();
	        wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));

    public static void openPage(String url) {

	public static WebDriver getDriver() {
		return driver;	
	public static void setUpDriver() {
		if (helperClass==null) {
			helperClass = new HelperClass();

	 public static void tearDown() {
		 if(driver!=null) {
		 helperClass = null;


Step 10 – Create Locator classes in src/main/java

Create a locator class for each page that contains the detail of the locators of all the web elements. Here, I’m creating 3 locator classes – LoginPageLocators, HomePageLocators, and ForgotPasswordLocators.


import org.openqa.selenium.WebElement;

public class LoginPageLocators {

	@FindBy(name = "username")
    public WebElement userName;
    @FindBy(name = "password")
    public WebElement password;
    @FindBy(id = "logInPanelHeading")
    public WebElement titleText;
    @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button")
    public WebElement login;
    @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/div/div[1]/div[1]/p")
    public  WebElement errorMessage;
    @FindBy(xpath = "//*[@href='']")
    public  WebElement linkedInIcon;
    @FindBy(xpath = "//*[@href='']") //Invalid Xpath
    public  WebElement faceBookIcon;
    @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[4]/p")
    public  WebElement ForgotYourPasswordLink;


import org.openqa.selenium.WebElement;

public class HomePageLocators {

	@FindBy(xpath = "//*[@id='app']/div[1]/div[2]/div[2]/div/div[1]/div[1]/div[1]/h5")
	public  WebElement homePageUserName;


import org.openqa.selenium.WebElement;

public class ForgotPasswordLocators {
	@FindBy(xpath = "//*[@id='app']/div[1]/div[1]/div/form/h6")
    public WebElement ForgotPasswordHeading;


Step 11 – Create Action classes in src/main/java

Create the action classes for each web page. These action classes contain all the methods needed by the step definitions. In this case, I have created 2 action classes – LoginPageActions, HomePageActions, and ForgotPasswordActions.


In this class, the very first thing will do is to create the object of the LoginPageLocators class so that we should be able to access all the PageFactory elements. Secondly, create a public constructor of LoginPageActions class.

import com.example.locators.LoginPageLocators;
import com.example.utils.HelperClass;

public class LoginPageActions {

	LoginPageLocators loginPageLocators = null; 
    public LoginPageActions() {

    	this.loginPageLocators = new LoginPageLocators();

	// Set user name in textbox
    public void setUserName(String strUserName) {
    // Set password in password textbox
    public void setPassword(String strPassword) {
    // Click on login button
    public void clickLogin() {;
    // Get the title of Login Page
    public String getLoginTitle() {
        return loginPageLocators.titleText.getText();
    // Get the title of Login Page
    public String getErrorMessage() {
        return loginPageLocators.errorMessage.getText();
    // LinkedIn Icon is displayed
    public Boolean getLinkedInIcon() {
        return loginPageLocators.linkedInIcon.isDisplayed();
    // FaceBook Icon is displayed
    public Boolean getFaceBookIcon() {
        return loginPageLocators.faceBookIcon.isDisplayed();
    // Click on Forget Your Password link
    public void clickOnForgetYourPasswordLink() {
    public void login(String strUserName, String strPassword) {
        // Fill user name
        // Fill password
        // Click Login button


import com.example.locators.HomePageLocators;
import com.example.utils.HelperClass;

public class HomePageActions {

	HomePageLocators homePageLocators = null;
	public HomePageActions() {
		this.homePageLocators = new HomePageLocators();


    // Get the User name from Home Page
    public String getHomePageText() {
        return homePageLocators.homePageUserName.getText();



import com.example.locators.ForgotPasswordLocators;
import com.example.utils.HelperClass;

public class ForgotPasswordActions {
	ForgotPasswordLocators forgotPasswordLocators = null;
	public ForgotPasswordActions() {
		this.forgotPasswordLocators = new ForgotPasswordLocators();


    // Get the Heading of Forgot Password page
    public String getForgotPasswordPageText() {
        return forgotPasswordLocators.ForgotPasswordHeading.getText();

Step 12 – Create a Step Definition file in src/test/java

Create the corresponding Step Definition file of the feature file.


import org.junit.jupiter.api.Assertions;
import com.example.actions.ForgotPasswordActions;
import com.example.actions.HomePageActions;
import com.example.actions.LoginPageActions;
import com.example.utils.HelperClass;

public class LoginPageDefinitions{	

	LoginPageActions objLogin = new LoginPageActions();
    HomePageActions objHomePage = new HomePageActions();
    ForgotPasswordActions objForgotPasswordPage = new ForgotPasswordActions();
    @Given("User is on HRMLogin page {string}")
    public void loginTest(String url) {
    @When("User enters username as {string} and password as {string}")
    public void goToHomePage(String userName, String passWord) {
        // login to application
        objLogin.login(userName, passWord);
        // go the next page
    @When("User clicks on Forgot your Password Link")
    public void goToForgotYourPasswordPage() {
    @Then("User should be able to login sucessfully and new page open")
    public void verifyLogin() {
        // Verify home page
        Assertions.assertTrue(objHomePage.getHomePageText().contains("Employee Information"));
    @Then("User should be able to see error message {string}")
    public void verifyErrorMessage(String expectedErrorMessage) {
        // Verify home page
    @Then("User should be able to see LinkedIn Icon")
    public void verifyLinkedInIcon( ) {
    @Then("User should be able to see FaceBook Icon")
    public void verifyFaceBookIcon( ) {
    @Then("User should navigate to a new page")
    public void verfiyForgetYourPasswordPage() {
   	Assertions.assertEquals(objForgotPasswordPage.getForgotPasswordPageText(), "Reset Password");

Step 13 – Create Hook class in src/test/java

Create the hook class that contains the Before and After hook to initialize the web browser and close the web browser.

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import com.example.utils.HelperClass;

public class Hooks {
    public static void setUp() {


	public static void tearDown(Scenario scenario) {

		//validate if scenario has failed
		if(scenario.isFailed()) {
			final byte[] screenshot = ((TakesScreenshot) HelperClass.getDriver()).getScreenshotAs(OutputType.BYTES);
			scenario.attach(screenshot, "image/png", scenario.getName()); 

Step 14 – Create a Cucumber Test Runner class in src/test/java

Cucumber needs a TestRunner class to run the feature files. It is suggested to create a folder with the name of the runner in the src/test/java directory and create the Cucumber TestRunner class in this folder. Below is the code of the Cucumber TestRunner class.

import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example")
public class CucumberRunnerTests  {

Step 15 – Run the tests from Maven or Command Line

Use the below command to run the tests.

mvn clean test -Dcucumber.features="src/test/resources/features"

Step 16 – Cucumber Report Generation

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

Congratulations!! We have built the framework using Cucumber 7 with JUnit5.


12 thoughts on “Integration of Cucumber7 with Selenium and JUnit5

  1. @SelectClasspathResource(“com.example”) should be
    @SelectClasspathResource(“com/example”) in order to work when run as a JUnit test.


  2. …Thank You for Sharing the article. Implemented a test using this reference. However as I read Junit5+Cuc7 we can have scenarios executed in parallel for a feature file. My feature file has 2 scenarios
    Executed via ‘mvn clean test -Dcucumber.features=”src/test/resources/features”
    But I dont see scenarios executed in parallel.
    Can you please advise on same.


    1. Hi Vaibhav, I appreciate you found this article useful. I’m in the process of writing another article on parallel execution of cucumber with JUnit5. It will be out soon. I’ll let you know when it will be out.


      1. Hi Vaibhav, you can try below command
        mvn clean test -Dcucumber.filter.tags=”@ValidCredentials”
        I think you are missing @, the tests won’t work.


      2. Hello any update on parallelism as I am facing a issue. What happens is lets say I have 20 scenarios and I have cucumber.execution.parallel.config.fixed.parallelism=2 and if due to some reason a test case fails then that thread does not get quit and now as a result of this all scenarios henceforth will be marked as failed even though in UI I can see that scenarios are executed (non headless mode). Is there any way we can make reporting independent of the thread failed or kill that thread after some time and report gets created as expected. Thanks Vaibhav


  3. Hi, Vaibhav

    Thank you very much for sharing your knowledge, I will use it as a basis for my studies,
    Friend, doubt, I’m getting the error below when I run the project, am I doing something wrong?

    ‘TestEngine with ID ‘cucumber’ failed to discover tests’


    1. Hi, are you trying to run the tests through command line or from junit runner? The tests should run through command line. If you will try to run the tests through junit runner in case of junit5, you will see the error that testengine failed to discover the tests


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 )

Facebook photo

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

Connecting to %s