Hooks

  • Similar to Background keyword. But Hooks have more advantages than Background keyword.
  • Hooks in Cucumber are special blocks of code that run before or after each scenario. They are defined in the step definition classes and help in managing test setup and teardown.

Hooks are annotated with:
  • @Before → runs before each scenario
  • @After → runs after each scenario

Order:
  1. Before Hooks will execute
  2. Check any background and will execute
  3. then scenario will execute
  4. After hooks will execute

✅ Tagged Hooks (Conditional Hooks)

Run a hook only for scenarios with a specific tag:


@Before("@Smoke") public void setup Smoke() { System.out.println("Setup for Smoke Tests"); } @After("@Regression") public void cleanUpRegression() { System.out.println("Teardown for Regression Tests"); }


✅ Order in Hooks

You can define multiple hooks and control their execution using the order attribute:

@Before(order = 1) public void openBrowser() { System.out.println("Opening browser"); } @Before(order = 2) public void login() { System.out.println("Login to application"); }
  • Hooks with lower order value run first for @Before
  • For @After, lower order runs last

***************************** Example 1 ********************************************
Code Structure:

Feature File: login.feature
Step Definitions: LoginSteps.java
Hooks Class: Hooks.java
TestNG Runner: TestNGRunner.java

1. Feature File – login.feature 📁 src/test/java/features/login.feature Feature: Application Login @Smoke Scenario: Login with valid credentials Given user is on login page When user logs in with "admin" and "1234" Then homepage is displayed

//This feature file outlines a single scenario where a user logs in with valid credentials.

//The @Smoke tag indicates that this is a smoke test, which is a preliminary test to check the basic functionality.

✅ 2. Step Definitions – LoginSteps.java 📁 src/test/java/stepDefinitions/LoginSteps.java package stepDefinitions; import io.cucumber.java.en.*;

public class LoginSteps { @Given("user is on login page") public void user_is_on_login_page() { System.out.println("Step: User is on login page"); } @When("user logs in with {string} and {string}") public void user_logs_in_with_credentials(String username, String password) { System.out.println("Step: Logging in with Username: " + username + " | Password: " + password); } @Then("homepage is displayed") public void homepage_is_displayed() { System.out.println("Step: Homepage is displayed"); } } ✅ 3. Hooks Class – Hooks.java 📁 src/test/java/stepDefinitions/Hooks.java package stepDefinitions; import io.cucumber.java.Before; import io.cucumber.java.After; public class Hooks { @Before public void setUp() { System.out.println("Hook: Launching Browser..."); } @After public void teardown() { System.out.println("Hook: Closing Browser..."); } }

//The Hooks class contains methods that run before and after each scenario.

//The setup method is where you would typically initialize your WebDriver, while the teardown method handles cleanup.

✅ 4. TestNG Runner – TestNGRunner.java 📁 src/test/java/cucumberOptions/TestNGRunner.java package cucumberOptions; import io.cucumber.testng.AbstractTestNGCucumberTests; import io.cucumber.testng.CucumberOptions; @CucumberOptions( features = "src/test/java/features", glue = "stepDefinitions", monochrome = true, plugin = { "pretty", "html: target/cucumber-report.html" }, tags = "@Smoke" ) public class TestNGRunner extends AbstractTestNGCucumberTests { } ✅ Console Output (Expected) Hook: Launching Browser... Step: User is on login page Step: Logging in with Username: admin | Password: 1234 Step: Homepage is displayed Hook: Closing Browser...

***************************** Example 2 ********************************************