Cucumber Hooks

Hooks are the block of code that runs before or after the execution of each scenario. They are defined under step definition file using the annotation @Before and @After. They help in reducing the code redundancy.

@Before()  {   







Why Cucumber Hooks are needed?

While testing, there are certain prerequisite steps need to be performed before testing the actual test scenario. These prerequisite steps could be below:

Likewise, after testing of scenarios some steps need to be executed which are below:

  • Close/Exit Webdriver
  • Close Database connections
  • Clearing the test data
  • Clearing browser cookies
  • Log out from the application
  • Printing reports or logs
  • Taking the screenshots of error

Thus, to handle these situations, cucumber hooks are used. Any preconditions that need to be performed before executing the test steps are defined under the @Before method and the conditions which need to be run after execution of steps are defined under @After method.


How to Implement Cucumber Hooks

Let us see how to Implement Cucumber hooks

Step 1: Feature

Feature: Login

Scenario: Successful Login with Valid Credentials

Given user is in login page

When user provides below credential to login

        | username| Password |

        | testuser| [email protected] |

Then user should be successfully navigated to home page


Step 2: Create a Test runner class.


import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

import cucumber.api.CucumberOptions;



              features= "E:\\AutomationCode\\DemoBDD\\src\\main\\java\\com\\qa\\features"

              ,glue= {""}

              ,monochrome= true

              ,format = {"pretty","html:cucumber-html-report", "json:json_output/cucumber.json"})

public class TestRunner {



Step 3: Step Definition Implementation

Here we will use cucumber hooks @Before and @After method along with the implantation of the Given When Then statement.

Ensure that the package import statement should be import; & import;


public void SetUp() {    

       driver = new FirefoxDriver();


@Given("^user is in login page$") 



@When("^user provides below credential to login$")
public void user_provides_below_credential_to_login(DataTable credential) throws Throwable {

//Write the code to handle Data Table
List<List<String>> data = credential.raw();

//This is to get the first data of the set (First Row + First Column)

//This is to get the first data of the set (First Row + Second Column)



@Then("^user should be successfully navigated to home page$")
public void
user_should_be_successfully_navigated_to_home_page() throws Throwable {




public void teardown() {



On execution of this code, the following will be the sequence of execution:

  • In the beginning, @Before annotation will be invoked which will set up the web driver and other required prerequisites to execute the test.
  • The Given statement will be executed after setting up web driver and other prerequisites.
  • The When statement will be executed after the execution of the Given statement,
  • The Then statement will be executed after the execution of the When statement,
  • In the end, @After hook will be called which will close the web driver and do the cleanup process.


Tagged Hooks

The hooks can be tagged by associating the test name with the @Before and @After hooks. Learn how to use  Cucumber Tags


@Before (“@SmokeTest”)  

@After (“@RegressionTest”)