0

I am trying to fetch the values from excel sheet using data provider with hash map.For now I can read the entire rows from the excel sheet.The excel sheet has 3 columns "TestcaseId","Testdata","scriptname".Here I need to pass the script name as a paramater to the test method from data provider.Please help me how to achieve this.Below is the dataprovider code using hashmap

@DataProvider(name="cbndataprovider")
    public static Iterator<Object[]> cbntestdata() throws IOException
    {
        List <Object[]> alist = new ArrayList<Object[]>();
        OriginalExcelRW Excel = new OriginalExcelRW("F:\\anand_acer\\selenium\\cbnindia1\\Test_Data_Sheet.xlsx");
        XSSFSheet s = Excel.Setsheet("Test_Data");
        int rowcount = s.getLastRowNum();
        for(int i =1;i<=rowcount;i++)
        {
        Object[] obj = new Object[1];

        Map<String,String>hm=new HashMap<String,String>();

        hm.put(Excel.Readvalue(s, 0, 0), Excel.Readvalue(s, i, 0));
        hm.put(Excel.Readvalue(s, 0, 1), Excel.Readvalue(s, i, 1));
        hm.put(Excel.Readvalue(s, 0, 2), Excel.Readvalue(s, i, 2));
        hm.put(Excel.Readvalue(s, 0, 3), Excel.Readvalue(s, i, 3));

        System.out.println(Excel.Readvalue(s, 0, 0)+"...."+ Excel.Readvalue(s, i, 0));
        System.out.println(Excel.Readvalue(s, 0, 1)+"...."+ Excel.Readvalue(s, i, 1));
        System.out.println(Excel.Readvalue(s, 0, 2)+"...."+ Excel.Readvalue(s, i, 2));
        System.out.println(Excel.Readvalue(s, 0, 3)+"...."+ Excel.Readvalue(s, i, 3));

        obj[0]=hm;
        alist.add(obj);

        }
        return alist.iterator();
        }
    }
Ak17
  • 75
  • 3
  • 15
  • What you are referring to script name? Do you want to pass 3 column + script name to your test method? – CARE Jul 18 '16 at 19:17
  • I will be passing all the 3 column from the data provider, but the row selection will be done based on the scriptname I pass from the dataprovider to the test method and scriptname column is included with in the 3 columns.The test method should take only the scriptname parameter. – Ak17 Jul 19 '16 at 03:12
  • if the below answer answers your question, please mark the questions as answered – virusrocks Jul 19 '16 at 17:03

2 Answers2

0

You can get the test context passed in to your data provider method. This will be done internally by TestNG. from test context you can query the name of the test which called it.

Change the signature of your data provider to

public static Iterator<Object[]> cbntestdata(ITestNGMethod testContext) throws IOException
{
String testName = testContext.getMethodName();

//your regular logic

}

use the name obtained from testContext to get data from excel.

virusrocks
  • 861
  • 1
  • 5
  • 19
  • How can I pass this is in the test method.This is how my Test method looks like @Test(groups={"Regression"},dataProvider="cbndataprovider",dataProviderClass=dataprovider.cbn_DataProvider.class,priority=1,enabled = true) public void GB01_cbn_SearchNoRecords(Maphm) {/*body*/} – Ak17 Jul 19 '16 at 18:29
  • You can only do that by adding the test name in the map and retrieving the test name back from the map in your test method – virusrocks Jul 19 '16 at 20:09
0

If I understand correctly, your Excel has a Data corresponding to the Script/ClassName and you want some mechanism through which your tests can directly identify the running test and fetch the data corresponding to it. You can achieve this through a simple mechanism: Suppose your ClassName is ABC.java. As per your excel sheet, under column Name"Scriptname", you can add a row as: ABC Now, in your class ABC.java, before doing anything call the function

getClass().getSimpleName()

And link this to read your excel. This in turn will read "Testdata" stored corresponding to the "Scriptname" row with matching classname.