Skip to main content

Maps in Data Tables Cucumber

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. 

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.