• Skip to main content
  • Skip to primary sidebar

AutomationTestingHub

Selenium, Appium, Cucumber and more !!!

Working with Cucumber Data table

May 17, 2018 by anish Leave a Comment

Cucumber data table is one of the most commonly used methods for passing test data from feature files to your test scripts. In our previous article, you saw how we can pass string and numeric data from feature files, which Cucumber simply treats as variables.

With Cucumber data tables, you can pass parameters from feature files in tabular format. And you can then use this data in step definition methods in the form of Lists and Maps.

Different ways of representing Cucumber data tables

In Cucumber, you can add data tables in two different formats –

  • Data table with a header
  • Data table without a header

Cucumber data table - with and without header

Here, a header means to have a top row which tells what type of data you have. You will get more clarity about this when you have a look at the examples of both these types of data tables.

Let’s first start with cucumber data table without a header.

Cucumber data table without header

Let us take the same login example that we had covered in our previous article on cucumber data driven testing examples. In that article, we had added the cucumber scenario like this –

Scenario: Login to application
Given I open my application
And I login with credentials "admin" and "pass1234"

You can write the same scenario in data table format like this –

Scenario: Login to application
Given I open my application
And I login with following credentials 
| admin | pass1234 |

Cucumber data table example without header

Notice the last line in the above example. Here, admin and pass1234 are the parameters, that we have added between the pipe ( | ) symbols. This is what we refer to as a data table in cucumber.

Also, please note that even though, admin and pass1234 are string parameters, we don’t specify them in double quotes inside the data table. This is actually handled in the step definition method, when we convert the Data table into Maps or List (more on this later in this article).

Let us now look at the step definition method for this data table step.

Step definition method for Cucumber data table without header

When you run this cucumber scenario from test runner class, you will see that the Eclipse console displays the missing step definition method like this –

Step definition method - Cucumber data table List example

The important points to note here is that Cucumber uses DataTable as method argument, and provides four different ways in which you can access this DataTable methods in your code. In this example, since we have only one line in the data table, List< YourType > would be the best suited candidate.

Also, since the data table parameters that we have are of type String, hence we will use List< String > to fetch the data. If the data is numeric, you will then need to use List< Integer >.

The code to access the DataTable values is given below –

@When("^I login with following credentials$")
public void i_login_with_following_credentials(DataTable dt) {
	List list = dt.asList(String.class);
	System.out.println("Username - " + list.get(0));
	System.out.println("Password - " + list.get(1));
}

With the above code, you can get the DataTable values in standard Java list which you can then use it in your code.

Let us now have a look at the data table where we add the header as well.

Cucumber data table with header

In the previous example, there were only two columns in the data table. So, when someone reads the cucumber scenario, it is easy for them to figure out that the first column would be user name and the second one will be password. But what happens when you have multiple columns in the data table (as in below scenario)?

Scenario: Create new account in Facebook
Given I open Facebook URL
And fill up the new account form with the following data
| Test FN | Test LN | 0123123123 | Pass1234 | 01 | Jan | 1990 | Male |

Now if someone reads this scenario, don’t you think that it will be little bit more difficult to figure out what some of these values mean? For such cases, we add the header as well to the Data table, so that we can improve the readability of our cucumber scenarios and scripts. After adding the header, the scenario will look like this (the readability here is better compared to the previous example) –

Scenario: Create new account in Facebook
Given I open Facebook URL
And fill up the new account form with the following data
| First Name | Last Name | Phone No   | Password | DOB Day  | DOB Month  | DOB Year  | Gender |
| Test FN 	 | Test LN 	 | 0123123123 | Pass1234 | 01 		| Jan 		 | 1990 	 | Male   |

When you run this cucumber scenario, you will see that the structure of the missing step definition method is the same as with the previous example (data table without header).

Cucumber data table step definition method with header

Since this Data table contains a header as well, the best way to access its content is using List< Map< K, V > > as shown below –

