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()  {   

    //code

  }

@After(){   

    //code

 }

 

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.

package com.qa.runner;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

import cucumber.api.CucumberOptions;


@RunWith(Cucumber.class)

@CucumberOptions(

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

              ,glue= {"com.qa.stepDefination"}

              ,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 cucumber.api.java.After; & import cucumber.api.java.Before;

@Before

public void SetUp() {    

       driver = new FirefoxDriver();

       driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

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

driver.get("https://teQuality.com");

}

@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)
driver.findElement(By.id("username")).sendKeys(data.get(0).get(0));

//This is to get the first data of the set (First Row + Second Column)
driver.findElement(By.id("password")).sendKeys(data.get(0).get(1));

driver.findElement(By.id("login")).click();

}


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

driver.findElement(By.id("menu-main_menu-1")).isDisplayed();

}

@After

public void teardown() {

       driver.close();

}

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

Example:

@Before (“@SmokeTest”)  

@After (“@RegressionTest”)