It looks simple when we just have one, two, or maybe five scenarios in a feature file. However, in real life it does not happen. For each feature under test, we may have 10, 20, or may be more number of scenarios in a single feature file. They may represent different purpose (Smoke test/Regression test), different prospectives (Developer/QA/BA), different status (Ready for execution/Work in progress), etc. How to manage execution for such a mass?
For this, Cucumber has already provided a way to organize your scenario execution by using tags in feature file. We can define each scenario with a useful tag. Later, in the runner file, we can decide which specific tag (and so as the scenario(s)) we want Cucumber to execute. Tag starts with “@”. After “@” you can have any relevant text to define your tag. Let’s understand this with an example.
Suppose, there are two or more scenarios in a feature file. We want to execute only one scenario as part of smoke test. So first thing is to identify that scenario and second is to tag it with “@SmokeTest” text at the beginning of the scenario. Let’s take a deep look at it −
Step 1 − Create a Maven project named as cucumberTag.
Step 2− Create a package named cucumberTag under src/test/java
Step 3− Create a feature file named cucumberTag.feature.
Write the following text within the file and save it. This feature file contains two scenarios where only one has been marked as SmokeTest tag.
Feature − Cucumber Tag
Scenario Outline − Login functionality for a social networking site.
Given user navigates to Facebook
When I enter Username as "<username>" and Password as "<password>"
Then login should be unsuccessful
| username | password | | username1 | password1 | | username2 | password2 |
#following scenario has been tagged as SmokeTest and this should get executed. @SmokeTest
Scenario:
Given user navigates to Facebook
When I enter Username as "<>" and Password as "<>"
Then the user should be redirected to login retry
Step 4 − Create a step definition file.
Select and right-click on the package outline.
Click on ‘New’ file.
Give the file a name such as cucumberTag.java
Write the following text within the file and save it.
package cucumberTag; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import cucumber.annotation.en.Given; import cucumber.annotation.en.Then; import cucumber.annotation.en.When; public class cucumberTag { WebDriver driver = null; @Given("^user navigates to facebook$") public void goToFacebook() { driver = new FirefoxDriver(); driver.navigate().to("https://www.facebook.com/"); } @When("^I enter Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$") public void I_enter_Username_as_and_Password_as(String arg1, String arg2) { driver.findElement(By.id("email")).sendKeys(arg1); driver.findElement(By.id("pass")).sendKeys(arg2); driver.findElement(By.id("u_0_v")).click(); } @Then("^login should be unsuccessful$") public void validateRelogin() { if(driver.getCurrentUrl().equalsIgnoreCase( "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ System.out.println("Test Pass"); } else { System.out.println("Test Failed"); } driver.close(); } @Then("^User should be redirected to login retry$") public void loginRetry() { if(driver.getCurrentUrl().equalsIgnoreCase( "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ System.out.println("Test Pass"); } else { System.out.println("Test Failed"); } driver.close(); } }
Step 5 − Create a runner class file.
Create a runner class named as runTest.java inside the package.
Write the following code.
Save the file.
package cucumberTag; import org.junit.runner.RunWith; import cucumber.junit.Cucumber; @RunWith(Cucumber.class) @Cucumber.Options(format = {"pretty", "html:target/cucumber"}) public class runTest { }
Run the test option.
Right-click and select the option ‘Run as’.
Select JUnit test.
You will observe the following things, when you run this class file.
Facebook opens in a new Firefox web-browser instance.
No value will be provided to the username and the password field.
Login will be clicked.
Login retry page will be loaded.
There is no limit in defining tags within the feature file. Based on your need, you can derive tags to be used and scenarios to be executed.
There are mainly two types of tag −
Default tag − Default tag has their predefined meanings. Example @Dev,@Ignore
Custom tag − Custom tag provides you full flexibility to choose appropriate text for defining your tag.
Tag can also be defined at a feature level. Once you define a tag at the feature level, it ensures that all the scenarios within that feature file inherits that tag. Depending on the nature of the scenario, we can use more than one tag for the single feature. Whenever Cucumber finds an appropriate call, a specific scenario will be executed.
Cucumber also provides a way to inverse the choice of tags. Consider that out of 25 defined scenarios, 10 are marked as smoke test. We are required to execute only regression test scenarios.
For this, we can use “~” in JUnit runner class to exclude smoke test scenario. It will look like the following.
@RunWith(Cucumber.class) @Cucumber.Options(format = {"pretty", "html:target/cucumber"}, tags = {"~@SmokeTest"}) public class runTest { }
While defining multiple tags, we can also define logical or/and logical and operation.
Defining logical or in runner class − @dev,@wip − It says that scenarios matching any of this tag needs to be executed.
Defining logical or in runner class − [@dev,~@wip] − It says that scenarios matching both these tag needs to be executed.