Cucumber Datatable Tutorial

When we pass in parameters into a Step Definition then we can only pass in a single value however we can overcome this issue and pass in chunks of data via Cucumber DataTable. We can use Cucumber DataTable to pass in an array of information in a single step.

Using Cucumber Datatable

Let’s have a quick look at how we can pass in a single value via a test step in a Feature file and into a Step Definition method:

Feature File:

When I click on "Cucumber Java Tutorial"

Step Definition:

@When("^When I click on \"([^\"]*)\"$")
public void iClickOn(String link){
//code here
}

We can see that in the above Feature file we are passing in a single String value from the Feature file and into the Step Definition method. However in this current context we are not able to pass in a chunk of data. To put it slightly differently when using Cucumber Parameters it is not possible to pass in ‘arrays’ of information. This is where we can apply Cucumber DataTable policy. Cucumber DataTable can be used to pass in arrays of information in a single step. Here is an example of where I use Cucumber DataTable:

Feature File:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com
  Scenario: To check that tutorial pages have loaded
    Given I navigate to TheTestRoom.com
    When I navigate to "Java WebDriver Tutorial" page
    Then the following links should be visible
      | Java Cucumber Tutorial |
      | Git Tutorial           |

Step Definition:

    @Then("^the following links should be visible$")
    public void the_following_links_should_be_visible(List linkData) throws Throwable {
        // List, List>, List> or Map.
        for (int i = 0; i < linkData.size(); i++) {
            System.out.println(linkData.get(i));
        }
    }

Looking at the above Feature file you can see that we are now passing in a table of data into a single step. If you look at the matching Step Definition then you can see that we are capturing that information in a List i.e. an array. When we pass in data in a Feature file in the above format then this allows us to pass in rows of information which in turn can be captured in an array in a Step Definition. This data in a Step Definition method is actually captured as a DataTable (a collection container in Cucumber) however we can cast it to be captured as a List or Map instead as part of the method signature. After we cast it to either a List of a Map, this makes it easier to work with in our Step Definition methods.

And that's it folks. In this tutorial we have learned how to pass in arrays of data from a Feature file and into a Step Definition method. We have seen how powerful this simple technique is as this gives another option to passing data in 'bulks' as opposed to single values.

Video Tutorial on YouTube

cucumber datatable

Direct Video Link

Mo

I am a passionate tester, father, husband and gamer. I love to upload videos on my YouTube channel, write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook - Google Plus

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to upload videos on my YouTube channel, write blogs about Software Testing and generally contribute back to the Software Testing world.

Leave a Reply