0

I am taking values from excel and than saving values in String type. Created object array and passed those values. added Object array to ArrayList. When using it as dataprovider it is giving me erros. Please help. My Excel has only 2 columns ussername and password.

Here is my code. Kindly help what i am missing Utility -- This is the code where i have accessed excel and saved values.

public static ArrayList<Object> datafromexcel() throws IOException 
    {
        
        ArrayList<Object> objectlist = new ArrayList<Object>();     
        FileInputStream fis = new FileInputStream("path i have given of excel");
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
         XSSFSheet sheet1 =  workbook.getSheetAt(0);
         int lastrowcount= sheet1.getLastRowNum();
        // int totalcellcount = sheet1.getRow(0).getLastCellNum();
         
         for(int i=1;i<lastrowcount;i++)
            {               
                String username = sheet1.getRow(i).getCell(0).getStringCellValue(); 
                String password = sheet1.getRow(i).getCell(1).getStringCellValue();             
                Object ob[] = {username, password};             
                objectlist.add(ob);                             
            }
                
            return objectlist;
        
    }

This is Data provider Data provider

    @DataProvider(name="exceldata")
        public Iterator<Object> getDataArray() throws IOException
        {
            ArrayList<Object> list= DataproviderclassFFW1.datafromexcel();
            return list.iterator();
            
        }

Calling Data provider in test.

@Test (dataProvider= "exceldata")
    public void homepageclass1(String username, String password) throws IOException, InterruptedException
    {
              
      HomePageFFW1 ObjHomePage = new HomePageFFW1(driver);
      ObjHomePage.HomePageLoginLink().click(); 
      
      signInFFW1 objsignin = new signInFFW1(driver);
      objsignin.enterusername().sendKeys(username);
      objsignin.enterpassword().sendKeys(password);
      objsignin.clicklogin().click();
    }

This is giving me Error

[RemoteTestNG] detected TestNG version 7.2.0
FAILED: homepageclass1
org.testng.internal.reflect.MethodMatcherException: 
[public void E2ETestGrpID1.E2EArtifactid1.HomePage.homepageclass1(java.lang.String,java.lang.String) throws java.io.IOException,java.lang.InterruptedException] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation).
Data provider mismatch
Method: homepageclass1([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}])
Arguments: [([Ljava.lang.Object;) [[email protected], qalastname]]
    at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:43)
    at org.testng.internal.Parameters.injectParameters(Parameters.java:905)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:34)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:821)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:588)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1214)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
    at org.testng.TestNG.runSuites(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:1034)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
1
  • 1
    Based on your error, you are passing an object but in your test you are expecting it to be in String,.
    – ky-chan
    Commented Jun 30, 2020 at 9:46

1 Answer 1

2

If you're dealing with data provider that returns iterator, then you have to return

Iterator<Object[]>, not Iterator<Object>

Otherwise you will need to return Object[][]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.