i

Selenium Step By Step Guide

Data Provider with Hashtable

We have seen how to use the data-provider with the help of an excel file. Now let us discuss how to use the Data-provider with the help of Hashtable.

We can create a TestNG test class where we can keep the implementation of the Data-provider with the help of HashTable:

package SeleniumTest.SeleniumTest;

import java.util.Hashtable;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class DataProviderHashTable {

      @DataProvider(name="data")

    public static Object[][] credentials() throws Exception //throws Exception

    {  

    int cols=2;

    int rows=2;

    Object[][] data=new Object[rows][1];

    Hashtable<String,String> table=null;

        for(int i=1; i<=rows;i++)

        {          

            table = new Hashtable<String,String>();

                for(int j=0 ; j<cols; j++)

                {

                try {

                    table.put(ExcelReading.getCellData(0, j), ExcelReading.getCellData(i, j));

                } catch (Exception e) {                 

                    e.printStackTrace();

                }

                }

  

                data[i-1][0] = table;

        }         

        return data;

    }

@Test(dataProvider="data")

    public void testCase(Hashtable<String,String> data)

    {

// Implement the method as per requirement

}

}

We can now implement the test method as per our requirement and use the data from the data-provider.