How To Use Annotations In Cucumber Framework [Tutorial]

Gunjan Kaushik

Posted On: August 10, 2021

view count236251 Views

Read time13 Min Read


This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Cucumber Tutorial.

Cucumber is one of the most popular open-source BDD frameworks that facilitates Selenium test automation. It offers a set of tools that helps us to manage the various scenarios available in multiple feature files. We have already seen how to configure Cucumber and write a basic Cucumber test using a feature file in our earlier blog on automated testing with Selenium and Cucumber. However, it doesn’t suffice when you are working on complex projects involving multiple feature files.

To handle multiple feature files and the multiple scenarios in each of the files, there is an inherent need for functionality to manage the execution of these scenarios. This is where Cucumber provides us a handful of utilities to ease the maintenance of our framework.

gif

Source

This Cucumber tutorial will help you learn about Cucumber annotations ranging from tags, Cucumber hooks, and more. However, you can refer to this guide for configuring the Cucumber setup In Eclipse and IntelliJ if you haven’t configured it in your machine yet. If you’re looking to improve your Cucumber interview skills, check out our curated list of Cucumber interview questions and answers.

Let’s look into following Cucumber annotations individually and explore more in this Cucumber tutorial.

How To Use Cucumber Tags

Automation frameworks today offer several APIs to cover various situations. But you can’t really mock everything! It’s cumbersome to mock every scenario in a new file, and it’s not a good deal to leave them in the existing file. So instead, we can use cucumber tags to separate the multiple scenarios in a single file and execute only one of them.

If you are familiar with TestNG, you would know how to specify the test method to be included or excluded in the XML file (refer to our TestNG tutorial for more).

While performing automation testing with Cucumber, you may use Tags in the feature file. Tags are nothing but a meaningful name you may give to your test scenario in the feature file. The ideal way to use tags is by using ‘@.’

For example; To mark a scenario as a regression test case in your framework, you may tag it as ‘@RegressionTest’ right above the scenario in the feature file.

Once you define the tags for your scenario, you may specify which scenario you would like to execute in your runner file. You may either tag the scenario or the entire feature file that the Scenario, Outline, etc., will then automatically inherit.

Below is an example of running multiple selective tests or a single feature file using the Cucumber annotation- Tags.

cucumber

Read – Cucumber Best Practices For Selenium Automation

Execution of Selective Scenarios using Tags

Let us consider an example where multiple scenarios are present in a feature file with different tags, as shown below:

Now, let us try to execute only the tests with the tag as ‘@Sanity’ using the Runner class as shown below:

On executing the above file, you will see that only two tests are executed, tagged as @Sanity.

executed

Here is one more example where we will execute the entire Feature File using its tag.

Execution of Feature File using Tags

The updated test runner file would look like below:

On executing this file, we will notice all the scenarios under the feature file are executed.

scenarios

So far in this Cucumber tutorial, we have seen execution through a single tag. Let us now explore the scenarios under which more than one tag is executed. To implement, we may use the logical operators either AND or OR. Let us see examples of each.

Execution of Scenarios using Tags with Logical Operators

To execute scenarios that are using either @Sanity or @E2E tag, we will use the OR keyword as shown in the code below:

You will see that three tests are executed that have either the @Sanity or @E2ETest tag.

cucumber

Similarly, we will use the AND operator to execute scenarios with both @Sanity and @Regression tags. The below code has been updated to run the same scenario.

And here you go! You will see just one test is tagged while both the tags are getting executed.

Now here comes another use case! What if you want to execute all the tests but ignore a few? How do we do that?

Well, you can use the same tags but with a ‘not’ preceding the tag. If you add ‘not’ before the tag name in the runner class, you will notice that the scenarios under the tag get skipped, as shown in the below example:

The execution would happen for all the scenarios except the ones tagged with @Sanity.

Next in this Cucumber tutorial, we will learn about Cucumber Hooks, which like other Cucumber annotations, are code blocks that can be executed either before or after scenarios.

