Multiple Test Data using Data table in Cucumber

a couple of years ago

When we have multiple test data to pass in a single step of a feature file, one way is to pass multiple parameters and another way is to use Data Tables.

Data Tables is a data structure provided by cucumber. It helps you to get data from feature files to Step Definitions. Its a bridge between feature file and Step Definition to pass values to the parameters.

Data can be passed as a large number of data, as One-Dimensional data, as two-dimensional data and also in the form of key-value pair.

Using Data Table as a List:

Let us consider a scenario: I want to Sign up to Facebook (http://facebook.com) where we have to pass data like Firstname, Surname, Mobile Number, Password, Date Of Birth, Gender.

In the feature file, these data parameters are passed using a separator (|)

Let us see the feature file below where we are covering signup to facebook.

Feature File:

Feature: Signup to facebook
  In this feature we will signup to facebook website.

  Scenario: Signup to Facebook
    Given I navigate to Facebook page
    Then I enter all required details
      | Saurabh                 |
      | Dhingra                 |
      | saurabh.d2106@gmail.com |
      | password                |
      | 21-06-1989              |
      | Male                    |
    Then I Clicked on the signup button

Step Definition File:

package steps;

import java.util.List;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;

public class FacebookSteps {
	WebDriver driver;

	String url = "https://www.facebook.com";

	@Given("^I navigate to Facebook page$")
	public void navigateToFacebook() throws Throwable {
		System.setProperty("webdriver.chrome.driver",
				"C:/Users/Saurabh Dhingra/workspace/libs/chromeDriver36/chromedriver.exe");

		driver = new ChromeDriver();

		driver.manage().window().maximize();

		driver.manage().deleteAllCookies();

		driver.get(url);
	}

	@Then("^I enter all required details$")
	public void enterAllDetails(DataTable testData) throws Throwable {
		List<String> details = testData.asList(String.class);
		driver.findElement(By.name("firstname")).sendKeys(details.get(0));
		driver.findElement(By.name("lastname")).sendKeys(details.get(1));
		driver.findElement(By.name("reg_email__")).sendKeys(details.get(2));
		driver.findElement(By.name("reg_passwd__")).sendKeys(details.get(3));
		Select date = new Select(driver.findElement(By.id("day")));
		Select month = new Select(driver.findElement(By.id("month")));
		Select year = new Select(driver.findElement(By.id("year")));
		String[] dob = details.get(4).split("-");
		date.selectByVisibleText(dob[0]);
		month.selectByVisibleText(dob[1]);
		year.selectByVisibleText(dob[2]);

		String genderXpath = String.format("//label[text()='%s']//preceding-sibling::input[@type='radio']",
				details.get(5));

		driver.findElement(By.xpath(genderXpath)).click();
	}

	@Then("^I Clicked on the signup button$")
	public void clickOnSgnUpButton() throws Throwable {
		driver.findElement(By.name("websubmit")).click();
	}

	@Then("^I enter username and password$")
	public void enterUsernameAndPassword(DataTable testData) throws Throwable {

		Map<String, String> data = testData.asMap(String.class, String.class);

		driver.findElement(By.id("email")).sendKeys(data.get("username"));
		driver.findElement(By.id("pass")).sendKeys(data.get("password"));

	}

	@Then("^I Clicked on the login button$")
	public void clickOnloginButton() throws Throwable {
		driver.findElement(By.id("u_0_2")).click();
	}

	@Then("^I verified that login is successful$")
	public void verifyLoginIsSuccessful() throws Throwable {
		System.out.println(driver.getTitle());
	}

}

In the above scenario, the DataTable is sending the list of data in the one-dimensional form. We received the data in a list of String and passed in order.

Using Data Table in key-value pair:

Let us consider a scenario: I want to login to Facebook (http://facebook.com) with username and password. We will pass the username and password as parameters using Data Tables.

Feature File:

Feature: Login to facebook
  In this feature we will login to facebook website.

  Scenario: Login to facebook
    Given I navigate to Facebook page
    Then I enter username and password
      | username | saurabh.d2106@gmail.com |
      | password | password                |
    Then I Clicked on the login button
    Then I verified that login is successful

Step Definition File:

package steps;

import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;

public class LoginFacebookSteps {
	WebDriver driver;

	String url = "https://www.facebook.com";

	@Given("^I navigate to Facebook page$")
	public void navigateToFacebook() throws Throwable {
		System.setProperty("webdriver.chrome.driver",
				"C:/Users/Saurabh Dhingra/workspace/libs/chromeDriver36/chromedriver.exe");

		driver = new ChromeDriver();

		driver.manage().window().maximize();

		driver.manage().deleteAllCookies();

		driver.get(url);
	}

	@Then("^I enter username and password$")
	public void enterUsernameAndPassword(DataTable testData) throws Throwable {

		Map<String, String> data = testData.asMap(String.class, String.class);

		driver.findElement(By.id("email")).sendKeys(data.get("username"));
		driver.findElement(By.id("pass")).sendKeys(data.get("password"));

	}

	@Then("^I Clicked on the login button$")
	public void clickOnloginButton() throws Throwable {
		driver.findElement(By.id("u_0_2")).click();
	}

	@Then("^I verified that login is successful$")
	public void verifyLoginIsSuccessful() throws Throwable {
		System.out.println(driver.getTitle());
	}

}

In the above example, the values are passed as the key-value format. Username – saurabh.d2106@gmail.com, and Password – password. While receiving the data we converted the DataTable data structure into the Map using a method called testData.asMap(String.class, String.class). Argument mention the data type.

For any questions, queries or comments feel free to write to us at support@qatechhub.com or saurabh@qatechub.com. Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: