Cucumber Tags And Or

Profile picture for user devraj

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.