简体   繁体   中英

Getting an UnresolvedParameter error when combining two DataProviders to read from excelsheet

I am getting the following error when combining the two data providers and I can't find the cause of the error. Could anyone help me?

  Utils] [ERROR] [Error] org.testng.TestNGException: 
    Some DataProvider public java.lang.Object[] com.w2a.testcases.XCartCheckout.concat(java.lang.Object[],java.lang.Object[]) throws java.lang.Exception parameters unresolved:  at 0 type class [Ljava.lang.Object;
     at 1 type class [Ljava.lang.Object;
        
            at org.testng.internal.MethodInvocationHelper.getParameters(MethodInvocationHelper.java:197)
            at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:130)
            at org.testng.internal.Parameters.handleParameters(Parameters.java:706)
            at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
            at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
            at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
            at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
            at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
            at org.testng.TestRunner.privateRun(TestRunner.java:648)
            at org.testng.TestRunner.run(TestRunner.java:505)
            at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
            at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
            at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
            at org.testng.SuiteRunner.run(SuiteRunner.java:364)
            at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
            at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
            at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
            at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
            at org.testng.TestNG.runSuites(TestNG.java:1049)
            at org.testng.TestNG.run(TestNG.java:1017)
            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)

The following is my test class.

      public class XCartCheckout extends TestBase{
            public static WebDriver driver;
            StoreHomePage ohome;
            StoreCartPage ocart;
            StoreCheckoutPage ochk;
            StoreLoginValidation ologin;
            int itemcount=0;
        
            // ArrayList<String> productname = new ArrayList<String>();
            @Test(dataProvider = "DataFromExcel", priority = 1)
            // public void AddToCartTest(productname) throws InterruptedException {
            public void AddToCartTest(String[][] productname) throws InterruptedException {
               //int b=0;
                //int itemcount =0;
                ArrayList<String> arrlist = new ArrayList<String>();
                boolean t = true;
                System.out.println("length =" + productname.length);
                
                System.out.println("arraylist values are");
                for (int i = 0; i < productname.length; i++) {
                    for (int j = 0; j < productname[i].length; j++) {
                        // System.out.println("productname=" + productname[i][j]);
                        arrlist.add(productname[i][j]);
                        // System.out.println("b=" + b+ "productname="+productname[i][j]);
                        // b++;
                    }
        
                }
        
                for (int k = 0; k < arrlist.size(); k++) {
                    System.out.println(arrlist.get(k));
                }
                itemcount = arrlist.size();
                driver.get("*********url******");
                driver.manage().window().maximize();
                ohome = new StoreHomePage(driver);
                ohome.movingBanners();
                boolean sortstatus = ohome.selectSortOrder();
                //b = Boolean.compare(sortstatus, t);
        
                if (sortstatus == t) {
                    for(int l=0;l<arrlist.size();l++)
                    {
                    String cartmsg = ohome.addToCart(arrlist.get(l),itemcount);
                    Assert.assertEquals(cartmsg, "You have just added");
                    System.out.println("Product added to cart");
                    }
                }
        
            }
            
            @Test(dataProvider ="concat",priority = 2)
            public void Testcheckout(String username,String password)
            {  
                System.out.println(username + password);
                ocart = new StoreCartPage(driver);
                ocart.verifyCartPage(itemcount);
                ologin = new StoreLoginValidation(driver);
                //ologin.login(username, password);
            }
        
            @BeforeTest
            public void beforeTest() {
        
                System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver_win32\\chromedriver.exe");
                driver = new ChromeDriver();
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            }
        
            @AfterTest
            public void afterTest() {
            }
        
            @DataProvider
            public Object[][] DataFromExcel() throws Exception {
                ExcelUtilities2 oexcel = new ExcelUtilities2("E:\\Eclipse_Workspace\\Excel File\\Xcart-Data.xlsx", "products");
                return oexcel.exceldp();
        
            }
    
    //The following is my first dataprovider
            @DataProvider
            public Object[][] DataFromExcel1() throws Exception {
                ExcelUtilities oexcel = new ExcelUtilities("E:\\Eclipse_Workspace\\Excel File\\Xcart-Data.xlsx", "login");
                return oexcel.exceldp();
        
            }
    
    //The following is my first dataprovider        
            
    
        @DataProvider
                public Object[][] DataFromExcel2() throws Exception {
                    ExcelUtilities oexcel = new ExcelUtilities("E:\\Eclipse_Workspace\\Excel File\\Xcart-Data.xlsx", "Address");
                    return oexcel.exceldp();
            
                }
                
            @DataProvider
            public Object[][] concat1(Object[][] DataFromExcel1,Object[][] DataFromExcel2) throws Exception {
                Object[][] result  = ArrayUtils.addAll(DataFromExcel1, DataFromExcel2);
                System.out.println("result" + result);
                return result;
        
            }
            
        
        > With the below datprovider,i am trying to combine the above 2 dataproviders named DataFromExcel1 & DataFromExcel2.I am trying to use the data from this datprovider in 'Testcheckout' method.
        
            @DataProvider
            public Object[][] concat(Object[][] DataFromExcel1,Object[][] DataFromExcel2) throws Exception {
                Object[][] result  = ArrayUtils.addAll(DataFromExcel1, DataFromExcel2);
                System.out.println("result" + result);
                return result;
        
            }
        
        }
        
        > The following is my excel utilities file for reading data from excelsheet.
        Bothe my dataproviders will read from data from the same excel file but from different sheets.
        
       

     public class ExcelUtilities {
            
            FileInputStream ofile;
            XSSFWorkbook owkbook;
            XSSFSheet osheet;
            XSSFRow orow;
            XSSFCell ocell;
            
            public ExcelUtilities(String fileloc, String wrksheet) throws Exception
            {
                ofile = new FileInputStream(fileloc);
                owkbook = new XSSFWorkbook(ofile);
                osheet = owkbook.getSheet(wrksheet);
                
            }
            
            public String getCellData(int rownum, int colnum)
            {
                orow = osheet.getRow(rownum);
                ocell = orow.getCell(colnum);
                DataFormatter formatter = new DataFormatter();
                //String value = ocell.getStringCellValue();
                String value = formatter.formatCellValue(ocell);
                System.out.println("value in the cell is" + value);
                return value;
        
            }
            
            
            public Object[][] exceldp()
            {  
                String ExcelData[][];
                int rowno = osheet.getLastRowNum();
                orow = osheet.getRow(0);
                int colno = orow.getLastCellNum();
                ExcelData= new String[rowno][colno];
                for(int i=1;i<=rowno;i++)
                {
                    for(int j=0;j<colno;j++)
                    {
                        ExcelData[i-1][j]= this.getCellData(i,j);
                    }
                }
                return ExcelData;
                
            }
            
            
        }

Data provider methods cannot have parameters except of listed here . You can try to apply Guice DI framework in order to inject objects of some different types, but personally I haven't tried that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM