How to get the current row in TestNG data provider

417 views
Skip to first unread message

Sohan Ranga

unread,
Dec 17, 2016, 2:38:15 PM12/17/16
to seleniu...@googlegroups.com

Hello Friends,

I need help in to get the current row in TestNG data provider.



Scenario:

I have parameterized test case using TestNG data provider. Reading data from excel to feed into the application. This is working fine and test is iterated based on the number of rows in excel. However, I need to store the current row number being executed because I need to pass the row number to another method to set data back to the excel sheet within the same iteration.

For example: Test case is creating a customer and once the customer is created then need to store the generated customer number back to the same row in the excel that is currently in use for execution. Row number is needed in ‘xls.setCellData’ method. Currently I have hard coded as “7”.

 

Please help.

 

public class TestWrite extends BaseTest {

 

       String testCaseName = "testWrite";

 

       @Test(dataProvider="getData")   

       public void testWrite(Hashtable<String,String> data) {           

 

              if(createCustomer())

                     reportPass("Create Customer --> Passed");

              else

                     reportFailure("Create Customer -->Failed");

             

              String customerNumber="00123897"//this will be generated at runtime

              String sheetName = Constant.TESTDATA_SHEET;

              xls.setCellData(sheetName"CustomerNumber", 7, customerNumber);

             

       }

       @DataProvider

       public Object[][] getData(){

              xls = new Xls_Reader(Constant.DATA_XLS_PATH);                    

              return getData(testCaseNamexls);

              }

      

}

 

Regards

Sohan

Ahmet OZKESEK

unread,
Dec 17, 2016, 2:50:54 PM12/17/16
to Selenium Users
Hi,

What does data parameter of testwrite function look like? Have you test it?

AO,

Sohan Ranga

unread,
Dec 17, 2016, 4:21:59 PM12/17/16
to seleniu...@googlegroups.com
Hi,
Yes, data provider is working fine.

Regards
Sohan



AO,

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/033c0107-c743-456b-941e-9e8666c09088%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ahmet OZKESEK

unread,
Dec 18, 2016, 5:46:16 AM12/18/16
to Selenium Users
Hi Sohan,

What I'm asking about is Hashtable<String, String> data paramater that you have never used in the testWrite function.

What is the data.keys? This may be a clue of the row (and its number) that you seeking.

AO,

Sohan Ranga

unread,
Dec 20, 2016, 1:07:22 PM12/20/16
to seleniu...@googlegroups.com
Hi Ahmet,
Yes, I am using that, Actually it was a lengthy test case so i copied into separate to make simply. I am passing this to 'data' to createCustomer.
 

public class TestWrite extends BaseTest {

 

       String testCaseName = "testWrite";

 

       @Test(dataProvider="getData")   

       public void testWrite(Hashtable<String,String> data) {           

 

              if(createCustomer(data))

                     reportPass("Create Customer --> Passed");

              else

                     reportFailure("Create Customer -->Failed");

             

              String customerNumber="00123897"//this will be generated at runtime

              String sheetName = Constant.TESTDATA_SHEET;

              xls.setCellData(sheetName"CustomerNumber", 7, customerNumber);

             

       }

       @DataProvider

       public Object[][] getData(){

              xls = new Xls_Reader(Constant.DATA_XLS_PATH);                    

              return getData(testCaseNamexls);

              }

      

}

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

Ahmet OZKESEK

unread,
Dec 20, 2016, 4:06:38 PM12/20/16
to Selenium Users
Hi Sohan,

I don't know what exactly your test data, flow and algorithm are, 
but you need to figure out how to pass not only test length but the row-number 
within the 'data'.  

Of course you also have other option which is defining a member row_number variable
and then incrementing it every time you created new customer.  

I"ll give you a very simple TestNG example;


public class Library {
        
    public static void createCustomer(Hashtable<String, String> data) {
            System.out.println(data.get("row_number"));
    }
    
}

public class LibraryTest {
        
    @DataProvider(name="testData", parallel=false)
    public Object[][] testData() {
            return new Object[][] {
                    { new Hashtable<String, String>() {{ put("row_number", String.valueOf(0)); put("name", "ahmet"); put("surname", "ozkesek"); }} },
                    { new Hashtable<String, String>() {{ put("row_number", String.valueOf(1)); put("name", "hmet"); put("surname", "zkesek"); }} },
                    { new Hashtable<String, String>() {{ put("row_number", String.valueOf(2)); put("name", "met"); put("surname", "kesek"); }} }
            };
    }
    
    @Test(dataProvider="testData")
    public void createCustomer(Hashtable<String, String> data) {
            Library.createCustomer(data);
    }
}



best wishes,

AO,

Sohan Ranga

unread,
Dec 20, 2016, 8:10:15 PM12/20/16
to seleniu...@googlegroups.com
Thanks Ahmet. I will try this option.


Regards
Sohan

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages