Passing Data table with key and values to cucumber steps

We can pass the Data table with key and value as shown in below example.

Feature: Simple Datatable feature

  Scenario: Test web title
    Given I am on home page of xyz site
    And I submit the form with below details
      |name      | sagar  |
      |id        | 98989  |
      |address   | mumbai |
    Then I see that form submission is successful

Here is the step definition class for above scenario. Note how we have converted the data table to map and accessed the keys and values.

package org.softpost;

import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;

import java.util.List;
import java.util.Map;

/**
 * Created by Sagar on 13-07-2016.
 */
public class DatatableSteps {


    @Given("^I am on home page of xyz site$")
    public void i_am_on_home_page_of_xyz_site() throws Throwable {
        System.out.println("Navigate to home page \n");
    }

    @Given("^I submit the form with below details$")
    public void i_submit_the_form_with_below_details(DataTable arg1) throws Throwable {
        Map<String,String> m =  arg1.asMap(String.class,String.class);
        System.out.println("\nFilling form with below data\n");
        for( String k : m.keySet())
        {
            System.out.println("Key -> " + k + " Value -> " + m.get(k));
        }
    }

    @Then("^I see that form submission is successful$")
    public void i_see_that_form_submission_is_successful() throws Throwable {
        System.out.println("\nVerify form success message");
    }
}

Here is the output of execution of above feature file.

Navigate to home page

Filling form with below data

Key -> name Value -> sagar
Key -> id Value -> 98989
Key -> address Value -> mumbai

Verify form success message
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.525s

You may also like...