Hooks Example 2

Feature File

language-gherkin
Feature: Application Login Background: When launch the browser from config variables And hit the homepage URL of the banking site @RegressionTest @Netbanking Scenario: User Page default Login with Reusability Given user netbanking login When user login with netbanking URL with "NormalUser1" and password "Pswd123953" Then Netbank homepage is displayed And users cards are displayed @Smoketest @RegressionTest @Mortage Scenario Outline: Mortgage user default login Given Mortgage user is landing in netbanking login page When Mortgage user is login with "<Username>" and password "<Password>" combination Then MortgageUser homepage is showing And MortgageUser cards are displayed in homepage Examples: | Username | Password | |---------------|------------| | MortgageUser1 | MRUser123 | | MortgageUser2 | MRUser456 |

Step Definitions

language-java
package stepDefinitions; import io.cucumber.java.After; import io.cucumber.java.Before; public class hookSteps { @Before("@Netbanking") public void netBankingSetup() { System.out.println("Hook: setup the entries in Netbanking database"); } @After public void teardown() { System.out.println("Hook: clear the entries "); System.out.println("************************************************"); } @Before("@Mortage") public void mortageSetup() { System.out.println("Hook: setup the entries in Mortage database"); } }

Main Steps

language-java
package stepDefinitions; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; public class MainSteps { @When("launch the browser from config variables") public void launch_the_browser_from_config_variables() { System.out.println("Browser launched successfully"); } @When("hit the homepage URL of banking site") public void hit_the_homepage_url_of_banking_site() { System.out.println("Hitting the banking site URL successfully"); } @Given("user netbanking login") public void user_netbanking_login() { System.out.println("User landed on netbanking page with reusability"); } @When("user login with netbanking URL with {string} and password {string}") public void user_login_with_netbanking_URL(String username, String password) { System.out.println("Username is ---> " + username + " & password is ---> " + password); } @Then("Netbank homepage is displayed") public void Netbank_homepage_displayed() { System.out.println("Home page is displayed"); } @Then("users cards are displayed") public void users_cards_are_displayed() { System.out.println("Cards are displayed"); } @Given("Mortgage user is landing in netbanking login page") public void Mortgageuserislandingnetbankingpage() { System.out.println("Mortgage User landed on netbanking page"); } @When("Mortgage user is login with {string} and password {string} combination") public void Mortgageuser_is_login_with_and_password_combination(String username, String password) { System.out.println("User login with username ---> " + username + " & password is ---> " + password); } @Then("MortgageUser homepage is showing") public void MortgageUserhomepageisshowing() { System.out.println("Home page is showing to the users"); } @Then("MortgageUser cards are displayed in homepage") public void mortgage_user_cards_are_displayed_in_homepage() { System.out.println("Cards are displayed to mortgage user"); } }

Test Runner

language-java
Explain
package cucumberOptions; import io.cucumber.testng.AbstractTestNGCucumberTests; import io.cucumber.testng.CucumberOptions; @CucumberOptions(features="src/test/java/features", glue ="stepDefinitions", tags= "@Smoketest or @RegressionTest", monochrome=true) public class TestNGRunner extends AbstractTestNGCucumberTests

} 


Output:


Hook: setup the entries in Netbanking database

broswer launched successfully

Hitting the banking site URL successfully

User landed on netbanking page with resuablity

Username is ---> NormalUser1 & password is ---> Pswd123953

Home page is displayed

Cards are displayed

Hook: clear the entries

************************************************

Hook: setup the entries in Mortage database

broswer launched successfully

Hitting the banking site URL successfully

Mortage User landed on netbanking page

user login with username ---> MortageUser1 & password is ---> MRUser123

Home page is showing to the users

Cards are displayed to mortage user

Hook: clear the entries

************************************************

Hook: setup the entries in Mortage database

broswer launched successfully

Hitting the banking site URL successfully

Mortage User landed on netbanking page

user login with username ---> MortageUser2 & password is ---> MRUser456

Home page is showing to the users

Cards are displayed to mortage user

Hook: clear the entries

************************************************

PASSED: io.cucumber.testng.AbstractTestNGCucumberTests.runScenario("Mortage user default login", "Application Login")

Runs Cucumber Scenarios

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

Runs Cucumber Scenarios

PASSED: io.cucumber.testng.AbstractTestNGCucumberTests.runScenario("Mortage user default login", "Application Login")

Runs Cucumber Scenarios


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

Default test

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

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



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

Default suite

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

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






Order of Execution in Your Setup

  1. @Background → runs before each scenario

  2. @Before Hooks (tagged) → run before specific tagged scenarios

  3. @After Hook → runs after every scenario

  4. Scenario and Scenario Outline

  5. Tags to control execution from TestNGRunner

🔄 Test Flow – Execution Steps

tags = "@Smoketest or @RegressionTest"

Which means both scenarios will be executed

▶️ Execution Order – Step by Step

1️⃣ First Scenario: @RegressionTest @Netbanking


Scenario: User Page default Login with Reusability


Execution Flow:


1. Hook: @Before("@Netbanking") → Netbanking DB Setup 2. Background Step 1Launch browser from config 3. Background Step 2Hit banking site URL 4. Scenario Step: Given user netbanking login 5. Scenario Step: When user login with netbanking URL with "NormalUser1" and password "Pswd123953" 6. Scenario Step: Then Netbank homepage is displayed 7. Scenario Step: And users cards are displayed 8. Hook: @After → clear entries


2️⃣ Second Scenario Outline: @Smoketest @RegressionTest @Mortage


Scenario Outline: Mortage user default login

Executed twice (once per Examples row):


Iteration 1 – MortageUser1 / MRUser123


1. Hook: @Before("@Mortage") → Mortage DB Setup 2. Background Step 1Launch browser from config 3. Background Step 2Hit banking site URL 4. Scenario Step: Given Mortage user is landing in netbanking login page 5. Scenario Step: When Mortage user logs in with "MortageUser1" and password "MRUser123" 6. Scenario Step: Then homepage is shown 7. Scenario Step: And cards displayed 8. Hook: @After → clear entries

Iteration 2 – MortageUser2 / MRUser456

Same flow with different data