Cucumber Tags

The feature files cover different scenarios which basically tests the different functionality of the application. These scenarios could be the part of the smoke test or regression tests etc. In certain situations, in the project, there could be a need to just run the smoke tests or regression tests. This need can be fulfilled using cucumber tags. Tags are used to associate a group like smoke, regression, etc. with a particular scenario or scenario outline.

Tagging can be done not just at the Scenario level but also at the feature level. Tagging of feature level is done through by adding tags at the features file. Tag that exists on a Feature will be inherited by Scenario, Scenario Outline, or Examples.

The tags help in fulfilling below purposes:

  1. Through the test runner file, it is possible to execute the associated scenarios or feature with a specific tag.
  2. It provides the feasibility to prepare reports for specific scenarios under the same tag

 

How to define tags

Tags can be declared in a feature file by the following syntax:

@TagName

Scenario: Scenario Name

@: It is a symbol used to declare a tag.

@TagName: It is the name of the specific tag

 

How to use tags

Suppose a feature file consists of 10 scenarios. The scenarios are basically categorized into smoke tests or Regression tests. Thus, the tags will be associated with scenarios as below:

@SmokeTest

Scenario: Sign out in Gmail

Given User does not have a  Gmail account


@RegressionTest

Scenario: Login to Gmail account

Given User have a  Gmail account


@SmokeTest

Scenario: User forgot Gmail account password

Given User have a  Gmail account


@RegressionTest

Scenario: User forgot Gmail account username

Given User have a  Gmail account

 

Analysis of Feature file

1. The scenarios are categorized and associated with @SmokeTest and @RegressionTest Tags

2. The scenario of a particular category can be included or excluded as per the requirement at the time of execution.

 

Define Test Runner Class

Suppose only those scenarios which are declared under the smoke test needs to be executed, then it can be defined under Test Runner as:

tags={“@SmokeTest”} 

 

Complete code

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"}

                  ,tags= {"@SmokeTest"}

       )

public class TestRunner {

}  

 

How to use multiple tags in cucumber testing

f the test scenario is a part of Smoke test as well as the Regression test, then the scenario can be tagged with Smoke Test as well as the Regression test.  

Example:

@SmokeTest @RegressionTest

Scenario: Sign out in Gmail

Given User does not have a  Gmail account

@SmokeTest @RegressionTest 

Scenario: Login to Gmail account

Given User have a  Gmail account

@SmokeTest

Scenario: User forgot Gmail account password

Given User have a  Gmail account

@RegressionTest

Scenario: User forgot Gmail account username

Given User have a  Gmail account

Here the execution of Smoke Tests will run Scenario 1, Scenario 2, and Scenario 3

And the execution of Regression Tests will run Scenario 1, Scenario 2, and Scenario 4  

 

Test Execution with multiple tags

The testing through multiple tags can be done by using two operators:·        

  • AND operator·        
  • OR operator 

They are defined under the Test Runner class as explained in the previous example

 

AND operator

The AND operator is used when the execution of the second category test depends upon the first test. If the first category tests are passed, then only the second category test gets executed, or else the testing gets terminated. Syntax of using the AND operator is below

Tags = {“@SmokeTest”, “@RegressionTest”}

In the above example if the Smoke Tests are passed then only Regression tests will be executed. 

 

OR operator

The OR operator is used where there is no dependency on executions. Irrespective of the test results of the first category, the second category tests will get executed. Syntax of using OR operator is below 

Tags = {“@SmokeTest, @RegressionTest”} 

In the above example even if the Smoke Tests fails then also Regression tests will be executed. 

 

 Ignore Tags in Cucumber Testing

If any category of tests needs to be skipped while execution, then it can achieve using Ignore tags.

Tags = {“@SmokeTest”, “@~RegressionTest”}

In the above example, the Smoke Tests will be executed, and Regression tests will be skipped.