In the previous recipe, we learnt how we can send data in Steps, which can be used by the application for processing. The data that we have sent up to this point has been either Strings or integers. But what if we want to send data structures that are more complex and span across multiple lines?
Let's write a Scenario for this functionality—we want to verify whether various users exist or not:
Scenario: Existing user Verification Given user is on Application landing page Then we verify user "Shankar" with password "P@ssword123", phone "999" exists Then we verify user "Ram" with password "P@ssword456", phone " 888" exists Then we verify user "Sham" with password "P@ssword789", phone "666" exists
The problem with this approach of writing Feature files is that Feature files are not expressive enough and there is a lot of repetition.
We are going to add one more Scenario to the login.feature
file, and we are going to use Data Table to send a large set of test data along a Step:
Scenario: Existing user Verification Given user is on Application landing page Then we verify following user exists | name | email | phone | | Shankar | [email protected] | 999 | | Ram | [email protected] | 888 | | Sham | [email protected] | 666 |
Here we have used Data Tables. Tables as arguments to Steps are handy for specifying larger datasets. Let's understand Data Tables in more detail:
Tables as arguments to Steps are handy to specify larger datasets.
The first row of a Data Table is always the header row, where we specify the headers for each column. All the other rows in a Data Table are data rows, which contain the actual data that will be used by the application.
Data tables will be passed to the Step Definition as the last argument.
Data tables are very easy to handle in Step Definition files as well. This is what a sample Step Definition code looks like:
@Then("^we verify following user exists$") public void we_verify_following_user_exists(DataTable userDetails){ //Write the code to handle Data Table List<List<String>> data = userdetails.raw(); System.out.println(data.get(1).get(1)); }
In the preceding code sample, the Data Table has been converted into a List of String and can be handled very easily thereafter.
Note
Data table transformation has been explained in detail in the Transforming Data Tables to parse test data recipe in Chapter 2, Creating Step Definitions.