top of page

Tagged Hooks In Cucumber

​

​

What Are Tagged Hooks In Cucumber ?

​

Cucumber provides hooks that run before and after each scenario that means this is prerequisites for all scenario but what if we have different prerequisites for different scenarios ? For the purpose of its solution cucumber has provided a feature of tagged hooks.

All the tagged hooks are executed before the default hooks.

We can add multiple tags with a single hook.

​

For implementing tagged hooks all we need is :

  • Add tag with hook in step-definition :  @Before("@RunTag")

  • Add tag on scenario in feature file :​

@RunTag

  Scenario: Scenario 1 is included

    Given Run step of scenario 1

 

Let's demonstrate it with an example :

  • Step-definition :

​

package stepdefinitions;

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;

​

public class TaggedHooksExample {
   
   @Before
   public void beforeScenario() {
       System.out.println("\nThis will run before each scenario");
   }
   
   @Before("@RunTag")
   public void beforeTaggedScenario() {
       System.out.println("This will run before each tagged scenario");
   }
   
   @After("@ExecuteTag")
   public void afterTaggedScenario() {
       System.out.println("This will run after each tagged scenario");
   }
   
   @After
   public void afterScenario() {
       System.out.println("This will run after each scenario\n");
   }
   
   @Given("^Run step of scenario 1$")
   public void run_Scenario_I() throws Throwable {
       System.out.println("Runnig step of Scenario 1");
   }

    @Given("^Run step of scenario 2$")
   public void run_Scenario_II() throws Throwable {
       System.out.println("Runnig step of Scenario 2");

    }

    @Given("^Run step of scenario 3$")
   public void run_Scenario_III() throws Throwable {
       System.out.println("Runnig step of Scenario 3");

    }

    @Given("^Run step of scenario 4$")
   public void run_Scenario_IV() throws Throwable {
       System.out.println("Runnig step of Scenario 4");

    }

}

​

​

  • Feature File :

 

Feature: Tagged Hooks Example

​

  @RunTag
  Scenario: Scenario 1 is included
    Given Run step of scenario 1
   
  @ExecuteTag
  Scenario: Scenario 2 is included
    Given Run step of scenario 2
    
  @ExecuteTag
  Scenario: Scenario 3 is included
    Given Run step of scenario 3
    
  @RunTag
  Scenario:  Scenario 4 is included
    Given Run step of scenario 4

​

​

  • Test Runner Class :

 

package cucumberTest;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

​

@RunWith(Cucumber.class)
@io.cucumber.junit.CucumberOptions(
       features = "Features/featureTag.feature" 
       ,glue={"stepdefinitions"} 
       )

​

public class TestRunnerForTaggedHooks {

}

​

 

Output :

​

This will run before each scenario
This will run before each tagged scenario
Runnig step of Scenario 1
This will run after each scenario


This will run before each scenario
Runnig step of Scenario 2
This will run after each tagged scenario
This will run after each scenario


This will run before each scenario
Runnig step of Scenario 3
This will run after each tagged scenario
This will run after each scenario


This will run before each scenario
This will run before each tagged scenario
Runnig step of Scenario 4
This will run after each
scenario

​

​

​

​

​

Refer next page Execution Order Of Hooks

​

​

bottom of page