Reusability Example

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


#Reusable


Scenario: User Page default Login

Given user is on NetBanking Landing Page

When user login into application

Then homepage is displayed

And cards are displayed



#Difference will be providing when credeintials between Admin & User...

#Remaining Homepage & Cards are displayed same for both Admin & User


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


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");

}

}


#We can use for Reusable same stepDefinitions

#Output will be

User landed on netbanking page

User login into the application

Home page is displayed

Cards are displayed

User landed on netbanking page

User login into the application

Home page is displayed

Cards are displayed


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

but if you changed some scenario in feature.java file like


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


#Reusable


Scenario: User Page default Login

Given user is on NetBanking Landing Page

When user login into application

Then homepage is Populated <<---------------------------

And cards are displayed


#Now it cannot reusable.becuase in stepDefinitions file we mentioned we are not following standard reusable


@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");

}

#so it can be consider as new method


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


#we have to give Admin login & User login in firstprogram.feature file like


Feature: Application Login


Scenario: Admin Page default Login

Given user is on NetBanking Landing Page

When user login into application with "admin" and password "1234"

Then homepage is displayed

And cards are displayed


#Reusable


Scenario: User Page default Login

Given user is on NetBanking Landing Page

When user login into application with "user" and password "0953"

Then homepage is displayed

And cards are displayed


# If you run testNGRunner.java file you will get error like


io.cucumber.testng.UndefinedStepException: The step 'user is on NetBanking Landing Page' and 1 other step(s) are undefined.

You can implement these steps using the snippet(s) below:


@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

throw new io.cucumber.java.PendingException();

}

@When("user login into application with {string} and password {string}")

public void user_login_into_application_with_and_password(String string, String string2) {

// Write code here that turns the phrase above into concrete actions

throw new io.cucumber.java.PendingException();

}



==================================FINAL CODE===========================================


  • Feature Files: These files contain scenarios written in Gherkin syntax, which describe the behavior of the application.
  • Step Definitions: These are Java methods that implement the steps defined in the feature files.
  • Test Runner: This is a class that executes the feature files and reports the results.

#Update Mainsteps.java file (StepDefiniation file)

#Re-Run testNGRunner.java file


firstProgram.feature file


Feature: Application Login


Scenario: Admin Page default Login

Given user is on NetBanking Landing Page

When user login into application with "admin" and password "1234"

Then homepage is displayed

And cards are displayed


#Reusable


Scenario: User Page default Login

Given user is on NetBanking Landing Page

When user login into application with "user" and password "0953"

Then homepage is displayed

And cards are displayed


MainSteps.java file

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() {

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

}

@When("user login into application with {string} and password {string}")

public void user_login_into_application_with_and_password(String username, String password) {

System.out.println(username + " and password in " +password );

}

/*

below values fetch from fristprogram.feature file and will store in (String username, String password)

username = admin, password = 1234

username = user, password = 0953


we can use like below

When user login into application with "admin" and password "1234"

user login into application with {string} and password {string}

we can not use as like below

When user login into application with "admin" and password "1234"

user login into application with {string} and an password {string}

*/

@Then("homepage is displayed")

public void homepage_is_displayed() {

System.out.println("Home page is displayed");

}

@Then("cards are displayed")

public void cards_are_displayed() {

System.out.println("Cards are displayed");

}

}



testNGRunner.java file



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 {


}


Output:


User landed on netbanking page

admin and password in 1234

Home page is displayed

Cards are displayed

User landed on netbanking page

user and password in 0953

Home page is displayed

Cards are displayed

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

Runs Cucumber Scenarios

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

Runs Cucumber Scenarios

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

Default test

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

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

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

Default suite

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

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