Running only selected test with tags in Cucumber java




Hi, all welcome to another of my lockdown blog series posts. In this post, I will show a simple example of how we can execute the selected tests with the use of tags in Cucumber. Let's say we have 10 features written and we want to execute the test related to a single feature, then we should be able to tell our test scripts that only a single test needs to be run.

As the first step, we create a feature file and add multiple features. Let's say we want to test the login and logout functionality. Our feature file should look like below.

Feature: Login functionality feature

In order to do make a holiday tour
As a valid newtours customer
I want to login successfully

@LoginUser
Scenario Outline: login successfully

Given I am in the login page of the newtours application
When I enter valid < username > and < password >
 Then I should be taken to the overview page

Examples:
 | username | password |
 |"abcd123" | "password123" |


 @LogoutUser
Scenario: logout successfully

Given I am logged in
 When I click on logout button
Then I should be logged out of the system 
 
 
In our feature file, there are two features, with two tags added as @LoginUser and @LogoutUser. To enable us to execute only the specific feature what we need to do is to add the attribute tags = {"@LoginUser"}) Within our test runner class as shown below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package newtours;

import io.cucumber.testng.CucumberOptions;
import io.cucumber.testng.*;


    @CucumberOptions(features="/training/training/src/test/java/newtours",
            glue="/training/training/src/test/java/newtours", 
            tags = {"@LoginUser"})
    public class TestRunner {

    }
When executing our test with this approach we should be able to run only the test case that matches the feature we want to execute. However, when running only selected test we may need to use the hooks approach so that dependencies don't fail the test.

The test implementation for the above scenario is as below:

package newtours;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.*;


public class Login {

    private WebDriver driver;
    private WebDriverWait wait;

    @Given("I am in the login page of the newtours application")
    public void i_am_in_the_login_page_of_the_newtours_application() {

        System.setProperty("webdriver.firefox.driver","/training/training");
        driver=new FirefoxDriver();
        driver.get("http://demo.guru99.com/test/newtours/index.php");
    }

    @When("I enter valid {string} and {string}")
    public void i_enter_valid_and(String username, String password) {
        driver.findElement(By.name("userName")).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.xpath("/html/body/div[2]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/" +
                "table/tbody/tr[2]/td[3]/form/table/tbody/tr[4]/td/table/tbody/tr[4]/td[2]/div/input")).click();
    }

    @Then("I should be taken to the overview page")
    public void i_should_be_taken_to_the_overview_page() {
        String actualTitle = driver.getTitle();
        String expectedTitle = "Login: Mercury Tours";
        Assert.assertEquals(expectedTitle,actualTitle);
        driver.close();
    }
}
 
Final note: Lets say we want to run this test as a maven test to enable this what we need to do is to give mvn test -Dcucumber.options="-- tags @LoginUser". This approach will come in handy when we want to run selected test for our regression test.

Comments