Cucumber Scenario Hooks

Profile picture for user devraj

Hooks are blocks of code that can run at various points in the Cucumber execution cycle. Hooks allow us to better manage the code workflow and helps us to reduce the code redundancy. Hooks are used to perform prerequisite steps before testing any test scenario.

Multiple Types of Hooks Exists in Cucumber:

  • Scenario Hooks
  • Around Hooks
  • Step Hooks
  • Conditional or Tagged Hooks
  • Global Hooks

Around Hooks and Global Hooks are not supported by Cucumber JVM.

Cucumber supports 2 types of Scenario hooks @Before and @After. @Before executes before any test scenario and @After executed after the scenario. Method defined within Before and After hooks, always run, even if the scenario gets passed or failed.

In Before hook you can add code for starting WebDriver, browser, DB connection, cookies etc. In After hook you can close browser, DB connection and capture screenshots etc.

Hooks are defined in step definition files or package where step definition file is.

Think twice before you use Before:

Whatever happens in a Before hook is invisible to people who only read the features. You should consider using a background as a more explicit alternative, especially if the setup should be readable by non-technical people. Only use a Before hook for low-level logic such as starting a browser or deleting data from a database.

Example: Cucumber Scenario Hooks

package com.pb.cucumbertest.stepdefinitions;
import com.pb.cucumbertest.helper.TestBase;
import io.cucumber.java.After;
import io.cucumber.java.Before;

@RunWith(Cucumber.class)
public class StepDefinitions extends TestBase
{	
	@Before
	public void bf()
	{
		TestBase tb = new TestBase();
		tb.setDriver();
	}

	@After
	public void af()
	{
		driver.quit();
	}
}

Here we have 2 Hooks in StepDefinition class. @Before hooks calling  method setDriver() of testbase class and @After hook calling method to close browser.

Now in our below feature file. @Before hook will be executed before each scenarios and @After hook will be executed after each scenario. I want to open my browser before every scenario and close it after every scenario. This step is repetitive and common to all the feature file and scenario inside it. So we have used hooks. Background execute after the @Before hook.

Here is the order of execution of Hooks, Background and Scenario:

1. Before Hook
2. Background
3. Scenario
4. After Hook

Feature: Registration, Login and MyAccount

  Background:
    Given I am on the homepage
    And I follow "Sign in"
  
  @smoke
  Scenario Outline: Verify Login Functionality
    And I fill "email address textbox" with "<email>"
    And I fill "password textbox" with "<password>"
    And I click "sign in button"
    Then I should see "<heading>" heading

    Examples: 
      | email                     | password | heading        |
      | goswami.tarun77@gmail.com | test1234 | MY ACCOUNT     |
      | wrongusername             | test     | AUTHENTICATION |

  @register
  Scenario: Create New User
    And I fill "registration email textbox" with "goswami.tarun77+1@gmail.com"
    Then I click "create an account button"
    And I enter following details
      | First Name | Tarun    |
      | Last Name  | Goswami  |
      | Password   | Test1234 |
      | Date       |       13 |
      | Year       |     1989 |
    And I click "register button"