DISCLAIMER: It is assumed that reader has some experience with Cucumber test tool.
The problem
As test scenarios in Gherkin become bigger and bigger, it is reasonable to use tables in order to represent complex test data.
How Cucumber handles tables?
Tables in Cucumber feature files are represented by using the pipeline “|” sign.
You can for any type for tables: with multiple rows and multiple columns.
Here is the example scenario with different forms of table:
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.
How to use custom properties?
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:
will be converted to:
or another example table:
can be represented as:
asLists() converts table to a simple list of lists (rows with values)
For example:
it will be converted to:
or in other sample:
the result will be:
asList() converts all table elements into a list
Consider the following table:
and result will be:
or another sample:
will be converted to:
The asMap() works only two column tables and converts data table to map.
E.g.:
it will be converted to:
But if in our example column should be mapped to value - table can be modified using transpose() method.
As a result:
Conclusion
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 - as it more flexible solution.