Tags in Cucumber

HOME

In this tutorial, I will explain about Tags in Cucumber

What are Tags?

Tags are great way to organize Features and Scenarios.
By default, Cucumber executes all the scenarios present in a Feature File. If you want to run a specific scenario from the feature file, then tag can be used.

Tags can be declared as below:-

@TagName
   Scenario: Test the scenario

Here,

@: It is a symbol used to declare a tag.
TagName: It is the name of a specific test.

In the below example, there are 5 different scenarios with different tags. If you want to run the scenario with valid username and invalid password, then This can be achieved by using tag @ValidUsernameInvalidPassword in TestRunner

 Feature: Sample use of Tags in Cucumber

   @ValidCredentials
   Scenario: Login with valid credentials
    
    Given User is on Home page
    When User enters username as "Admin"
    And User enters password as "admin123"
    Then User should be able to login sucessfully
    
   @InValidCredentials
   Scenario: Login with invalid credentials
    
    Given User is on Home page
    When User enters username as "username"
    And User enters password as "password"
    Then Login will be unsuccessfull with error message "Invalid credentials"
    
    
   @InValidCredentials @BlankCredentials
   Scenario: Login with Blank credentials
    
    Given User is on Home page
    When User enters username as " "
    And User enters password as " "
    Then Login will be unsuccessfull with error message "Invalid credentials"
    
   @InValidCredentials @ValidUsernameInvalidPassword
   Scenario: Login with valid username and invalid password
    
    Given User is on Home page
    When User enters username as "Admin"
    And User enters password as "aaaa"
    Then Login will be unsuccessfull with error message "Invalid credentials"
    
   @InValidCredentials @InValidUsernameValidPassword
   Scenario: Login with invalid username and valid password
    
    Given User is on Home page
    When User enters username as "SSS"
    And User enters password as "admin123"
    Then Login will be unsuccessfull with error message "Invalid credentials"
    

Running single Cucumber Tag

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)

@CucumberOptions(features = { "src/test/resources/features/CucumberTagsExample.feature" }, glue = {"com.cucumber.demo.definitions" }, tags = "@ValidCredentials ")

public class TestRunner {
}

Tags can be placed above Feature, Scenario, Scenario Outline and Examples

A feature or Scenario can have as many number of tags. But each tag should be seperated by a blank space.

How to execute Multiple Cucumber Tags

Let us consider a situation where a tage (@InValidCredentials) has multiple test scenarios, and you use this tag in Runner class, then all the test scenarios associated with this tag will execute.

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)

@CucumberOptions(features = { "src/test/resources/features/CucumberTagsExample.feature" }, glue = {"com.cucumber.demo.definitions" }, tags = ("@InValidCredentials"))

public class CucumberTestRunner {

}

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

  1. OR operator
  2. AND operator

OR operator

OR means scenarios that are tagged either with @BlankCredentials or @InvalidCredentials will execute. The syntax is mentioned below:

@CucumberOptions(features = { "src/test/resources/features/CucumberTagsExample.feature" }, glue = {"com.cucumber.demo.definitions" }, tags = ("@BlankCredentials or @ValidCredentials"))

AND operator

Suppose you want to test application with multiple test cases and if first test case is failed in the application and the we do not want to test second test case. The syntax is mentioned below:

@CucumberOptions(features = { "src/test/resources/features/CucumberTagsExample.feature" }, glue = {"com.cucumber.demo.definitions" }, tags = ("@BlankCredentials and @InValidCredentials"))

How to ignore Tags in Cucumber?

To skip a specific test scenario , use not keyword.

@CucumberOptions(features = { "src/test/resources/features/CucumberTagsExample.feature" }, glue = {"com.cucumber.demo.definitions" }, tags = ("not @InValidCredentials"))

All these syntax are valid for Cucumber Version – 6.8.1

Below mentioned syntax are not valid for Cucumber Version -6.8.1 and above

AND - {"@BlankCredentials , @InValidCredentials"}
OR - {"@BlankCredentials" , "@InValidCredentials"}
NOT - {"~@InValidCredentials"}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s