App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide How To Use Annotations In Cucumber Framework

How To Use Annotations In Cucumber Framework

By Mohit Joshi, Community Contributor -

The world of testing has a lot of frameworks that have accelerated the development cycle, one of which is the Cucumber Framework. It can be easily understood even by someone who does not have the technical expertise. 

Cucumber is a testing framework that supports Behavior Driven Development (BDD). This technique can be well understood by all the contributors to the project, such as QA, Business Analysts, etc. Cucumber’s source was originally written in the Ruby language and was later extended to Java.

 

Cucumber users annotation to define or describe an action. It is a predefined text that helps the compiler understand and execute the command accordingly. This guide explains how to use annotations in Cucumber.

Setting up for Cucumber Testing

Let’s first set up the project using the Cucumber framework. 

  • Step 1: Install IDE and Set up Java

You can install any IDE, however, for the sake of this tutorial, let us use Eclipse IDE. Also, install the latest version of Java on your system. To ensure that Java is installed on your system, run a quick command in the terminal to check the Java version. 


Java -version

  • Step 2: Create a new Maven Project

To create a Maven project, you must install the Maven Plugin, however, in the latest versions of Eclipse, it comes installed already. 

  • Step 3: Add Maven Dependencies to your project

In this step, you have to add several dependencies to your project. Navigate to the pom.xml file in the folder structure and then add the following dependencies – Cucumber Java, Cucumber JUnit, JUnit, and Selenium Java. You can get these dependencies from Maven Repository

  • Step 4: Create a feature file 

Navigate to src/test/resources in the folder structure and then create a feature folder. Also, add a feature file in the feature folder there. 

  • Step 5: Install the Cucumber Plugin 

You can easily install the Cucumber plugin from the Eclipse marketplace. The Cucumber plugin lets the codebase know that you’re creating a Cucumber project. 

  • Step 6: Create Step Definitions

To create Step definitions, navigate to the src/test/java package and then add a step definition folder and file there. 

  • Step 7: Create a Runner Class

Create a class file in the Step Definitions folder and add the runner class script. The runner class will look something like this. 


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

@RunWith(Cucumber.class)
@CucumberOptions(features=”src/test/resources/Features”,
glue={“StepDefinitions”})
public class TestRunner {

}

What are Cucumber Annotations 

Annotations in Cucumber are predefined texts that contain some meaning for the compiler for the execution. In this section, let us dive a little deeper and understand hooks, tags, and more. 

How to use Cucumber Tags 

Tags are used to associate a test with a particular scenario.

For instance, there are multiple feature files containing several scenarios. However, if you require to test only a few scenarios out of all the scenarios from all the feature files, the brute force way combines all the required scenarios in one feature file and then executes it from the test runner class. However, this method becomes a headache and does not bring flexibility. Therefore, tags in such cases come in handy, separating multiple scenarios and executing the required ones only. 

Tags are the names that you give to the scenarios in the feature file. To assign a name, you have to use the ‘@’ symbol before writing the name. For example, to assign a scenario smoke test, use @smoke in the feature file right above the scenario you want to name. 

@name_of_the_test

Scenario: Define the scenario 

Let’s take a practical example to understand how to use cucumber tags. 

  • Create a Feature File 

@Smoke

Feature: Feature to demo tags

Scenario: Example 1
Given
When
And

  • Create Step Definitions

Here, let us create a sample step definition file, where you will bind the necessary code with the scenarios in the feature file. 

  • Create a Test Runner Class

In the below test, the scenario is marked with @SmokeTest, therefore, in the test runner class file, you will assign those tag names that you want to run.


@RunWith(Cucumber.class)
@CucumberOptions(
features=”src/test/resources/Features”,
glue= {“StepDefinition”},
tags = {“@SmokeTest”}
)

public class TestRunner {
}

  • If you have created multiple scenarios, then the above test runner class file will allow the execution of only that code only that matches the names you have provided inside the ‘tags’ script.
  • To execute several scenarios, you must use ‘OR’ keyword while writing the names of the scenarios you want to use inside the ‘tags’ script. For example, to execute all the scenarios that are marked with either @Smoke or @Regression, add tags = {“@Smoke OR @Regression”}.
  • Similarly, the ‘AND’ keyword is used when you have to execute scenarios that contain both keywords. For example, in tags = {“@Smoke AND @Regression”} it will execute scenarios that contain both names.
  • Use the ‘NOT’ keyword to ignore the scenarios that you want to skip from getting executed. 

How to use Cucumber Hooks

Let’s consider a situation where you are creating multiple scenarios, however, while writing the scenarios you notice that before or after the execution of each scenario some sets of actions are the same. Therefore to prevent the repeated use of the same code, hooks are used. Hooks are the blocks of code that run before or after each scenario. 

To define hooks, you must use annotations like @Before and @After. There are three types of hooks in Cucumber – Scenario, Step, and Conditional. Scenario Hooks run before and after each scenario. Step Hooks run before and after each step. Conditional Hooks are linked with tags for conditional execution. 

The idea behind implementing hooks is to manage the setup & teardown and allow better maintenance of code.

  • Create a Feature File 

@RegressionTest
Feature: Feature to Hooks
Scenario: Example 2
Given
When
Then


  • Create Step Definitions

Create the necessary step definitions file required for your project.

  • Create a Class for Hooks 

Create a Java class for hooks. Note that, you must create this hooks class file inside the Step Definitions package. 


package StepDefinitions;
import io.cucumber.java.After;
import io.cucumber.java.Before;
public class Hooks {

@Before
// write code here which you want to be executed before each scenario
}

@After
// write code here which you want to be executed after each scenario

}


  • Create a Test Runner Class

package cucumberOptions;

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

@RunWith(Cucumber.class)
@CucumberOptions(features=”src/test/java/Features”, glue=”StepDefinitions”, tags=”@RegressionTest”)
public class TestRunner {

}

How to Use the ‘Background’ Keyword

Tags and Hooks gives an overview of Cucumber Annotations. Now, let’s look at one more important keyword which helps in improving the overall maintainability of our code. Background keyword is a group of steps that are common to all the scenarios in a feature. It is defined once and for all in the project. It improves the readability and reusability of the code and prevents repeating steps in every scenario. 

Moreover, It is pretty similar to hooks however, the background is visible in the feature file.

Now, let’s understand how to use the ‘background’ keyword with the help of an example. 

  • Create a Feature File 

Feature: Test bstackdemo

Scenario: Check ‘Add to Cart’ button
Given user is logged in
When user clicks on ‘Add to Cart’ button
Then product is displayed in the cart

Scenario: Check ‘Favorite’ button is working
Given user is logged in
When user clicks on ‘Favorite’ button
Then product is displayed in the ‘Favorites’ page

In the above feature file, it can be clearly seen that the same Given step is used for two scenarios. Therefore, let us create a common step with the help of the ‘background’ keyword. The new feature file will look like this. 


Feature: Test bstackdemo
Background:

Given user is logged in

Scenario: Check ‘Add to Cart’ button
When user clicks on ‘Add to Cart’ button
Then product is displayed in the cart

Scenario: Check ‘Favorite’ button is working
When user clicks on ‘Favorite’ button
Then product is displayed in the ‘Favorites’ page

After this, create the necessary Test Runner class and Step Definitions for the feature file and you’re good to go running a successful test using the ‘background’ keyword.

Cucumber is an amazing testing framework that supports BDD (Behavior Driven Development). It offers several tools that ease our testing experience. Cucumber supports several programming languages allowing users to be more flexible. 

You can use Cucumber with popular automation frameworks like Selenium and Cypress. Testing on a real device cloud gives you the advantage of identifying all the issues faster and in a more seamless manner. BrowserStack offers a real device cloud platform that allows you to run your tests on a combination of 3000+ devices, browsers, and operating systems.

Try BrowserStack now

Tags
Automation Testing Cucumber

Featured Articles

How to Run Tests with Cypress and Cucumber: Tutorial

Page Object Model in Cucumber

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack