Maps in Data Tables Cucumber

Profile picture for user devraj

We can use Maps along with Data Table. Consider below example where you have to check whether user is valid or invalid based on its user information.

Feature File

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

Step Definition

@When("I Logged in with users")
public void i_Logged_in_with_users(DataTable dataTable) 
{
    for (Map<String, String> data : dataTable.asMaps())
    {
        WebElement userName = driver.findElement(By.cssSelector("input[id='email']"));
        WebElement password = driver.findElement(By.cssSelector("input[id='passwd']"));
        userName.clear();
        userName.sendKeys(data.get("UserName")); 
        password.clear();
        password.sendKeys(data.get("Password"));
        driver.findElement(By.cssSelector("button[id='SubmitLogin']")).click();

        try
        {
            if(driver.findElement(By.xpath("//h1[text()='My account']")).isDisplayed())
            {
                System.out.println(data.get("UserName")+" is valid User");
            }
        }
       catch(Exception e)
       {
           System.out.println(data.get("UserName")+" is invalid User");
       }
       driver.manage().deleteAllCookies();
       driver.navigate().refresh();
    }	
}

In above example, If the user is valid "My Account" page will be displayed and User is valid will displayed otherwise invalid user will display.