Skip to main content

Cucumber Tagged Hooks

Hooks can be conditionally selected for execution based on the tags of the scenario. To run a particular hook only for certain scenarios, you can associate a Before or After hook with a tag expression. Not just Scenario you can tag even your Hooks. @Before and @After hooks are by default associated with all the Scenarios but there might be possibility when you want to execute some piece of code only before certain Scenarios and not before all the Scenarios. 

In that case we use Tagged Hooks. Tagged Hooks are combination of Hooks and Tags. It is very useful when you want to perform some action only for specific and not for all scenarios.

Tags are added in parentheses after the Hook to transform it into a tagged hooks. Consider below Hook class and feature file

Example : Tagged Hooks in Cucumber

public class Hooks extends BaseFunctions
{
	@Before
	public void bf()
	{
		System.out.println("Before Hook");
	}
	
	@Before("@login")
	public void loginBeforeHook()
	{
		System.out.println("Tagged Before Hook");
	}
	
	@After("@login")
	public void loginAfterHook()
	{
		System.out.println("Tagged After Hook");
	}
	
	@BeforeStep
	public void bfs()
	{
		System.out.println("Before Step Hook");
	}
	
	@AfterStep
	public void afs()
	{
		System.out.println("After Step Hook");
	}
	
	@After
	public void af() throws InterruptedException
	{
		System.out.println("After Hook");
	}
}

Feature File 

@sanity
Feature: Registration, Login and MyAccount
 
  Background: Title of Background
 	  Given I am on the home page
    And I follow "Sign in"	  
 	   
  @login
  Scenario: Verify Login Functionality
    When I fill "email" with "goswami.tarun77@gmail.com"
    Then I fill "passwd" with "Test1234"  
    And I click on "Sign in"

Console Output

@sanity
Feature: Registration, Login and MyAccount

  Background: Title of Background # features/Login.feature:4
Before Hook
Tagged Before Hook
Before Step Hook
    Given I am on the home page   # com.pb.cucumbertest.stepdefinitions.StepDefinitions.i_am_on_the_homepage() in file:/Users/tarungoswami/eclipse-workspace1/cucumbertest/target/test-classes/
After Step Hook
Before Step Hook
    And I follow "Sign in"        # com.pb.cucumbertest.stepdefinitions.StepDefinitions.i_follow(String) in file:/Users/tarungoswami/eclipse-workspace1/cucumbertest/target/test-classes/
After Step Hook
Before Step Hook

  @sanity @login @smoke
  Scenario: Verify Login Functionality                   # features/Login.feature:9
    When I fill "email" with "goswami.tarun77@gmail.com" # com.pb.cucumbertest.stepdefinitions.StepDefinitions.i_fill_with(String,String) in file:/Users/tarungoswami/eclipse-workspace1/cucumbertest/target/test-classes/
After Step Hook
Before Step Hook
    Then I fill "passwd" with "Test1234"                 # com.pb.cucumbertest.stepdefinitions.StepDefinitions.i_fill_with(String,String) in file:/Users/tarungoswami/eclipse-workspace1/cucumbertest/target/test-classes/
After Step Hook
Before Step Hook
    And I click on "Sign in"                             # com.pb.cucumbertest.stepdefinitions.StepDefinitions.i_click_sign_in_button(String) in file:/Users/tarungoswami/eclipse-workspace1/cucumbertest/target/test-classes/
After Step Hook
After Hook
Tagged After Hook

1 Scenarios (1 passed)
5 Steps (5 passed)
0m0.186s

In above feature file you can observer tagged hooks are used in same way like tag. When you will execute your feature file, First it will execute @Before("@login") and after that @Before Hook and after that Background will be executed. This hook is applicable to this particular Scenario only. You can have different for another scenario or feature file.

As we use AND and OR Tags, similar way we can AND and OR the combination of Tagged Hooks. By default Before hook execute for all the Scenarios and when we specify one of the tag in Before Hook, it execute before the tagged scenario.

When Before hook needs to be executed for certain Scenario for Features but not for all Feature we can use this technique. ANDing and ORing is like a Boolean AND and OR.

Similarly you can use NOT with tags if you do not want to ignore or skip some tag.

Consider below feature file

@sanity
Feature: Registration, Login and MyAccount
 
  Background: Title of Background
 	  Given I am on the home page
    And I follow "Sign in"	  
 	   
  @login @smoke
  Scenario: Verify Login Functionality
    When I fill "email" with "goswami.tarun77@gmail.com"
    Then I fill "passwd" with "Test1234"  
    And I click on "Sign in"

AND

If you want to write specific hooks for scenario which are tagged with @login and @smoke 

@Before("@login and @smoke")
public void loginBeforeHook()
{
    System.out.println("Tagged Before Hook");
}
	
@After("@login and @smoke")
public void loginAfterHook()
{
    System.out.println("Tagged After Hook");
}

OR

If you want to write specific hooks for scenario which are tagged with @login or @smoke 

@Before("@login and @smoke")
public void loginBeforeHook()
{
    System.out.println("Tagged Before Hook");
}
	
@After("@login and @smoke")
public void loginAfterHook()
{
    System.out.println("Tagged After Hook");
}

NOT

If you want code written should not be executed for scenarios tagged with @login

@Before("~@login")
public void loginBeforeHook()
{
	System.out.println("Tagged Before Hook");
}

@After("~@login")
public void loginAfterHook()
{
    System.out.println("Tagged After Hook");
}

Combination of AND & OR, Not

This will execute @login scenario which are not tagged with @smoke

@Before("@login and not @smoke")
public void loginBeforeHook()
{
	System.out.println("Tagged Before Hook");
}
	
@After("@login and not @smoke")
public void loginAfterHook()
{
	System.out.println("Tagged After Hook");
}

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.