首页 > 解决方案 > 组合两个 DataProviders 以从 excelsheet 读取时出现 UnresolvedParameter 错误

问题描述

组合两个数据提供程序时出现以下错误,但找不到错误原因。有人可以帮我吗?

  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)

以下是我的测试课。

      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;
                
            }
            
            
        }

标签: javaseleniumtestng

解决方案


数据提供者方法不能有除了这里列出的参数。您可以尝试应用 Guice DI 框架来注入一些不同类型的对象,但我个人没有尝试过。


推荐阅读