Skip to main content

Cucumber Java: Map Data Tables to Class Objects

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;
    }	
}

cucumber with java course on Udemy

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.