Skip to main content

Cucumber Tags And Or

There would be situation when you want to run all the scenarios related to two or more features. You may be interested in executing a scenarios that are tagged with all desired tags or either of one.

Logic of AND and OR work similar to logical operator exist in other programming language. Difference is we have our own way of using this logic, we do not use operators.

There is also logical NOT in cucumber if you want to skip or ignore particular scenario. Which is represented by ~ sign.

Consider below example:

Feature: Registration, Login and MyAccount
 
  Background: Titllrgdgdgd sgdgdsfg
 	  Given I am on the home page
    And I follow "Sign in"	  
 	   
  @sanity
  Scenario: Verify Login Functionality
    When I fill "email address textbox" with "goswami.tarun77@gmail.com"
    Then I fill "password textbox" with "Test1234"  
   
 @regression
  Scenario: Create New User
    When I fill "registration email textbox" with "goswami.tarun77+1@gmail.com"
    Then I click "create an account button"
    
  @sanity @regression
  Scenario: Verify Logout Functionality
    When I fill "email address textbox" with "goswami.tarun77@gmail.com"
    Then I fill "password textbox" with "Test1234"
    And I click on "Logout"    
    

ANDing: When you want to execute scenarios tagged with @regression and @sanity. Use below code in your Cucumber runner class:

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

@RunWith(Cucumber.class)
@CucumberOptions(
		plugin = {"pretty", "html:target/cucumber"},
		features = {"features"},
		glue={"com.pb.cucumbertest.stepdefinitions"},
		monochrome = true,
		strict = true,
		dryRun = true,
		tags = {"@regression and @sanity"} //This is same as {"@regression", "@sanity"} 
		)

public class Runner {

}

This will execute first scenario. Note there is another technique mentioned in comment that both tags are in separate double quotes and are separated by comma.

ORing: When we want to run the Scenarios with either of the mentioned Tags.  For example when you want to execute the scenario tagged with @sanity or @regression.

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

@RunWith(Cucumber.class)
@CucumberOptions(
		plugin = {"pretty", "html:target/cucumber"},
		features = {"features"},
		glue={"com.pb.cucumbertest.stepdefinitions"},
		monochrome = true,
		strict = true,
		dryRun = true,
		tags = {"@regression or @sanity"} 
		)

public class Runner {

}

This will execute both scenarios. Note the difference here, both tags are in single double quotes separated by or.

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.