Cucumber Tags and Hooks

Reading Time: 3 minutes

In continuation of my cucumber series, this blog will help us clear the concept of using cucumber tags and hooks. We will look at some practical approach and how a lot of time can be saved by using different tags and hooks.

What are cucumber tags and hooks?

Now, tagging is nothing but a simple annotation. So, you can provide your annotation using a conventional symbol “@”
On the other hand, hooks in Cucumber is the code block which can have optional definition in step definition file (with each scenario) by using the annotation @Before and @After.

Let’s say you have a feature file with around 100 scenarios and you don’t want to execute those 100 scenarios every time.
Suppose, out of those 100 you have 15 smoke test scenarios and rest are regression test scenarios
Basically, cucumber by default runs all the scenarios, thus degrading the system performance. Tags help us to skip unnecessary scenarios to save time.

Tagging in Feature File

First, create a simple feature file in your existing project as discussed in previous blog. Say, tagging.feature

Feature: This feature will verify Yahoo Homepage 

@SmokeTest 
Scenario: Search phone number
Given Required phone number will be displayed

@RegressionTest @SmokeTest
Scenario: Search a product
Given Desired deal will be displayed
Then Add it to cart

@RegressionTest
Scenario: Search an email	
Given Desired email will be displayed

Let us now create Test Runner class. It will look like below snippet

package testRunner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/java/featurePackage/tagging.feature", 
glue={"stepDefinitionPackage"},
tags = {"@Before","@SmokeTest , @RegressionTest"},
monochrome = true				
)
public class TestRunnerclass {

}

Lastly, give definition to your StepDefinition.java

package stepDefinitionPackage;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;


public class StepDefinition {
	
	
	@Given("^Required phone number will be displayed$")
	public void required_phone_number_will_be_displayed() throws Throwable {
		 System.out.println("SMOKE EXECUTED");
	}

	@Given("^Desired deal will be displayed$")
	public void desired_deal_will_be_displayed() throws Throwable {
		 System.out.println("REGRESSION EXECUTED");
	}

	@Then("^Add it to cart$")
	public void add_it_to_cart() throws Throwable {
		 System.out.println("REGRESSION EXECUTED");
	}

	@Given("^Desired email will be displayed$")
	public void desired_email_will_be_displayed() throws Throwable {
	   System.out.println("REGRESSION AND SMOKE EXECUTED");
	
	}		
}

Output will look something like this as attached below

  • Please note and scenarios can be included and excluded as per requirement.

Hooks in Cucumber

Sometimes, we need a few prerequisites like to do a setup before or after any test runs. For example, web driver should start before the execution of main script or clear the cookies before the test runs. Also, browser should close itself when the execution gets over.

Briefly, there are only two tags which are supported by hooks
1. @Before : As per its naming convention, we can use this annotation before the execution of main automated test script.
1. @After : As per its naming convention, we can use this annotation after the execution of main automated test script

In above StepDefinition.java let us add hook tags just like the one below

package stepDefinitionPackage;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;


public class StepDefinition {

	
@Before
public void setup()
{
	System.out.println("Before is executed FIRST regardless of it's placement");
}

@After
public void cleanUp()
{
	System.out.println("After is executed at LAST regardless of it's placement\n");
}
	
	@Given("^Required phone number will be displayed$")
	public void required_phone_number_will_be_displayed() throws Throwable {
		 System.out.println("SMOKE EXECUTED");
	}

	@Given("^Desired deal will be displayed$")
	public void desired_deal_will_be_displayed() throws Throwable {
		 System.out.println("REGRESSION EXECUTED");
	}

	@Then("^Add it to cart$")
	public void add_it_to_cart() throws Throwable {
		 System.out.println("REGRESSION EXECUTED");
	}

	@Given("^Desired email will be displayed$")
	public void desired_email_will_be_displayed() throws Throwable {
	   System.out.println("REGRESSION AND SMOKE EXECUTED");
	
	}		
}

Here is the final console output snippet

References :

https://www.javatpoint.com/hook-in-cucumber
https://www.javatpoint.com/cucumber-tags

Knoldus-blog-footer-image

Written by 

Nearly 3 years of experience in automation testing, I call myself an automation enthusiast. I can create, execute automated test scripts using framework guidelines and best practices to ensure wider and efficient end-to-end automation coverage. Simultaneous experience in Defect tracking and bug reporting through JIRA