Explanation of Cucumber Framework Setup & TestNGRunner without stepDefinition

 We are setting up a Cucumber-TestNG automation framework using Maven. This involves creating feature files, step definitions, and a TestNG Runner class to execute our tests.

1.Folder Structure


  • create feature file : /AutomationCucumber/src/test/java/Features/firstProgram.feature
  • create TestNG Runner File : /AutomationCucumber/src/test/java/cucumberOptions/TestNGRunner.java
  • create stepDefiniations file : /AutomationCucumber/src/test/java/stepDefinitions

2. Creating the Feature File

📌 Path: /AutomationCucumber/src/test/java/Features/firstProgram.feature

A feature file contains test scenarios written in Gherkin language (Given-When-Then syntax).

Feature: Application Login


Scenario: Admin Page default Login

Given user is on NetBanking Landing Page

When user login into application

Then homepage is displayed

And cards are displayed


3. Creating the TestNG Runner File

📌 Path: /AutomationCucumber/src/test/java/cucumberOptions/TestNGRunner.java

The TestNG Runner file integrates Cucumber with TestNG.


package cucumberOptions;


import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;


@CucumberOptions(features="src/test/java/features", glue ="stepDefinitions", monochrome=true)

public class TestNGRunner extends AbstractTestNGCucumberTests {


}


Explanation: @CucumberOptions:

  • features = "src/test/java/Features" → Specifies the location of feature files.
  • glue = "stepDefinitions" → Specifies the package where step definitions are implemented.
  • monochrome = true → Makes console output readable by removing ANSI colors.
extends AbstractTestNGCucumberTests:
  • Enables running Cucumber Scenarios using TestNG.

4. Execute TestNGRunner without stepDefinition




If you run above TestNGRunner.java file you will get below error FAILED: io.cucumber.testng.AbstractTestNGCucumberTests.runScenario("Admin Page default Login", "Application Login") Runs Cucumber Scenarios io.cucumber.testng.UndefinedStepException: The step 'user is on NetBanking Landing Page' and 3 other step(s) are undefined. You can implement these steps using the snippet(s) below:

What this means: You have defined a scenario in your .feature file:
  • Given user is on NetBanking Landing Page When user enters username and password Then home page is displayed
  • But Cucumber could not find Java methods that match these steps in your stepDefinitions package.

package stepDefinitions;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class MainSteps {
@Given("user is on NetBanking Landing Page")
public void user_is_on_net_banking_landing_page() {
// Write code here that turns the phrase above into concrete actions
System.out.println("User landed on netbanking page");
}
@When("user login into application")
public void user_login_into_application() {
// Write code here that turns the phrase above into concrete actions
System.out.println("User login into the application");
}
@Then("homepage is displayed")
public void homepage_is_displayed() {
// Write code here that turns the phrase above into concrete actions
System.out.println("Home page is displayed");
}
@Then("cards are displayed")
public void cards_are_displayed() {
// Write code here that turns the phrase above into concrete actions
System.out.println("Cards are displayed");
}
}



Again run the TestNGRunner.java file


User landed on netbanking page

User login into the application

Home page is displayed

Cards are displayed

PASSED: io.cucumber.testng.AbstractTestNGCucumberTests.runScenario("Admin Page default Login", "Application Login")

Runs Cucumber Scenarios


===============================================

Default test

Tests run: 1, Failures: 0, Skips: 0

===============================================