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
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 |
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 –
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).
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.