Question

I started learning Selenium2 (WebDriver) with Eclipse and TestNG. I have a question about DataProvider. I have a login page having user, password and login button for example. I have written a test in TestNG. I have used pageobject for UI objects (have separate class) and actual test in another class.

Here glogin is a class and login is the function where finding elements and sending keys is done and this is called in another class gtest(which is main test) which has TestNG annotations.

I access that class in main script which will take values.

@test(Dataprovide = "test")
public void glogin(String user, String pass)
{
  glogin log1 = new login;
  log1.login(user,pass);
}

I have the following excel sheet

user      pass    
John      Smith      
Carson    Black    
Carla     ck23
test      test4

When I use dataprovider and get data from the excel sheet as array and use it in Test then the following error is displayed:

org.testng.TestNGException: 

The data provider is trying to pass 4 parameters but the method plus.gmail#glogin takes 2
    at org.testng.internal.Invoker.injectParameters(Invoker.java:1337)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1225)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Any help is really appreciated.

Here is code for method annotated with Dataprovider and

    @DataProvider(name="test")
        public Object[][] createdata1()throws Exception
        {
            Object[][] retobj = getexcel();
            return retobj;              
        }

        private String[][] getexcel() throws Exception
        {
            // TODO Auto-generated method stub

            String[][] tabarray = null;
            try {
                Workbook wb1 = Workbook.getWorkbook(new 

File("F:/testdata.xls"));

                Sheet sheet = wb1.getSheet("userlogin");

                Cell tablestart = sheet.findCell("login");

                int startrow = tablestart.getRow();
                int startcol = tablestart.getColumn();

                Cell tableend = sheet.findCell("login",startcol+1,startrow+1, 

100, 64000, false);

                int endrow = tableend.getRow();
                int endcol = tableend.getColumn();

                System.out.println("startRow="+startrow+", endRow="+endrow+", 

" + "startCol="+startcol+", endCol="+endcol);

                 tabarray = new String[endrow - startrow + 1][endcol - 

startcol + 1];

                int ci = 0;
                for(int i = startrow +1 ;i<endrow;i++,ci++)
                {

                    int cj = 0;
                    for(int j = startcol + 1;j<endcol;j++,cj++)
                    {
                        tabarray[ci][cj] = sheet.getCell(j, 

i).getContents();
                        System.out.println(tabarray[ci][cj]);

                    }   
                }

            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return tabarray;
        }

test(Dataprovider = "test")
public void glogins(String user, String pass)
{
  glogin log1 = new glogin(driver);
  log1.login(user,pass);
}

When I executed the test, I received data from excel as

john
smith
carson
Black
carla
ck23
test
test4

as output

No correct solution

OTHER TIPS

Isn't the error message self-explanatory?

The data provider is trying to pass 4 parameters but the method plus.gmail#glogin takes 2

Use a debugger and figure out why your data provider is returning 4 parameters instead of just 2.

Use

tabarray = new String[endrow - 1][endcol - 1]; instead of //tabarray = new String[endrow - startrow + 1][endcol - startcol + 1];

Because you intended to return only 2*2 array

Replace the line

tabarray = new String[endrow - startrow + 1][endcol - startcol + 1];

with

tabarray = new String[endrow - startrow - 1][endcol - startcol - 1];

I agree with the response posted by Kalyan; the reason being the first and last column of the data provider excel sheet is also counted in as arguments / parameters by the function; therefore please use tabArray=new String[endRow-startRow-1][endCol-startCol-1]; Hope this helps and happy testing...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top