DataTable_asLists_in_Cucumber_BDD

Tutorial 8​​ ​​ ​​ DataTable (asLists)​​ in​​ Cucumber​​ BDD

What you will Learn :

  • DataTable concept

  • DataTable asLists

DataTable concept

We will​​ now​​ study about Data Table concept in Cucumber BDD.​​ We can parameterize a particular step in our feature file. For example, I might want to do​​ a​​ couple of user registrations​​ with multiple users.​​ Now, to register a user, we​​ might need to enter the user’s firstname, lastname, etc.​​ 

Consider the below block​​ to register a single user. It would become cumbersome if we repeat the same block again and again​​ to register​​ all the users. ​​ 

To make this simpler, we will use the DataTable feature in cucumber and write as follows. We write the pipe separated user data and notice that the feature file is more readable now. The file contains the data of​​ 3 users.

Save the file

Let us now run this file to generate the step definition methods

Copy the code snippets

Paste them in a step definition file

Import the missing packages and remove the​​ comments plus​​ exceptions.​​ If you notice, for the second method, the data is coming in the form of dataTable.​​ Also, it is coming from library​​ io.cucumber.datatable.DataTable

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

​​ 

However you have to import the​​ same​​ package​​ io.cucumber.datatable

So let us do that

So when the 2nd​​ step @When will be executed, the​​ multiple​​ data​​ sets that you have given in the feature file will be coming to​​ this particular dataTable

Next, let us write simple SOPs for first and third step

Next, let us now concentrate on second step @When, see below

DataTable asLists

As you can see below, there is a method​​ asLists()​​ available

Let us use that method

Now, what kind of list you want to get? We want to get ‘String’ type of list, because the dataset in the feature file is in string format. So we will simply write String.class

Now,​​ as you can see below, this asLists() method returns​​ “List of List of object”. Again, this list will again be of type ‘String’

So we can say

Next, let us import this list from java.util package

So we have


Next, we will create a ‘for’ loop​​ so that we can traverse this particular userList.​​ 

So, we want to traverse through a​​ ‘List of string’ viz List<String>​​ 

We will next print the value of ‘li’​​ 

It means, print each and every list. So in the first iteration, we will be getting the first list viz first set of data

In the second iteration, we will get

And so on…

Save the step definition file.

Let us now re-run our feature file

See the console. When the user is on registration page, we are entering the 3 data set of values

So like this way, you can use the concept of datatable in cucumber.​​ The datatable can be applied to any type of step Given/When/Then etc..

Let us create a RunnerTest file and run it as Junit test

See the console o/p below. We get exactly the same thing

Reference
You can further refer​​ www.baeldung.com/cucumber-data-tables​​ 

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 1st Nov 2024