Cucumber Data Tables

Cucumber data tables are one of the most commonly used methods for passing test data from feature files to the test scripts. It is used to add multiple parameters in a Step Definition in a tabular form rather than putting all the parameters in the Gherkin statement. This is much easier to read and multiple rows of data can be passed in the same step.

 

Difference between Scenario Outline and Data Table

Scenario Outline

Data Tables

Example keyword is used to define the test data for the Scenario

No keyword is required to define the test data

This works for the whole test

This works only for the single step, below which the test data are defined

Cucumber automatically run the complete test the number of times equal to the number of data in the Test Set

A separate code is needed to run the test data single or multiple times that also for a single scenario

 

How to define Cucumber data tables

In Cucumber, the data tables can be added in two different formats:

Data table with a header

Scenario: Successful Login with Valid Credentials

Given user is in login page
When user provides below credential to login

        | username| Password |
        | testuser| [email protected] |

Then user should be successfully navigated to home page

Data table without a header

Scenario: Successful Login with Valid Credentials

Given user is in login page
When user provides below credential to login
        | testuser_1 | [email protected] |

Then user should be successfully navigated to home page

 

Implement Cucumber Data Tables

Step 1: Create a Test runner class.

package com.qa.runner;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

import cucumber.api.CucumberOptions;


@RunWith(Cucumber.class)

@CucumberOptions(

              features= "E:\\AutomationCode\\DemoBDD\\src\\main\\java\\com\\qa\\features"

              ,glue= {"com.qa.stepDefination"}

              ,monochrome= true

             ,format= {"pretty","html:cucumber-html-report", "json:json_output/cucumber.json"})

public class TestRunner {

}

 

Step 2: Run the scenario(with or without header) without implementing the steps, the following code would be displayed in the Eclipse console window.

Data-Tables

 

Step 3: Complete the Implementation of the missing code. The complete code will look like below:

package com.qa.stepDefination;

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


public class TestDefination {

WebDriver driver;

@Given("^user is in login page$")
public void user_is_in_login_page() throws
Throwable {

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("https://teQuality.com");

}

@When("^user provides below credential to login$")
public void user_provides_below_credential_to_login(DataTable credential) throws Throwable {

//Write the code to handle Data Table
List<List<String>> data = credential.raw();

//This is to get the first data of the set (First Row + First Column)
driver.findElement(By.id("username")).sendKeys(data.get(0).get(0));

//This is to get the first data of the set (First Row + Second Column)
driver.findElement(By.id("password")).sendKeys(data.get(0).get(1));

driver.findElement(By.id("login")).click();

}



@Then("^user should be successfully navigated to home page$")
public void
user_should_be_successfully_navigated_to_home_page() throws Throwable {

driver.findElement(By.id("menu-main_menu-1")).isDisplayed();

}

}

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