Cucumber | Data Table | Access & Usage
Assumption here — we have good understanding of following tool / terminologies.
- Cucumber tool (Basic Idea)
- BDD (Behavior Driven Development)
- Gherkin Syntax / Feature File
What is Data table in Cucumber?
Data Tables
are handy for passing a list of values to a step definition. Cucumber provides a rich API for manipulating tables from within step definitions.
- Data Tables are also used to handle large amount of data. Tables in Cucumber feature files are represented by using the pipeline “|” sign.
- We can use data table under scenario OR scenario Outline.
Here is the example scenario with different forms of table:
----------------------EXAMPLE-----------------------------@tag @wip
Feature: Learn data tableScenario: Cucumber Data Table
Given Table with two rows and multiple columns
| columnA | columnB | columnC | columnD |
| value1 | value2 | value3 | value4 |
| value5 | value6 | value7 | value8 |
And Table with only two rows and two columns
| username | admin |
| password | admin123 |
And Table with multiple rows and one column
| columnNameG |
| row1 |
| row2 |
| row3 |----------------------EXAMPLE-----------------------------@tag @wip
Feature: Learn data tableScenario outline: Cucumber Data Table
Given Table with example
| FirstName | <FirstName> |
| MiddleName | <MiddleName> |
| LastName | <LastName> |Examples:
| FirstName | MiddleName | LastName |
| Priyank | B | Shah |
| Mansi | P | Shah |
What about tables in steps?
When Cucumber tool is parsing the feature file, it converts all tables in special data type — DataTable. As it can be seen below — input parameter for the step definitions is DataTable.
@Given("Table with two rows and multiple columns")
public void table_with_two_rows_and_multiple_columns(DataTable dataTable) {
}@Then("Table with only two rows and two columns")
public void table_with_only_two_rows_and_two_columns(DataTable dataTable) {
}@Then("Table with multiple rows and one column")
public void table_with_multiple_rows_and_one_column(DataTable dataTable) {
}@Then("Table with example")
public void table_with_example(DataTable dataTable) {
}
Possible ways to convert DataTable
Take a closer look at possible ways to convert DataTable into more useful data types: Lists or Maps.
— — — — — — — — — asMaps(): — — — — — — — — -
- converts table into list of maps, where columns (from the first row) are mapped to values in each row
For example the following table:
Then Table with two rows and multiple columns
| columnA | columnB | columnC | columnD |
| value1 | value2 | value3 | value4 |
| value5 | value6 | value7 | value8 |
will be converted to:
[[columnA:value1, columnB:value2, columnC:value3, columnD:value4], [columnA:value5, columnB:value6, columnC:value7, columnD:value8]]
For reference, sudo code: (katalon, groovy)
@Then("Table with two rows and multiple columns")
public void table_with_two_rows_and_multiple_columns(DataTable dataTable) {println "using Maps ------------------------"
List<Map<String,String>> data = dataTable.asMaps(String.class, String.class)
println data
for (row in data)
{
println row.get('columnA')
println row.get('columnB')
println row.get('columnC')
println row.get('columnD')}
}
or another example table:
| columnNameG |
| row1 |
| row2 |
| row3 |
will be converted to:
[[columnNameG:row1], [columnNameG:row2], [columnNameG:row3]]
— — — — — — — — — — asLists(): — — — — — — — — -
- converts table to a simple list of lists (rows with values)
For example the following table:
Then Table with two rows and multiple columns
| columnA | columnB | columnC | columnD |
| value1 | value2 | value3 | value4 |
| value5 | value6 | value7 | value8 |
will be converted to:
[[columnA, columnB, columnC, columnD], [value1, value2, value3, value4], [value5, value6, value7, value8]]
For reference, sudo code: (katalon, groovy)
@Then("Table with two rows and multiple columns")
public void table_with_two_rows_and_multiple_columns(DataTable dataTable) {println "using Lists ------------------------"
List<List<String>> dataLs = dataTable.asLists(String.class)
println dataLs
for (int i ; i < dataLs.size() ; i++)
{
for (int j=0 ; j < dataLs.get(i).size() ; j++)
{
println dataLs.get(i).get(j)
}
}
}
or another example table:
| columnNameG |
| row1 |
| row2 |
| row3 |
will be converted to:
[[columnNameG], [row1], [row2], [row3]]
— — — — — — — — — asList(): — — — — — — — — — —
- converts all table elements into a list
For example the following table:
Then Table with two rows and multiple columns
| columnA | columnB | columnC | columnD |
| value1 | value2 | value3 | value4 |
| value5 | value6 | value7 | value8 |
will be converted to:
[columnA, columnB, columnC, columnD, value1, value2, value3, value4, value5, value6, value7, value8]
For reference, sudo code: (katalon, groovy)
@Then("Table with two rows and multiple columns")
public void table_with_two_rows_and_multiple_columns(DataTable dataTable) {println "using List ------------------------"
List<String> dataL = dataTable.asList(String.class)
println dataL
for (int i ; i<dataL.size(); i++)
{
println dataL.get(i)
}
}
}
or another example table:
| columnNameG |
| row1 |
| row2 |
| row3 |
will be converted to:
[columnNameG, row1, row2, row3]
— — — — — — — — — — asMap(): — — — — — — — — — -
- works only two column tables and converts data table to map.
For example the following table:
And Table with only two rows and two columns
| username | admin |
| password | admin123 |
will be converted to:
[username:admin, password:admin123]
For reference, sudo code: (katalon, groovy)
@Then("Table with multiple rows and one column")
public void table_with_multiple_rows_and_one_column(DataTable dataTable) {
println "using Map-------------"
Map<String, String> cred = dataTable.asMap(String.class, String.class)
println cred
println 'username: ' + cred.get('username')
println 'Password: ' + cred.get('password')
println "using Map Iteration (Get all keys) -------------"
cred.keySet().forEach{key -> println key}
println "using Map Iteration (Get all keys and values) ---------"
cred.forEach{key,value ->
println "key: "+ key
println "value: " + value
}
}
Scenario Outline example the following table:
Scenario: Cucumber Data Table
And Table with example
| FirstName | <FirstName> |
| MiddleName | <MiddleName> |
| LastName | <LastName> |Examples:
| FirstName | MiddleName | LastName |
| Priyank | B | Shah |
| Mansi | P | Shah |
For reference, sudo code: (katalon)
@Then("Table with multiple rows and one column")
public void table_with_multiple_rows_and_one_column(DataTable dataTable) {println "using Map Object------------"
Map<String,String> dataM = dataTable.asMap(String.class, String.class)
println dataM
println dataM.get('FirstName')
println dataM.get('MiddleName')
println dataM.get('LastName')
}
}
— — — — — — —My Experience with Data table: — —
As it can be seen — tables in cucumber can be represented in the various ways. It’s up to the user of the Cucumber to choose which way is best. From my practice I can recommend to use tables as maps/map — as it more flexible solution.