What are Tagged Hooks in Cucumber?
Tagged Hooks in Cucumber are special types of hooks (@Before, @After) that run only for scenarios with specific tags. This allows for fine-grained control over the setup and teardown of tests based on the tags assigned to scenarios or features.
Instead of running a hook before/after every scenario (as general hooks do), tagged hooks run only when the tag matches.
Why Use Tagged Hooks?
Run different setup/teardown for different scenario groups (like
@Smoke,@Regression, etc.)Optimize test execution time
Avoid unnecessary setup for unrelated tests
Syntax of Tagged Hooks
@Before("@Smoke") public void beforeSmokeTests() { // Code to run before scenarios tagged with @Smoke } @After("@Regression") public void afterRegressionTests() { // Code to run after scenarios tagged with @Regression }
Example Code
Feature File – login.feature
@Smoke Feature: Login Feature @Smoke Scenario: Valid Login Given User is on login page When User enters valid credentials Then User should be navigated to the homepage @Regression Scenario: Invalid Login Given User is on login page When User enters invalid credentials Then User should see an error message
Step Definition Class –
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("User navigates to login page"); } @When("User enters valid credentials") public void user_enters_valid_credentials() { System.out.println("User enters correct username and password"); } @When("User enters invalid credentials") public void user_enters_invalid_credentials() { System.out.println("User enters wrong username and password"); } @Then("User should be navigated to the homepage") public void user_should_be_navigated_to_the_homepage() { System.out.println("User is redirected to the homepage"); } @Then("User should see an error message") public void user_should_see_an_error_message() { System.out.println("Error message is displayed"); } }
Hooks Class –
Hooks.java
package stepDefinitions; import io.cucumber.java.Before; import io.cucumber.java.After; public class Hooks { @Before("@Smoke") public void setupForSmoke() { System.out.println("Setting up SMOKE environment..."); } @After("@Smoke") public void tearDownForSmoke() { System.out.println("Tearing down SMOKE environment..."); } @Before("@Regression") public void setupForRegression() { System.out.println("Setting up REGRESSION environment..."); } @After("@Regression") public void tearDownForRegression() { System.out.println("Tearing down REGRESSION environment..."); } }
Output When Running Scenarios
If you run the @Smoke scenario, output will be:
Setting up SMOKE environment... User navigates to login page User enters correct username and password User is redirected to the homepage Tearing down SMOKE environment...
If you run the @Regression scenario, output will be:
Setting up REGRESSION environment... User navigates to login page User enters wrong username and password Error message is displayed Tearing down REGRESSION environment...