Selenium and Cucumber

Tags in Selenium and Cucumber BDD Framework with Java 

Tags in Cucumber

Introduction 

When we execute our test cases with cucumber, we can tag our test cases based on their types, tags use ‘@’ symbol with some text I.e., tag name, e.g., @SmokeTest. 

We can also do some documentation with the help of tags. Tags are useful when we want to run a specific type of test case from different feature files having similar kinds of scenarios by tagging them with the same tag name and then executing them through the test runner file by placing the tag in it so that it will run only those scenarios which have that specific tag, we can run a single tag or multiple tags and also run with a combination of tags or using AND, OR conditions.

In this blog, we will discuss- 

  • Project setup for using tags in cucumber  
  • How to mark features and scenarios with tags in cucumber
  • How to use @ symbol with some text 
  • How to have multiple tags for a feature or scenario 
  • How to run specific tags in the test runner 

Project setup for using tags in cucumber 

Firstly, we must install all prerequisites in our project (for more info about prerequisites you can visit- Web automation using Selenium and Cucumber with Java

Add the maven dependencies. 

After verifying the prerequisites, add the required dependencies as shown below: 

To get the maven dependencies first we need to visit the following link: 

https://mvnrepository.com/repos/central  and then we need to search for the required dependency 

Project setup for using tags in cucumber

Select Cucumber JUnit>click on the latest version>copy the dependency 

JUnit

> add it to the pom.xml file 

Similarly, we need to search for Cucumber JVM:JUnit(io.cucumber)> Select Cucumber JVM:JUnit(io. cucumber) >click on the latest version>copy the dependency 

Tags in cucumber JVM

> add it to the pom.xml file 

Below is an image of the pom.xml file after all the dependencies have been added 

tags in cucumber code

Mark features and scenarios with tags 

In order to use tags, we first need to create a feature file. 

To create a new feature file, right-click on the Features folder in ‘src/test/resources’> click on New> go to File>provider File name with ‘.feature’ extension> click on Finish 

Features and scenarios for tags in cucumber

A feature file with the name ‘Tags’ has been created in the below image. 

tags in cucumber code

Next, we need to create the features, scenarios for the features, and the steps to follow for the specific feature as per the scenario along with the tag. 

Feature: Feature to demonstrate tags  
@Smoke 
Scenario: Validate the search functionality of google 
    Given browser is launched 
    When User enters a text in search box 
    And clicks on the search button
    Then the user is redirected to search results 
tags in cucumber code

Code explanation:  

In this example, we have a feature file containing the title of the feature as Feature to demonstrate tags, and a scenario to validate the search functionality of Google, under the @Smoke tag followed by the steps given by using Given, When, Then, And which will instruct the process for testing the particular feature. 

How to have multiple tags for a feature or scenario

After creating the feature file, we can add more scenarios with different tags in the feature file. 

Feature: Feature to demonstrate tags 
@Smoke
Scenario: Validate the search functionality of google
    Given browser is launched 
    When User enters a text in search box 
    And clicks on the search button 
    Then the user is redirected to search results 
@Regression 
Scenario: Filling in the registration form
    Given I want to fill up the registration form 
    And get admission in the institution
    And the duration to fill the form is from 1stSept2022 to 5thSept2022 
    When I complete filling in the form 
    And submit it on 5thSept2022 
    Then I validate if the registration process has been done successfully 
@Smoke @Regression 
Scenario: Validate the login functionality  
    Given user has the login credentials 
    When User enters the login ID and password
    And clicks on login button 
    And clicks on login button 
@Sanity 
Scenario: Verify the title of the page 
    Given user is on the home page of Gmail account 
    And the user clicks on the sent option
    And checks the sent items 
    When user goes back to the home page 
    Then the title should be Google  
tags in cucumber code

Code Explanation: 

In the above example, we have added more scenarios to the feature file under the title of the feature as Feature to demonstrate tags.  We have added a scenario for Filling in the registration form under the @Regression tag, a scenario to Validate the login functionality under the @Smoke @Regression tags and a scenario to Verify the title of the page under the @Sanity tag, along with the steps by using Given, When, Then, And which will instruct the process for testing each scenario. 

Next, we need to run the feature file and then copy the provided functions for Given, When, And, Then under the message stating to implement the missing steps from the console, make changes to it and paste it into the class created in the Step Definitions folder.

package StepDefinitions; 
import io.cucumber.java.en.And; 
import io.cucumber.java.en.Given; 
import io.cucumber.java.en.Then; 
import io.cucumber.java.en.When; 
public class Steps { 
    @Given("browser is launched") 
    public void browser_is_launched() { 
        System.out.println("browser is launched"); 
} 
    @When("User enters a text in search box") 
    public void user_enters_a_text_in_search_box() { 
         System.out.println("User enters a text in search box"); 
} 
    @And("clicks on the search button") 
    public void clicks_on_the_search_button() { 
         System.out.println("clicks on the search button"); 
} 
     @Then("the user is redirected to search results")
     public void the_user_is_redirected_to_search_results() { 
         System.out.println("the user is redirected to search results"); 
} 
     @Given("I want to fill up the registration form") 
     public void i_want_to_fill_up_the_registration_form() { 
         System.out.println("I want to fill up the registration form"); 
} 
     @And("get admission in the institution") 
     public void get_admission_in_the_institution() { 
        System.out.println("get admission in the institution"); 
} 
     @And("the duration to fill the form is from 1stSept2022 to 5thSept2022") 
     public void the_duration_to_fill_the_form_is_from_1st_sept2022_to_5th_sept2022() { 
         System.out.println("the duration to fill the form is from 1stSept2022 to 5thSept2022"); 
} 
     @When("I complete filling in the form")
     public void i_complete_filling_in_the_form() { 
          System.out.println("I complete filling in the form"); 
} 
     @And("submit it on 5thSept2022")
     public void submit_it_on_5th_sept2022() { 
          System.out.println("submit it on 5thSept2022"); 
}   
     @Then("I validate if the registration process has been done successfully") 
     public void i_validate_if_the_registration_process_has_been_done_successfully() { 
         System.out.println("I validate if the registration process has been done successfully"); 
} 
     @Given("user has the login credentials") 
     public void user_has_the_login_credentials() { 
         System.out.println("user has the login credentials"); 
} 
     @When("User enters the login ID and password") 
     public void user_enters_the_login_id_and_password() { 
         System.out.println("User enters the login ID and password"); 
} 
     @And("clicks on login button") 
     public void clicks_on_login_button() { 
         System.out.println("clicks on login button"); 
} 
      @Then("the user is redirected to the home page") 
      public void the_user_is_redirected_to_the_home_page() { 
          System.out.println("the user is redirected to the home page"); 
} 
      @Given("user is on the home page of Gmail account") 
      public void user_is_on_the_home_page_of_gmail_account() { 
          System.out.println("user is on the home page of Gmail account"); 
} 
      @And("the user clicks on the sent option") 
      public void the_user_clicks_on_the_sent_option() { 
          System.out.println("the user clicks on the sent option"); 
} 
      @And("checks the sent items") 
      public void checks_the_sent_items() { 
          System.out.println("checks the sent items"); 
} 
      @When("user goes back to the home page") 
      public void user_goes_back_to_the_home_page() { 
         System.out.println("user goes back to the home page"); 
} 
      @Then("the title should be Google") 
      public void the_title_should_be_google() { 
         System.out.println("the title should be Google"); 
} 
      
} 
tags in cucumber code

The above code will provide a backend code for the steps that we have written in the feature file so that the processor will know the exact code to be executed through the step definition for each step that we have created in the feature file. 

How to run specific tags in the test runner 

To Execute the code, we need to create a runner class in the Step Definitions folder. 

For creating the Step Definitions folder, right-click on ‘src/test/java’> go to New> select Folder>Provide the name of the folder>click on Finish 

To create a runner class, we need to right-click on the Step Definitions folder>go to new> select class> provide the name of the class>click on Finish 

New java class

After Creating the test runner class file, we need to add the cucumber annotation and options, also provide the location of the feature files and step definitions, and add the tag in the Cucumber Options section to execute tests belonging to the smoke tag as shown below: 

package StepDefinitions; 
import org.junit.runner.RunWith; 
import io.cucumber.junit.Cucumber; 
import io.cucumber.junit.CucumberOptions; 
@RunWith(Cucumber.class) 
@CucumberOptions(features="src/test/resources/Features", 
glue= {"StepDefinitions"}, 
tags = "@Smoke" 
         ) 
public class TestRunner { 
} 
tags in cucumber code

To run the above code, we need to right-click on the test runner file>select Run As> Select JUnit Test. 

We can also run the test runner class with a combination of tags with the help OR conditions as shown below:

package StepDefinitions; 
import org.junit.runner.RunWith; 
import io.cucumber.junit.Cucumber; 
import io.cucumber.junit.CucumberOptions; 
@RunWith(Cucumber.class) 
@CucumberOptions(features="src/test/resources/Features", 
glue= {"StepDefinitions"}, 
tags = "@Smoke or @Regression" 
         ) 
public class TestRunner { 
} 
tags in cucumber code

To run the above code, we need to right-click on the test runner file>select Run As> Select JUnit Test. It will execute the scenarios that either have smoke or regression. 

We can run a scenario with two tags by using AND condition, as shown below:

package StepDefinitions; 
import org.junit.runner.RunWith; 
import io.cucumber.junit.Cucumber; 
import io.cucumber.junit.CucumberOptions; 
@RunWith(Cucumber.class) 
@CucumberOptions(features="src/test/resources/Features", 
glue= {"StepDefinitions"}, 
tags = "@Smoke and @Regression" 
         ) 
public class TestRunner { 
} 

To run the above code, we need to right-click on the test runner file>select Run As> Select JUnit Test. It will execute the scenarios that have both smoke and regression tags. 

We can run a scenario by using both AND and OR conditions, as shown below:

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

@RunWith(Cucumber.class) 
@CucumberOptions(features="src/test/resources/Features", 
glue= {"StepDefinitions"}, 
tags = "@Smoke or @Regression and @Sanity" 
         ) 
  
public class TestRunner { 
}

To run the above code, we need to right-click on the test runner file>select Run As> Select JUnit Test. It will execute the scenario with either Smoke or Regression tag and Sanity tag. 

We can also skip tags by using AND Not conditions as shown below:

package StepDefinitions; 
import org.junit.runner.RunWith; 
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions; 
@RunWith(Cucumber.class) 
@CucumberOptions(features="src/test/resources/Features", 
glue= {"StepDefinitions"}, 
tags = "@Smoke and not @Sanity" 
         ) 
public class TestRunner { 
} 

To run the above code, we need to right-click on the test runner file>select Run As> Select JUnit Test. It will execute the scenario with the Smoke tag and not the Sanity tag. 

Results 

After each and every execution we should get JUnit results as passed, as shown in the image below: 

Result