Cucumber Tags — Selenium (Java)

Anandhi K
4 min readFeb 15, 2021

--

In Cucumber we create many Feature files and each with many scenarios. To test any specific feature, we will run all the scenarios together from a Feature file from the Runner class.

What If we want to run a few selected scenarios from a Feature file?

Or, what if we want to run a few selected scenarios from different Feature files?

Here we use the option called Tags.

In this article we see in detail about,

  • What are Tags?
  • How to use Tags in Feature file?
  • How to use Tags in Runner class?
  • How to combine Tags using logical ‘AND’ and ‘OR’?
  • How to ignore Scenarios using Tags?
  • What is Tag Inheritance?
  • How to use tags while running as maven test?

We want to create some test suites each with different scenarios. Let we create SanityTest, Regression and SmokeTest.

An option to organize scenario execution in Cucumber is called Tags. Tags starts with ‘@’ eg. @SmokeTest.

Using Tags in Feature File :

Tags can be used with ,

  • Feature
  • Scenario
  • Scenario Outline
  • Examples

Note : It is not possible to use tags with Background or steps(Given, When,Then, And and But).

Content of pom.xml file is,

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CucumberTagSample</groupId>
<artifactId>CucumberTagSample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.9.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<properties>
<property>
<name>junit</name>
<value>true</value>
</property>
</properties>
<includes>
<include>**/*Runner.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>

Let we use Tags in our Feature file. Create two Feature files under /src/test/java/features/.

File 1 :

@EndtoEnd
Feature: Sample Tags One

@SanityTest @Regression
Scenario: Valid Login
When User enters valid login credentials

Scenario: Invalid Login
When User enters invalid login credentials

@SmokeTest
Scenario: Open New Account
When User clicks Open New Account

@Regression
Scenario: Accounts Overview
When User clicks Accounts Overview

@SmokeTest
Scenario: Transfer Funds
When User clicks Transfer Funds

@Regression @SanityTest
Scenario: Logout
When User Clicks Logout

File 2:

Feature: Sample Tags Two
@Regression
Scenario: Bill Pay
When User clicks Bill Pay

Scenario: Request Loan
When User clicks Request Loan

@SmokeTest
Scenario: Find Transactions
When User Clicks Find Transactions

Now we have two feature files each with common tags.

Using Tags in Runner class :

Let we create a runner to run these feature files under /src/test/java/runner/.

@RunWith(Cucumber.class)
@CucumberOptions(
features="src//test//resources//features//",
glue= {"stepdefs"},
monochrome=true
)
public class TagRunner {
}

When we run this runner, this will generate implementation for the steps given in our Feature file. With this, let we create a Step definition file under /src/java/test/stepdefs/.

public class TagStepDef {
@When("^User enters valid login credentials$")
public void user_enters_valid_login_credentials() throws Throwable {
System.out.println("Valid login");
}
@When("^User enters invalid login credentials$")
public void user_enters_invalid_login_credentials() throws Throwable {
System.out.println("Invalid login");
}
@When("^User clicks Open New Account$")
public void user_clicks_Open_New_Account() throws Throwable {
System.out.println("Open New Account");
}
@When("^User clicks Accounts Overview$")
public void user_clicks_Accounts_Overview() throws Throwable {
System.out.println("Account Overview");
}
@When("^User clicks Transfer Funds$")
public void user_clicks_Transfer_Funds() throws Throwable {
System.out.println("Transfer Funds");
}
@When("^User clicks Bill Pay$")
public void user_clicks_Bill_Pay() throws Throwable {
System.out.println("Bill Pay");
}
@When("^User Clicks Find Transactions$")
public void user_Clicks_Find_Transactions() throws Throwable {
System.out.println("Find Transactions");
}
@When("^User clicks Request Loan$")
public void user_clicks_Request_Loan() throws Throwable {
System.out.println("Request Loan");
}
@When("^User Clicks Logout$")
public void user_Clicks_Logout() throws Throwable {
System.out.println("User Logout");
}
}

If we run our runner class, all the scenarios in our feature files will be executed and the output would be,

Valid login
Invalid login
Open New Account
Account Overview
Transfer Funds
Bill Pay
Open New Account
Find Transactions
User Logout

Now to run only specific scenarios, we should use the option called 'tags' in our runner class. To run only the scenarios of @Regression we need to change our runner class as,

@CucumberOptions(
features="src//test//resources//features//",
glue= {"stepdefs"},
monochrome=true,
tags= "@Regression"
)

Combine Tags using logical operators :

To run the scenarios of either @Regression or @SanityTest,

tags="@Regression or @SanityTest"

To run the scenarios of both @Regression and @SanityTest,

tags= "@Regression and @SanityTest"

Ignore Scenarios:

To run the scenarios other than @Regression,

tags= " not @Regression"

Tag inheritance :

  • Tags are inherited by child elements.
  • Tags that are used at the Feature level will be inherited by Scenario, Scenario Outline or Examples.
  • Tags that used at the Scenario Outline will be inherited by Examples.

To run scenarios from specific feature files, tag them at the Feature level.

@EndtoEnd
Feature: Sample Tags Two

To run all scenarios from various Feature files and to ignore @Regression,

tags= "not @Regression and @EndtoEnd"

To run all scenarios from @EndtoEnd other than @Regression and @SanityTest,

tags= "(not @Regression) and (not @SanityTest) and @EndtoEnd"

To run scenarios which are not marked with tags,

tags= "(not @Regression) and (not  @SanityTest) and (not @SmokeTest)"

To run from command line as maven test,

mvn test -Dcucumber.filter.tags="@SmokeTest"

Related Links :

https://youtu.be/JWBMBIBBsd8

References :

Cucumber Reference — Cucumber Documentation

Conclusion :

Cucumber tags are very much helpful in customizing our Test packs while automating large scale applications having various Feature files with multiple scenarios.

--

--

Anandhi K
Anandhi K

Written by Anandhi K

DevOps Test Automation Consultant, Trainer and Blogger in Cypress, Selenium, Cucumber, Playwright & CI/CD Tools.

No responses yet