Cucumber with TestNG

Testing leads to failure and failure leads to understanding.”  Burt Rutan

If you are interested in automation testing practice and you are a beginner in this area of development, then this article will help you a lot. Below you could find some interesting things about a friendly feature called Cucumber, which is simple to learn and understand. We will learn about this interesting tool and its features and then we will start configuring a simple automation framework using TestNG, Selenium, and Maven.

            The article is written as a tutorial and it doesn’t contain all the resources needed in order to fully set up the project, because only the important steps are explained, but you can download the framework from the Git repository provided in the end. The framework is built to run both on MAC OSX systems and Windows.


1. Cucumber introduction

2. What is TestNG?

3. Selenium introduction

4. Maven overview

5. Prerequisites

6. Implementation steps

7. Conclusion

8. References

1. Cucumber introduction

Cucumber is a testing tool which helps you to a better organization of the automated tests and it is used especially in Behavior Driven Development (software engineering process which derives from Test Driven Development).

In the below schema we could see a schema which helps us for a better understanding of a Cucumber based project.



BDD is an elegant way of expressing the requirements of a software product, but it is not an independent software methodology so it will need to be used together with other methodologies, such as would be Agile or Test Driven Development. Also, the presented technology offers a more organized way of working.



In order to you run some tests using Cucumber, we need to create some files called features and to write within them at least one test scenario. Each scenario needs to have some implemented methods and then it will be executed.

The feature files are written using Gherkin syntax. The below schema will help you for a better understanding of how steps will look like.


2. What is TestNG?

TestNG is a testing framework used together with Java language. It comes with multiple features as annotations, instances support, test distribution and easy XML files configuration. It also could generate test reports in HTML or XML file formats with the help of some plugins.

Using TestNG as your default testing environment has a lot of advantages like flexible test configuration, support for parameterized tests, it is supported by a large variety of plugins and tools (IntelliJ IDEA, Eclipse), clear test suite organization. Minimum java version to use together with TestNG is JDK 8.



The following method will run in different contexts:

@BeforeSuite: before all tests from the suite.

@AfterSuite: after all tests from the suite.

@BeforeTest: before any test method inside the class.

@AfterTest: after any test method inside the class.

@BeforeClass: before the first test method from the current class.

@AfterClass: after all the test methods from the current class.

@BeforeMethod: before each method.

@AfterMethod: after each method.

@Test: The way to define a method as a test.

3. Selenium introduction

Selenium is a testing framework used for web applications and it provides the best way to automate web tests. It provides cross-browser support and there are periodical releases with improvements and fixes for selenium drivers.

4. Maven – generalities

Maven is a software tool that works with the help of various plugins and is used to build Java applications; Apache Maven’s specific plug-ins would be testing plugins, Web server plugins, generating project files (HTML, XML, JSON). All of this will be defined in the pom.xml directory, in the <plugins> section. To learn more about this head over to Apache Maven website.


5. Prerequisites

  1. Make sure you have Java (minimum JDK 8) installed and configured in Environment variables.
  2. Make sure you have Apache Maven installed and configured in Environment variables.
  3. You can either choose between IntelliJ IDEA and Eclipse and install one of them. Download any of chromedriver.exe, geckodriver.exe and IEDriverServer.exe create a drivers directory in the project root and copy them there.

6. Implementation steps

6.1  Create a new maven project and add the below code (dependencies and plugins) to the pom.xml file. Choose ‘Enable Auto-Import’ Maven projects prompt at the beginning (to manually enable this, go to File – Settings – Maven – Importing – enable ‘Import Maven projects automatically’).


<properties>…</properties>: a section where it’s easier to set the versions of the dependencies.


<dependency>…</dependency>: dependencies needed in order to set-up the project.


  1. <resource>: path to the test steps (src/test/resources).
  2. maven-surefire-plugin: plugin used to generate HTML reports.
  3. <suiteXmlFiles>: path to the file where we define the test suite.
  4. <systemPropertyVariables>: specify the browser which will be transmitted as a parameter in the Hooks class


6.2 Add Cucumber for Java IntelliJ IDEA plugin. Go to File – Settings – Plugins search ‘Cucumber for Java’ – click ‘Search in repositories’ – click Install.


6.3 Create project structure as below.

  • pageobjects – this package will contain the page objects;
  • runner – a package where we will define the Runner class;
  • steps – package which will contain the test methods;
  • util – package which will contain the classes with base methods from the framework;
  • resources/features – file where we will write the test steps using Gherkin;


  • test
    • proiect
      • pageobjects
      • runner
      • steps
      • util
    • resources


6.4  Create Runner class in the runner package and add the below code lines. In this file, we provide the test runner, where we will specify the classpath of the project and also the HTML report target file and output (cucumber-html-report).


6.5 Create TestSuite.xml in the project root and add the below code. In this file, we provide the location of the test runner.


6.6  Create a file in the project root by right-clicking on the project name, click New and select Resource Bundle. Here you need to provide to the runner a browser on which to navigate. Code support is available for the following browsers: Google Chrome, Mozilla Firefox and Internet Explorer.


6.7 Create a Hooks class in the steps package. In this class, we will check the operating system, because the Selenium WebDriver which runs on Windows has different capabilities than the one from OSX and with the help of the method from OSDetector class, we will identify the operating system.


Also, with the method provided below we will specify the detected operating system.

@Before(order = 1)

public void storeOs() {

CommonSteps.setDetectedOs(getOs().toString());“Test cases will be executed on the detected {} operating system.”, CommonSteps.getDetectedOs());


6.8       In the OSDetector class we will detect the operating system with the method getProperty and then in the other method we will set the chrome driver specific to each os.


6.9 The project is built using a layered architecture based on ‘Page Object’ design pattern. The pageobjects package is designed in order to store and model the objects identified on the Web page screens.


6.10  The next step is to define the steps that we will use in order to build our scenario.


6.11 Now it’s time to write the scenario in the feature file. In the following lines, you will see an example of a test which is already implemented in the framework.

Feature: My feature

Scenario: Log in
Given I navigate on the page
Then Application is displayed
When I choose to sign in
Then the “Authentication” page is displayed
When I enter the “” “email” for Log in
When I enter the “Test1234!” “password” for Log in
When I click on the Submit button to Log in
Then the “My Account” page is displayed

7. Conclusion

Cucumber is a useful tool for automation testing and provides an easy way to organize tests and test scenarios. Also, TestNG provides a lot of methods that facilitate things like clicking buttons, filling forms, navigating through Web pages, HTML reports, clear code.

If you want to start writing automated tests, then TestNG and Cucumber are definitely a good choice.

8. References

1. The Cucumber for Java Book, Behaviour-Driven Development for Testers and Developers by Seb Rose, Matt Wynne & Aslak Hellesøy


3. Framework repository



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 )

Google photo

You are commenting using your Google 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.