Use of Map (Hashmap) with TestNG Dataprovider in Data Driven Testing.

In this article, i will talk about how to use Map (Hashmap) with TestNG DataProvider for Data Driven Testing in Selenium WebDriver. Before to this, let us understand little more about Map.

What is Map in Java?
—> Map is a interface in java and can implements various classes in java like Hashmap, Hashtable & many other. They are the part of collections framework and are used to handle. store data. Map interface represent the mapping in between key and value. What that mean it, in Map or hashmap, value is stored in the form of Key & Value pair. In this article i will try to demonstrate the same.

Map in testng dataprovider selenium webdriver

Syntax to define map –>  Map mapA = new HashMap();

How to add value in map?   –> mapA.put(“key1”, “element 1”);

How to get value from map?  –>  Object value = mapA.get(key);
–> Value from map can also be retrived by using Iterator. Shown in above screenshot.

Now, how to use Map to with TestNG Dataprovider in Data Driven Testing?

Let us suppose my Test data file is located at: C://Users//Prakash//Desktop//TestData.xlsx & it looks as shown in below image –

Test data for Data Driven Testing with Map and TestNG DataProvider

Now, our task is to pass value from row 2 onward to our test case using Map. Have a look on below Selenium Java code –

package demoPackage1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Testclass {

  @Test(dataProvider = "data")
  public void integrationTest(Map<Object, Object> map) {
    System.out.println("-------------Test case started ----------------");
    System.out.println(map.get("UserName"));
    System.out.println(map.get("Password"));
    System.out.println(map.get("DoB"));

    System.out.println("-------------Test case Ended ----------------");

  }

  @DataProvider(name = "data")
  public Object[][] dataSupplier() throws IOException {

    File file = new File("C://Users//Prakash//Desktop//TestData.xlsx");
    FileInputStream fis = new FileInputStream(file);

    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sheet = wb.getSheetAt(0);
    wb.close();
    int lastRowNum = sheet.getLastRowNum() ;
    int lastCellNum = sheet.getRow(0).getLastCellNum();
    Object[][] obj = new Object[lastRowNum][1];

    for (int i = 0; i < lastRowNum; i++) {
      Map<Object, Object> datamap = new HashMap<>();
      for (int j = 0; j < lastCellNum; j++) {
        datamap.put(sheet.getRow(0).getCell(j).toString(), sheet.getRow(i+1).getCell(j).toString());
      }
      obj[i][0] = datamap;

    }
    return  obj;
  }

}

Once you run above test case, Console output will looks like –

[TestNG] Running:
  C:\Users\Prakash\AppData\Local\Temp\testng-eclipse-1161438956\testng-customsuite.xml

-------------Test case started ----------------
user1
pass1
dob1
-------------Test case Ended ----------------
-------------Test case started ----------------
user2
pass2
dob2
-------------Test case Ended ----------------
-------------Test case started ----------------
user3
pass3
dob3
-------------Test case Ended ----------------
PASSED: integrationTest({UserName=user1, DoB=dob1, Password=pass1})
PASSED: integrationTest({UserName=user2, DoB=dob2, Password=pass2})
PASSED: integrationTest({UserName=user3, DoB=dob3, Password=pass3})

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7c30a502: 47 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@7e0ea639: 15 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3ac3fd8b: 47 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@71be98f5: 16 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@511baa65: 15 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms

In Above Console O/P you can clearly see that, test case has been run 3 times with 3 set of test data & is taken from Excel sheet using testNG DataProvider.

 

Refer below video to understand more about How to use map and hashmap along with testng dataprovider –

Hope above example helps !!!

 

For more about Test Automation Framework, refer below links –

Automation Frameworks –

1. Why Automation Framework in selenium?
2. Types of Test Automation Framework in Selenium
         a. Page Object Model (POM)
         b. Data Driven Test Automation Framework
         c. Keyword Driven Test Automation Framework
         d. Hybrid Test Automation Framework
         e. Behavior Driven Development Framework

 

6 Comments

  1. shital

    Reply

    is that possible to pass radio button value by xls .I want pass female/male value selection to radio button by xlsx n using hashmap and dataprovider.

    • Prakash Narkhede

      Reply

      Yes, You can do that.
      In excel, set like True / False.
      Now read this value, use IF Else statement.
      If true, then click

  2. shital

    Reply

    I use your code for login with passing xlsx by using dataprovider. use is one time all values past in fields means in username i get all rows values of username .
    @Test(dataProvider = “data”)
    public void test(Map map)
    {

    //Need to use string variable for retrive value
    String s=map.get(“username”).toString();
    String pass=map.get(“password”).toString();

    driver.findElement(By.xpath(“//input[@type=’email’]”)).sendKeys(s);

    driver.findElement(By.xpath(“//input[@type=’password’]”)).sendKeys(pass);

    what i missed

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories