Cucumber Java: Map Data Tables to Class Objects

Profile picture for user devraj

There is one easier ways to access your data than DataTable. For instance you can create a Class-Object and have Cucumber map the data in a table to a list of these.

Feature File

#Login with multiple users
  @loginusersclassobjects
  Scenario: Verify Login with multiple users
    When I Logged in with users using class objects
      | UserName                    | Password |
      | goswami.tarun77+7@gmail.com | test1234 |
      | mahuja                      |   234567 |
      | ssharma                     |   975454 |   

Step Definition

@When("^I Logged in with users using class objects$")
public void user_enters_testuser_and_Test(List<Credentials>  usercredentials) throws Throwable
{
    //Write the code to handle Data Table
    for (Credentials credentials : usercredentials)
    {
        driver.findElement(By.id("log")).sendKeys(credentials.getUsername());
        driver.findElement(By.id("pwd")).sendKeys(credentials.getPassword());
        driver.findElement(By.id("login")).click();
    }		
}

Class Objects

package stepDefinition;

public class Credentials 
{
    private String username;
    private String password;

    public String getUsername()
    {
        return username;
    }

    public String getPassword()
    {
        return password;
    }	
}