DataTable (asMaps) in Cucumber BDD

Tutorial 9​​ ​​ ​​ DataTable (asMaps)​​ in​​ Cucumber​​ BDD

What you will Learn :

  • DataTable concept

  • Add column names

  • DataTable asMaps

DataTable concept

Please read the​​ DataTable concept​​ explained​​ in our previous tutorial.​​ Please read the previous tutorial before you read this one.

Add column names

Let us add one more scenario to the feature that we created in our previous tutorial. Add one row that will have the column names

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

If you want, instead of writing​​ io.cucumber.datatable.DataTable, you can simply write​​ DataTable​​ (see below)

If you get an error,​​ you have to import the package​​ io.cucumber.datatable

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”

This map will contain key-value pair of type ‘String’

Next, let us import this​​ map​​ from java.util package

So we have

Next, let us print the​​ list​​ ‘userList’

Save the file

Let us now re-run the feature file

Notice​​ the console​​ below​​ and see​​ that our map is getting printed

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

Save the file

Let us now re-run the feature file

Notice the console below and see that​​ entire​​ first map (index 0) is getting printed

Next,​​ how to​​ get a specific value from the first map?

See below. We can use the get(key) method

So we have​​ 

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

Save the file

Remember that, the keys are column headers that we have defined in our feature file, see line#14 below

Let us now re-run the feature file

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!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024