Tutorial_8_DataTable_asLists_in_Cucumber.docx
Tutorial_8_DataTable_asLists_in_Cucumber.docx
The first row will behave like a column for us. Other respective rows will behave as data
for us.
Save the feature file.
Let us now run this file to generate the step definition methods for the new scenario
Copy the code snippets
Paste them in the same step definition file that was created in previous tutorial, see
below
Remove the comments plus exceptions
DataTable asMaps
As you can see below, there is a method asMaps() available
Let us use that method
Now, we know that, a map contains values on the basis of key viz key:value pair. Also, a
map contains unique keys.
In our current case, both key and value would be of ‘String’ type
Now, as you can see below, asMaps() method returns “List of Map”
So we have
Next, let us print the list ‘userList’
Let us see how this list looks like. Just select and copy the the entire list from console.
Paste it in notepad and arrange it a little bit:
So we have a list containing 3 maps. Each map has key/value pairs. For example, in the
first map, one of the keys is ‘FirstName’ and the corresponding value is ‘Tom’. Another
key is ‘Zip’ and value is 12345, and so on.
Also, each and every row value will be mapped to respective column headers
(FirstName, LastName etc).
Now, a list is order based, it maintains the indexing or the order. The indexing starts from
0. So see below. From this userList, let us try to print the first map. The first map has
index 0 and hence we can say userList.get(0)
So we can say
Let us now provide a specific key to get its value, see below. We will now try to get the
value of key ‘FirstName’ from the 0th row or 1st map
Notice the console below and see that the value ‘Tom’ gets printed
This value is correct as seen in below
Similarly you can print the values of other keys. These keys behave like columns for us.
Comment the 2 sops
Next, we will create a ‘for’ loop so that we can traverse this particular userList.
So, we want to traverse through a ‘Map of string’ viz Map<String, String>
We will now print the values of all the keys from all the 3 maps, see below
Save the file
Let us now re-run the feature file
Notice the console below and see that all the values from all the maps get printed
So this is how we can use DataTable asMaps feature.
Thank you for reading!