Cucumber with TestNG & Selenium (Part-2) – Create Tests


In this post, we will discuss about how to create tests using Cucumber with TestNG & Selenium. Creating Feature file, Step Definition class, Test Runner class and executing the test case using TestNG.


  • Create a maven project and add the below dependencies for Cucumber, TestNG and Selenium.
<!-- -->
<!-- -->
<!-- -->
<!-- -->

Install Cucumber-Eclipse plugin

  • Install Cucumber-Eclipse plugin from here.

Create a feature file

  • In your project, locate the package, right click and navigate to New –> File

  • Now In new file window, enter the filename.feature and click on Finish button.

  • After clicking on Finish button, you can observe the test.feature file was created and automatically it was displayed with Cucumber symbol and the basic features with comments were pre-loaded to that feature file. This happens because of Cucumber-Eclipse plugin.

  • Add the actual features in test.features file.

Create Step Definitions

  • For creating step definitions easily, you should install Tidy Gherkin plugin in chrome.
  • After installing Tidy Gherkin, launch the Tidy Gherkin plugin.

  • Now, copy your feature set and paste at “Enter your Gherkin here…

  • Now, click on ‘JAVA STEPS‘ tab and observe that the basic step definition methods were created.

  • Now, copy the highlighted part and create the step definition class file and paste the same. And write the Selenium code to launch the browser, authenticating the user and validating the Home screen.
public class TestStepDefinition {
  WebDriver driver;
  @Given("I want to launch the facebook in firefox browser")
  public void i_want_to_launch_the_facebook_in_firefox_browser() {
    System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\Drivers\\geckodriver.exe");
    driver = new FirefoxDriver();

  @When("I want to authenticate with valid credentails")
  public void i_want_to_authenticate_with_valid_credentails() {

  @Then("I want to verify whether user navigated to home page or not")
  public void i_want_to_verify_whether_user_navigated_to_home_page_or_not() {
      Assert.assertEquals(driver.getTitle(), "Home");

Create Test Runner Class

  • Now, create the TestRunner class file with the below code.

Note: If we are using TestNG, then we need to extend the TestRunner class to AbstractTestNGCucumberTests.

    features="<Path of the Feature files>",
    glue="<Path of the Step Definition files>",
    plugin= {"pretty","html:HTML-Reports"},
public class TestRunner extends AbstractTestNGCucumberTests{


We need to arrange the features, step definitions and Test Runner classes/files in proper packaging.

Execute the Tests

Now run the Test Runner class as TestNG test and should be able to run your tests using Cucumber with TestNG & Selenium.

Download code

About the course

NPN Training is counted among the top ten Selenium Training in Bangalore for providing the best Selenium training in Bangalore. Our selenium course content and syllabus is designed according to the industry requirement to accomplish everyone’s career goal.


Related Post
Cucumber with TestNG & Selenium (Part-1) ̵... Objective In this post, will discuss about What is Behavior Driven Development, Importance of Cucumber, Feature file, Step definition and Cucumber Op...


M.r Pradeep is QA expert working as a Research Analyst at NPN Training. He is having 12 years of experience in Quality Assurance with extensive of 4 years in Automation experience (Selenium, UFT, Rest Assured, Jmeter & Soap UI). He has helped the peers/students to understand and learn Automation with Selenium and API automation with Rest Assured with Hands-On approach.