Thursday, August 2, 2018

Parameterization in Cucumber - Data Tables

To pass a group of data to a single feature step, you can use Data Tables in Cucumber feature definition.

Scenario: Successful Login with Valid Credentials
    Given User is on Home Page
    When User Navigate to LogIn Page
    And User enters Credentials to LogIn
        | user1 | p2ssword |
        | user2 | p2ssword |
    Then Message displayed Login Successfully

Just use "|" mark to separate different column and each line as a table row.

@When("^User enters Credentials to LogIn$")
public void user_enters_credentials_to_login(DataTable credentials) {
    List<List<String>> data = credentials.asLists();
    driver.findElement(By.id("xv_username")).sendKeys(data.get(0).get(0));
    driver.findElement(By.id("header-myairnz-password")).sendKeys(data.get(0).get(1));
    driver.findElement(By.cssSelector("input[name='login']")).click();
}

When it is passed to step definition method at runtime, you may access this data table through DataTable parameter.


1) Data tables as List<List<String>>
     You can call "credentials.asLists();" to get a List<List<String>> object to access all the data in the data table, just like a two dimension array.

2) Data tables as List<Map<String, String>>
    Or you can call "credentials.asMaps() to get a List<Map<String, String>> object to access all the data in the data table, but make sure you have added a key row in the first row in the data table.

     | Username| Password|
     | user1 | p2ssword |
     | user2 | p2ssword |

    To access Map list,

List<Map<String, String>> data =  dataTable.asMaps();
username = data.get(0).get("Username");
password = data.get(0).get("Password");

Check the other interface in DataTable, you can find other ways you wanted.

No comments:

Post a Comment