@Given("^fill up the new account form with the following data$")
public void fill_up_the_new_account_form_with_the_following_data(DataTable dt) {
    List> list = dt.asMaps(String.class, String.class);
    System.out.println(list.get(0).get("First Name"));
    System.out.println(list.get(0).get("Last Name"));
    System.out.println(list.get(0).get("Phone No"));
    //Fetch remaining data using same logic
}

Note: Please make sure that you are comfortable with the way this code works, as this is something that you will need to use a lot in your test cases.

Let’s now have a look at the last example we have for the data table.

Data table with header (and multiple rows)

Till now, both the previous examples that you saw dealt with only single row of actual data. In this example, let’s check out a scenario where you have multiple rows of data as well. Let’s use the same Facebook account creation example and extend it for multiple users.

Scenario: Create multiple new accounts in Facebook
Given I open Facebook URL and create new accounts with below data
| First Name | Last Name | Phone No   | Password | DOB Day  | DOB Month  | DOB Year  | Gender 	|
| Abc FN 	 | Abc LN 	 | 0123123123 | Pass1234 | 01 		| Jan 		 | 1990 	 | Male   	|
| Def FN 	 | Def LN 	 | 0456456456 | Abcd1234 | 01 		| Feb 		 | 1990 	 | Female   |
| Xyz FN 	 | Xyz LN 	 | 0789789789 | Pass2018 | 01 		| Mar 		 | 1990 	 | Female   |

In this case also, when you run the cucumber scenario, the structure of missing step definition method will be same as all the previous examples. Only difference here is you will have to use a loop or index-based logic to fetch values from different rows of data.

The below example shows how you can fetch first names and last names from all the three rows using a for loop.

@Given("^I open Facebook URL and create new accounts with below data$")
public void i_open_Facebook_URL_and_create_new_accounts_with_below_data(DataTable dt) {
	List> list = dt.asMaps(String.class, String.class);
	for(int i=0; i 	 	 	 	 	 	 	

You can also use List< List< String >> to fetch the values from this Cucumber data table. An example for this approach is given below –

@Given("^I open Facebook URL and create new accounts with below data$")
public void i_open_Facebook_URL_and_create_new_accounts_with_below_data(DataTable dt) {
	List 	 	 	 	 	 	 	> list = dt.asLists(String.class);
	for(int i=1; i 	 	 	 	 	 	 	

With this, we have now covered all the different combinations using which you can create a cucumber data table. Give it a try on your own and see how it goes. If you have any queries with any of the steps we have mentioned above, then please contact us using the comments section.

Filed Under: Cucumber

Primary Sidebar

Automation Testing Hub

Tutorials Guide

Write test scripts in BDD format using our Cucumber Selenium Tutorial

Use our Appium Tutorial Series to setup Appium from scratch

Categories

  • Android
  • Appium
  • Cucumber
  • Java
  • Playwright
  • Selenium
  • Tosca

Recent Posts

  • Appium Inspector – Download and Install
  • How to download Chromedriver for Selenium
  • Run Playwright tests on different browsers
  • Playwright Automation Testing Tutorial
  • Selenium IDE Chrome Download

Recent Comments

  • Run Playwright tests on different browsers - AutomationTestingHub on Playwright Automation Testing Tutorial
  • Confluence: CPSSoft Test Framework on Appium Inspector – Download and Install
  • Playwright Automation Testing Tutorial - AutomationTestingHub on Download and Install – Node.js and NPM
  • Chrome浏览器在使用Java的Appium中无法启动。 | JAVA语言的经验与见解 on 2 Ways to find appPackage and appActivity name of your App
  • Appium Inspector Tutorial - AutomationTestingHub on Download and Install Appium 2 Server

Archives

  • January 2024
  • July 2023
  • March 2023
  • February 2023
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016

Latest Selenium 3 Guide

Are you using the latest version of Selenium WebDriver? It provides lot better support for the latest browser versions. Check it out here - Setup Latest Selenium WebDriver

Categories

  • Android
  • Appium
  • Cucumber
  • Java
  • Playwright
  • Selenium
  • Tosca

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in

Go to mobile version