How To Work With Cucumber Hooks

You may know that multiple prerequisites are required at the start of test execution. Right from setting up the web driver, browser settings to cookies, navigating to the specific URL, etc.

Similarly, some steps must be performed after executing the test scenarios, like killing the web driver connection, clearing browser cookies, generating reports, etc. Such cases can be easily handled using one particular type of Cucumber annotations, namely Cucumber Hooks.

You might have worked across different TestNG annotations, like BeforeTest, BeforeMethod, BeforeSuite, AfterTest, AfterMethod, AfterSuite, etc. Unlike TestNG, Cucumber provides only two hooks, i.e., before and after. We will now see how we can implement these Cucumber hooks in our tests.

Implementing Cucumber Hooks

In this section of the Cucumber tutorial, we will now see how Cucumber hooks can be implemented for basic scenarios. We will cover examples where we have a single scenario and one with multiple scenarios.

Below is an example of a feature file with a single feature.

Next, let us create a simple step definition file for the feature file we just created.

Further, we will create the Java class for the Cucumber hooks to implement in our framework. This class is created under the stepDefinitions package, as shown in the below code:

Note that I am using simple text inside hooks methods, and you may use more logical steps and their corresponding code for your Selenium test automation.

We need to create the step definitions for the specific feature by calling the @SingleFeature tag we used for our feature file in the runner class, as shown in the below code snippet:

On executing the above code, you will see that the execution of the @Before and @After hook provides the following result:

executing the above

Now that we have covered an example with a single feature let’s take the example with multiple feature files. We will add one more feature to the project and execute these scenarios in parallel using the Maven Surefire Plugin.

Feature File 1

Feature File 2:

Let’s have a look at the step definition files for these features.

Step Definition File 1:

Step Definition File 2:

Next, in this Cucumber tutorial, we will configure the maven surefire plugin in our pom.xml with the parallel attribute for methods.

Plugin configuration in pom.xml:

Once you execute the pom.xml using maven install or any other relevant command, you will notice that the execution results clearly show different thread numbers (refer to the three lines highlighted in pink in the logs below) for the two feature files. This indicates that our feature files were executed in parallel.

Selenium test automation

Integrating parallel test execution for Selenium test automation to the Cucumber framework saves a considerable amount of time. However, for effective parallel execution, you might face challenges in terms of resources and time. As not every user might be using the same browser with the latest version or the same resolution and operating system.

Leveraging cloud platforms like LambdaTest, which offers cross browser testing on cloud, helps you achieve maximum test coverage at a pace. LambdaTest supports an online selenium grid of 2000+ browsers, browser versions, and operating systems to accelerate automation testing with the ability to perform parallel testing at scale.

Now that we have learned about Cucumber hooks let’s explore tagged hooks in Cucumber annotations in depth.

Read: Cucumber.js Tutorial with Examples For Selenium JavaScript

You can take this certification as proof of expertise in the field of test automation with JavaScript to empower yourself and boost your career.

Here’s a short glimpse of the Selenium JavaScript 101 certification from LambdaTest:

Tagged Hooks In Cucumber

Tagged Hooks are basically the problem solvers when you need to perform different Before and After actions for different scenarios. To explain in a more simple way, think you have ten different tags like Sanity, Regression, etc., which need to be tested with different URLs. This is where Tagged hooks help you achieve that.

Let us create a simple feature file with three different scenarios to understand the concept:

Next, we will create the step definition file for the feature.

Now we will create the hooks file and define the tagged hooks in it. This can be done using the @Before or the @After hook followed by the tag name of the scenario as shown in the code below:

Note that I have added Before and After hooks for scenario one and scenario two outlined in the feature file with tags @CaseOne and @CaseTwo, respectively.

Also, I have used a @Before and @After hook to be executed before and after each scenario. Let us run the same test runner file as used earlier and see the results:

same test runner

