Skip to main content

Execution Order of Hooks Cucumber

Cucumber execute Hooks in certain order but there is a way to change the order of the execution according to the need of the test. For this it require an extra parameter. This extra parameter decides the order of execution of the certain hook.

You can specify positive integer value starting from 0. For example @Before, and if you want to specify the order it will become @Before(value = 0).

Although Hooks are run in the order they are written, the order parameter can be used to define custom execution. The default value is 10000, and Cucumber runs @Before Hooks from low to high. 

A @Before Hook with an order of 1 will run before one with an order of 2. An @After Hook runs from high to low—so an @After Hook with an order of 2 will run before one with an order of 1.

Order is not limited to Default hooks. Order is applicable to all types of hooks whether it is Default, Tagged or Step Hooks. Difference is Steps hooks is associated with Steps and Default and Tagged Hooks are associated with Scenario.

Therefore, even if @BeforeStep Hooks order is 0 and @Before Hook order is 1. First @Before hooks will execute then @BeforeStep Hook will execute.

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"

Hook File

public class StepDefinitions
{	
	@Before(order = 4)
	public void bf0()
	{
		System.out.println("Before Hook with order 4");
	}
	
	@Before(order = 3)
	public void bf1()
	{
		System.out.println("Before Hook with order 3");
	}
		
	@Before(value = "@login and not @smoke", order = 2)
	public void loginBeforeHook()
	{
		System.out.println("Before Tagged with order 2");
	}
	
	@BeforeStep(order = 1)
	public void bfs1()
	{
		System.out.println("Before Step Hook with order 1");
	}
	
	@BeforeStep( order = 0)
	public void bfs0()
	{
		System.out.println("Before Step Hook with order 0");
	}
	
	@After(order = 2)
	public void af0() throws InterruptedException
	{
		System.out.println("After Hook with order 2");
	}
	
	@After(order = 1)
	public void af1() throws InterruptedException
	{
		System.out.println("After Hook with order 1");
	}
	
	@After(value="@login and not @smoke", order=3)
	public void loginAfterHook()
	{
		System.out.println("Tagged After Hook order 3");
	}
	
	@AfterStep(order=1)
	public void afs1()
	{
		System.out.println("After Step Hook order 1");
	}
	
	@AfterStep(order=0)
	public void afs0()
	{
		System.out.println("After Step Hook order 0");
	}
}

Output of above code will be 

  Background: Title of Background # features/Login.feature:4
Before Tagged with order 2
Before Hook with order 3
Before Hook with order 4
Before Step Hook with order 0
Before Step Hook with order 1
    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 order 1
After Step Hook order 0
Before Step Hook with order 0
Before Step Hook with order 1
    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 order 1
After Step Hook order 0
Before Step Hook with order 0
Before Step Hook with order 1

  @sanity @login
  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 order 1
After Step Hook order 0
Before Step Hook with order 0
Before Step Hook with order 1
    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 order 1
After Step Hook order 0
Before Step Hook with order 0
Before Step Hook with order 1
    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 order 1
After Step Hook order 0
Tagged After Hook order 3
After Hook with order 2
After Hook with order 1

In above example you can observe:

  • @BeforeStep and @AfterStep is associated with Step only.
  • @Before Hooks execute in Low to High order.
  • @After Hooks execute in High to Low order.

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.