multiple stepDefinitions files But Only map to a particular step definition file (TAGS)

In a Cucumber framework with multiple stepDefinitions files, if you want to run scenarios that only map to a particular step definition file, you don’t run the step definition directly — instead, you filter by tags in your .feature files and use those tags in the @CucumberOptions of your TestNGRunner.java.

parameterization.feature

Feature: Application Login for different users


@DifferentUserslogin

Scenario Outline: user login into the netbanking page

Given user is landing in netbanking login page

When user is login with "<Username>" and password "<Password>" combination

Then homepage is showing

And cards are displayed in homepage


Examples:

| Username | Password |

| debituser | hello123 |

| credituser | 123credit |



parameterizationSteps.java


package stepDefinitions;


import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;


public class parameterizationSteps {

@Given("user is landing in netbanking login page")

public void userison_netbankinglandingpage() {

System.out.println("User landed on netbanking page");

}


@When("user is login with {string} and password {string} combination")

public void user_is_login_with_and_password_combination(String string1, String string2) {

System.out.println("user login with username " +string1 + " and password is " +string2);

}


@Then("homepage is showing")

public void homepage_is_displayed() {

System.out.println("Home page is showing to the users");

}


@Then("cards are displayed in homepage")

public void cards_are_displayed() {

System.out.println("all cards are displayed");

}

}


parameterizationTestNGRunner.java


package cucumberOptions;


import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;


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

public class parameterizationTestNGRunner extends AbstractTestNGCucumberTests{


}



Output:


User landed on netbanking page

user login with username debituser and password is hello123

Home page is showing to the users

all cards are displayed


User landed on netbanking page

user login with username credituser and password is 123credit

Home page is showing to the users

all cards are displayed


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

Default suite

Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

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


How It Works

  • Cucumber will scan the glue package (stepDefinitions) and match step definitions based on what's used in the tagged scenarios.
  • If a feature file has a scenario with @DifferentUserslogin, only steps from parameterizationSteps.java (if defined properly) will be triggered.