Cucumber Examples Table

Cucumber examples tables help to perform data-driven testing that is important to increase the test coverage. The test case should be run with the different sets of data, for example, numeric values, string values etc. In cucumber data-driven testing, the data has to be pass from the cucumber feature file to the test case.

Cucumber supports Data-Driven testing by the use of the Scenario Outline and Examples.

Scenario outline: The keyword Scenario Outline is used to run the same scenario multiple times with different sets of values.

Example keyword: This is used followed by Scenario outline.  It contains the data that passed to the scenario for execution.

 

Cucumber Example Tables Steps

Let starts with automating the test scenario of login to Gmail account:

Step 1: Write a Test Scenario for login to Gmail account

Feature: Test the login functionality

Scenario Outline: Login with valid username and password

Given user is in login page
When user provides valid “<username>” and “<password>”
Then user should be successfully navigated to home page

Here username and password are the variables. Cucumber understands the above statement syntax and looks for the Examples Keyword in the test to read the Test Data for these parameters.

 

Step 2: Defining Test Data

Examples:

| username | password |
|   user  | password |
|   user  | password123 |
|   user  | [email protected] |

The table has a header row in the first line, corresponding to the variables defined in the Scenario Outline steps. In the Examples table .each argument variable represents a column in the table, separated by “|”. Each line below the header represents an individual run of the test case with the respective data.

 

Step 3: Define Test Runner

There is no change in Test runner class.

package cucumberTest;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;

import cucumber.api.junit.Cucumber;


@RunWith(Cucumber.class)

@CucumberOptions(

        features = "Feature"

       ,glue={"stepDefinition"}

)

public class TestRunner {

}

Right-click on Test Runner class and select Run As -> Junit Test. Cucumber will identify the missing step definition method on the console.

 

Step 4: Implement Step Definition

The missing implementation macro can be copied from the console and added to the step definition file. The regular expression and method arguments are used to deal with data parameters. The data provided in the feature file will be pass to the method using the string argument.

Add a step definition class and paste the code from the console. Complete the code implementation.

package com.qa.stepDefination;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class ExampleDefination {

WebDriver driver;


@Given("^user is in the login page$")
public void user_is_in_the_login_page() throws Throwable {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("https://TEQuality.com");

}

@When("^user provides valid \"([^\"]*)\" and \"([^\"]*)\"$")
public void user_provides_valid_and(String username, String password) throws Throwable {
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("login")).click();

}

@Then("^user gets successfully navigated to home page$")
public void user_gets_successfully_navigated_to_home_page() throws Throwable {
driver.findElement(By.id("menu-main_menu-1")).isDisplayed();
}

}

 

Step 5: Run the test case again from the Test Runner and verify if the test case passes.