Tagging the scenarios in Cucumber

We can tag scenarios using @ symbol in Feature files as shown in below example. Note that we can also tag all the scenarios in a feature file by writing the tag at the beginning of the feature file. We can also associate multiple tags with the same scenario as well. In below example, we have tagged the scenario with 4 tags – selenium, regression, sanity and critical.

@selenium
Feature: My feature

  @regression @sanity @critical
  Scenario: Verify title
    Given I am on the www.softpost.org home page
    Then I verify that title contains tutorials

To execute the scenarios with 2 tags say regression and critical, we can use below syntax.
tags = [“@regression”,”@critical”]

To execute the scenarios with 2 tags say regression or critical, we can use below syntax
tags = [“@regression,@critical”]

To skip the scenarios tagged with tag say sanity, we can use below syntax
Negative tags = [“[email protected]”]

We can also tie the @Before and @After methods with scenarios tagged with specific names.
For example, below before method will be executed only before the scenarios tagged with selenium
@Before(“@selenium”)

Here is the example showing the usage of tags in Cucumber.

Consider below feature file.

@selenium
Feature: My feature

    @regression @sanity @critical
    Scenario: Verify softpost title
    Given I am on the www.softpost.org home page
    Then I verify that title contains tutorials

    @sanity
    Scenario: Verify yahoo title
        Given I am on the www.yahoo.com home page
        Then I verify that title contains tutorials

Now take a look at below Test Class.  After executing below test class, only first scenario will be executed because that scenario is tagged with @sanity and @regression

package org.softpost;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "classpath:abc.feature",
        glue = "classpath:org.softpost",
        tags = {"@sanity","@regression"},
        plugin = "html:target/selenium-reports",
        dryRun = false,
        monochrome = true,
        strict = true,
        snippets = SnippetType.CAMELCASE
)

public class MyTest {
}

Now take a look at below test class. After executing below test, both scenarios will be executed as both are tagged with @sanity tag.

package org.softpost;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "classpath:abc.feature",
        glue = "classpath:org.softpost",
        tags = {"@sanity,@regression"},
        plugin = "html:target/selenium-reports",
        dryRun = false,
        monochrome = true,
        strict = true,
        snippets = SnippetType.CAMELCASE
)

public class MyTest {
}

Now take a look at below test class. After executing below test class, none of the scenarios will be executed as tag is prefixed with ~.

package org.softpost;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "classpath:abc.feature",
        glue = "classpath:org.softpost",
        tags = {"[email protected]"},
        plugin = "html:target/selenium-reports"
)

public class MyTest {
}

You may also like...