HOOKS(Tagged)

Cucumber hooks are powerful features that allow you to execute blocks of code before or after scenarios (or even steps). 

"Tagged hooks" take this a step further by letting you specify that a particular hook should only run for scenarios that have a specific tag (or combination of tags). 

This is incredibly useful for managing setup and teardown logic that isn't universally applicable to all your tests.

In Cucumber, hooks like @Before and @After can be:

  • Global Hooks – execute before/after every scenario
  • Tagged Hooks – execute only for scenarios with a specific tag (e.g., @Before("@setcookies"))

  • @Before: Runs before each scenario.
  • @After: Runs after each scenario (even if the scenario fails).
  • @BeforeStep: Runs before each individual step within a scenario.
  • @AfterStep: Runs after each individual step within a scenario.

These hooks are typically used for:

  • Setup: Initializing web drivers, setting up database connections, preparing test data, navigating to a default page, setting browser cookies.

  • Teardown: Quitting web drivers, closing database connections, clearing test data/cookies, logging out of an application, taking screenshots for failed/passed scenarios, generating reports.

Why use Tagged Hooks?

By default, @Before and @After hooks run for every scenario. However, in many real-world test suites, different scenarios have different prerequisites or cleanup requirements. This is where tagged hooks become essential.

Imagine you have:

  • Scenarios that require a database connection (@database_test).

  • Scenarios that involve UI automation (@ui_test).

  • Scenarios that need specific user roles (@admin_user, @guest_user).

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

Feature: Tagged Hooks Checking


@ui_test

Scenario: User logs in successfully

Given I am on the login page

When I enter valid credentials

Then I should be logged in


@api_test

Scenario: Create a new user via API

Given a user creation payload

When I send a POST request to the /users endpoint

Then the user should be created successfully

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

package Steps;

import io.cucumber.java.After;

import io.cucumber.java.Before;

import io.cucumber.java.Scenario; // Useful for getting scenario information


public class Hooks {


// This hook runs before any scenario tagged with @ui_test

@Before("@ui_test")

public void setupBrowser() {

System.out.println("Setting up browser for UI test...");

// Initialize WebDriver (e.g., ChromeDriver, FirefoxDriver)

}


// This hook runs after any scenario tagged with @ui_test

@After("@ui_test")

public void tearDownBrowser(Scenario scenario) {

System.out.println("Tearing down browser after UI test...");

// Close WebDriver

if (scenario.isFailed()) {

// Take a screenshot if the UI test failed

System.out.println("Taking screenshot for failed UI test: " + scenario.getName());

}

}


// This hook runs before any scenario tagged with @api_test

@Before("@api_test")

public void setupApiEnvironment() {

System.out.println("Setting up API environment...");

// e.g., Set base URI, configure HTTP client

}


// This hook runs after any scenario tagged with @api_test

@After("@api_test")

public void cleanupApiData() {

System.out.println("Cleaning up API data...");

// e.g., Delete created resources

}

}


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

package testRunner;



import org.junit.runner.RunWith;


import io.cucumber.junit.Cucumber;

import io.cucumber.junit.CucumberOptions;



@RunWith(Cucumber.class)

@CucumberOptions(

features = {"src/test/java/Features"},

glue = {"Steps"},

tags = "@ui_test or @api_test",

monochrome = true

)

public class ParameterRunner {

}


Setting up browser for UI test...

Tearing down browser after UI test...

Setting up API environment...

Cleaning up API data...

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