This way, we can write unique prerequisite actions and post-execution actions for each scenario.

Let’s learn how the same tagged hooks can be used for multiple scenarios as we move forward. All you have to do is simply append the tag name for the scenarios in the hooks file using the ‘or’ keyword, as shown below.

I am adding @CaseTwo with the @CaseOne tag name in the file we just used and removing the @Case tagged hook.

On executing the test, you may see the results in which the same @Before and @After hooks are executed for scenarios with tags @CaseOne and @CaseTwo.

annotations in cucumber framework

Setting Order or Priority of Cucumber Hooks

If you have worked on TestNG, you must be familiar with the priority of tests and execution order. Similarly, Cucumber hooks can also be executed as per order.

Let us consider an example with a hooks file consisting of two @After hooks and two @Before hooks. We will set the order of the hooks as per our requirement by simply specifying the order as in the hooks file code below:

As we execute the code, you will see that hook execution is similar to what we have defined.

cucumber framework

Now, you may set the order of execution of hooks as per your requirement.

Understanding The ‘Background Keyword’ In Cucumber

So now that we have learned about the different Cucumber annotations that can be used, we will understand another important keyword in Cucumber called the Background Keyword.

We have multiple scenarios that require the same prerequisites, like logging in or navigating to a URL in real-time. Now you may say that we can use the Cucumber hooks to trigger such scenarios. But what if all the scenarios in a feature need this prerequisite, and we are working with multiple features!

To make the code more efficient and manageable, Cucumber provides the Background Keyword, which contains a series of steps executed before each scenario of the feature file. This Background keyword, along with the steps, is placed in the feature file.

Let us see how it works by using an example. Consider a ticket booking application. To search for tickets or to book a ticket, you will have to login to the website. We will use the Background keyword to define the login functionality which each scenario can reuse.

The feature file for our example would look like below:

Since logging in is required in both the scenarios above, we keep the Login functionality under Background. Now, we will create the step definition class.

Note that this is all a dummy representation, and you may use logical code that would trigger the functionality of your application.

Let us now run this feature using the test runner file.

cucumber-framework

As you can see, the steps under the Background Keyword were executed before both scenarios.

We did not use any of the Cucumber hooks, but the Background Keyword solved our problem of executing a series of steps before each scenario in a feature file.

Now you must be wondering what if we use hooks along with the Background Keyword. Since Cucumber hooks execute before the execution, Background would execute only after @Before hook. We will use the below hooks file for our demonstration-

The execution results would look like this:

pasted image

And here you go! You have successfully implemented Cucumber hooks along with Background Keywords.

Wrapping Up!

Like TestNG, Cucumber provides us with various functionalities that can be used as annotations to enhance our test scripts.

  • Tags are used to identify scenarios in feature files uniquely. Thus, we can efficiently execute particular scenarios using tags and exclude specific scenarios using the same.
  • Hooks let us define prerequisites to be executed before the scenarios. We may use tags along with hooks so that specific pre-steps are executed before specific scenarios. Additionally, you may define the order of execution tags using the order parameter.
  • Background keyword comes as an enhancement when we need to run specific steps before each scenario in a feature file. A single background can serve the purpose of that pre-requirement for any feature file.

You can leverage these Cucumber annotations individually or as a combination of multiple annotations to attain maximum benefits for your Selenium test automation. In addition, you may use Cucumber hooks along with Background or tagged hooks as per the requirements of your project.

Happy testing!

Frequently Asked Questions

What Are The Annotations In Cucumber?

Cucumber Annotations are predefined text, which holds a specific meaning. It allows the compiler/interpreter to know what should be done upon execution.

Author Profile Author Profile Author Profile

Author’s Profile

Gunjan Kaushik

Automation Test Engineer with more than 8 years of experience. Over the years I have worked on multiple automation tools in different domains. Apart from being a techie, I love traveling and am an adrenaline junkie.

Blogs: 2



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free