A Java Project With Cucumber 6, TestNG and Maven.

Balaji Ganesh
The Startup
Published in
6 min readJul 21, 2020

What you’ll learn?

To set up a simple java project with Cucumber 6, TestNG and Maven.

Prerequisite :

  1. Intellij IDEA, install a pluging called “Cucumber for Java”.
  2. JDK.

Cucumber :

Cucumber is a framework available to write and test the application by using the high-level description of the software. It is a tool which is used frequently in projects which use Agile processes. It bridges the gap of communication across teams, reduced the workload of documentation and provides us with a common language which every stakeholder speaks and understands. Rather one of the main benefits of cucumber is that it allows a common collaborative document in which the application and end user behavior are captured. This document can be used by any team member and it will provide all the relevant information.

Components

The cucumber framework has three components :

  • Features — to define application behaviour in plain meaningful English text using a simple grammar defined by a domain-specific language (DSL)
  • Step Definitions — A Step Definition is a Java method with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute.
  • Test Runner — to automate and run the behaviour tests– e.g. TestNG (Java), jUnit (Java)

Setting up the Project

1. Creating a Maven Project

  1. Open your Intellij IDEA.
  2. Click on File -> New -> Project.
  3. Uncheck “Create from archetype”, If you prefer to use an archetype you can use maven-archetype-quickstart.
  4. Provide a name for your project and click “Next”.
  5. Your project structure will look like below :
├── pom.xml
└── src
├── main
├── java
└── resources
└── test
└── java

Now, you can delete the src/main/java directories and add a resources directory under test, under the resources directory create a new directory called as features and add a new package called StepDefs under src/test/java . Your new structure will look like :

├── pom.xml
└── src
└── test
├── java
└── StepDefs
└── resources
└── features

Now, that are folder structer is ready, lets look at the packages we need to setup a Cucumber Framework with TestNG.

Maven Dependencies :

  1. cucumber-java — In this project we will be using dependancies from “io.cucumber” and not “info.cukes”. io.cucumber and info.cukes are Maven group ids. info.cukes was for Cucumber version till 1.2.5. The latest version are in io.cucumber starting from 2.0.0.
  2. cucumber-testng
  3. selenium-java
  4. webdrivermanager — If you use Selenium WebDriver, you probably know that to use some browsers such as Chrome, Firefox, Edge, Opera, PhantomJS, or Internet Explorer, first you need to download the so-called driver, i.e. a binary file which allows WebDriver to handle browsers. In Java, the path to this driver must be set as JVM properties. This is quite annoying since it forces you to link directly this driver into your source code. In addition, you have to check manually when new versions of the binaries are released. WebDriverManager comes to the rescue, performing in an automated way all this dirty job for you.

Maven Plugins :

  1. maven-compiler-plugin — Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javac can accept such command using “-source” and “-target”. The Compiler Plugin will help you acheive this by providing the details in you POM.xml.
  2. maven-surefire-plugin — This is used to trigger your tests and also provides a few reports.

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>CucumberTests</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/Runner.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
</project>

Now that you have all the dependacies added to your POM.xml, IDEA will automatically try to install these dependancies for you. If you want to run it manually, you can do it as below :

2. Writing you first Feature File :

A Feature File is an entry point to the Cucumber tests. This is a file where you will describe your tests in Descriptive language (Like English). It is an essential part of Cucumber, as it serves as an automation test script as well as live documents. A feature file can contain a scenario or can contain many scenarios in a single feature file but it usually contains a list of scenarios.

Lets now start by creating it Right click on the src/test/resources/features directory and New -> File -> FirstFeature.feature , make sure you provide the extension for the feature file.

Keywords :

Feature, Backgournd, Scenario, Given, When, And and Then — These are keywords defined by Gherkin. Gherkin has few more keywords and I will make a new blog on those :). But to start off we can quickly explain some of the keywords in one line :

  • Feature − Name of the feature under test.
  • Description (optional) − Describe about feature under test.
  • Background − Used to define a step or series of steps that are common to all the tests in the feature file
  • Scenario − What is the test scenario.
  • Given − Prerequisite before the test steps get executed.
  • When − Specific condition which should match in order to execute the next step.
  • Then − What should happen if the condition mentioned in WHEN is satisfied.

FirstFeature.feature

Feature: Google Search

Background: To Launch the browser
Given Launch the browser

Scenario: Search for Cucumber in Google
When Hit Google on your browser
Then Enter "Cucumber" in the search text box.
And Select the first result.

You can notice that in the contents in the feature file are higlighted. This is because there are no steps defined in the project for those. This is where our IDEA will start helping us to write the StepDefinitions.

Place you cursor at the end of “Given Launch the browser” and press OPTION + ENTER in Mac or ALT + ENTER in Windows. You will get two options now, I recommend you to use Create step definition over Create all step definitions . Now, you’ll see a pop-up where you have to provide the filename and here my filename is FirstStepDef

FileType I am using here is Java but if you are comfortable/wish to use lambda then you can choose Java 8. The last step is to provide the path to your StepDefs package.

Also, ensure that the step definition file is created under the right directory. If not move it to the correct destination. Now you can start writing your selenium code directly here or use Page Object Model to execute your tests on your browser as show below :

3. TestRunner

With a cucumber-based framework, you cannot run a feature file on its own. You will need to create a java class, which in turn will run this cucumber feature file. We call this class as TestRunner. In addition to running a cucumber feature file, the test runner class also acts as an interlink between feature files and step definition classes. It is in test runner class, that you provide the path for both feature file and step defs.

Create a new package under src/test/java called as TestRunner and add the following code to it, incase you have you feature file/step definitions in a different folder then update the path here accordingly :

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;

@CucumberOptions(
features = {"src/test/resources/features"},
glue = {"StepDefs"},
tags = ""
)
public class TestRunner extends AbstractTestNGCucumberTests {

@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}

You can also notice that we have used @DataProvider(parallel = true) this will run your tests in parallel. Maximum threads it can start at once is 10. For more infomation on parallel execution you can refer here.

Note : We are extending “AbstractTestNGCucumberTests” which makes this a testNG class and you can use all the testNG annotations here.

You can now run this class directly or use a TestNG XML (trigger your test runner class) to start your test execution.

--

--