which function gives me the dataprovider loop counter in testNG

969 views Asked by At

I'm logging the testresults to DB while testNG runs the testcases. I'm using excel sheet to provide input data .

For eg:

tablename

       row1 col1
       row2  col2   
                  tablename

I want to know, which row is getting executed ? There might be any function in dataprovider class which will be storing the counter.

Please help me get the counter value.

Thanks in advance.

2

There are 2 answers

0
user1058106 On

Not completely sure that I get what you try to do; but data provider can hardly allow you to get information you want. It will be possible to access method arguments directly if one will implement ITestListener interface.

@Override
public void onTestStart(ITestResult result)
{
    Object[] params = result.getParameters();
}
0
curious On
You can do the following to get the information you want.

1) Create a dataprovider class that reads from excel sheet
You can use the  jdbc:odbc driver to do the same or use Apache POI
In the same class, create a method to get the record count. The queries
you will be writing is simply the number of rows in the excel sheet.

1) Implement the iterator inteface
public class RecordIterator implements Iterator<Object>
{
   private int iRecordCtr = 1;
    public RecordIterator()
    {
        iRecordCount = dataProvider.getNoOfRecords();
    }
     //Override the hasNext()
     public boolean hasNext() {

        return (iRecordCtr <= iRecordCount);

    }
   //Override the next() in iterator interface
    public Object next()
    {
       //Read the columns in excel sheet. In the DAO you return you can
       //also set the value of the record you process.
       return new Object[] { data } 
    }
}

public static RecordIterator getMyIteratorImpl(String worksheet)
{
 return new RecordIterator(params);
}

In your test:
@Test(dataProvider-"mydataProvider")
public void test(Data dataobj) //Single row of the excel sheet is returned as java object and one row is returned at a time. Lazy loading
}

@DataProvider(name="mydataProvider")
public Iterator<?> getMyIterator()
{
   return new RecordIterator(excelsheet name);
}