使用TestNG和Java的Selenium中的数据提供程序不匹配错误

时间:2019-02-12 13:57:58

标签: java selenium testng testng-dataprovider

能否请您提出从Selenium Java实现Excel流程时数据提供程序不匹配错误的可能原因。

org.testng.internal.reflect.MethodMatcherException: 
Data provider mismatch
Method: CreateFlow([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=2, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=3, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=4, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=5, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=6, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=7, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=8, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=9, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=10, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=11, type=java.lang.String, declaredAnnotations=[]}])
Arguments: [(org.apache.poi.xssf.usermodel.XSSFCell) AakashAuto,(org.apache.poi.xssf.usermodel.XSSFCell) Dummy,(org.apache.poi.xssf.usermodel.XSSFCell) dummy,(org.apache.poi.xssf.usermodel.XSSFCell) gmaAIL.COM,(org.apache.poi.xssf.usermodel.XSSFCell) asdsad,(org.apache.poi.xssf.usermodel.XSSFCell) sads,(org.apache.poi.xssf.usermodel.XSSFCell) asd,(org.apache.poi.xssf.usermodel.XSSFCell) asd,(org.apache.poi.xssf.usermodel.XSSFCell) Dummy,(org.apache.poi.xssf.usermodel.XSSFCell) Dummy,(org.apache.poi.xssf.usermodel.XSSFCell) Dummy,(org.apache.poi.xssf.usermodel.XSSFCell) asd]
    at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
    at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:983)
    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:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

我用来从Excel中获取数据的代码:

    public static Object[][] TestData() {
Workbook workBook;
    Sheet sheet;           
File src = new File(FilePath);
        workBook = WorkbookFactory.create(src); 
sheet = book.getSheetAt(0);
            int rowCount = sheet.getLastRowNum();
            int cellCount = sheet.getRow(0).getLastCellNum();
            Object[][] data = new Object[rowCount][cellCount];
            for (int i = 0; i < rowCount; i++) {[![enter image description here][1]][1]
                for (int j = 0; j < cellCount; j++) {
                    data[i][j] = sheet.getRow(1).getCell(j);
                }
            }
            return data;
        }

我在其中调用此TestData的方法:

@DataProvider
    public Object[][] getData() {
        Object[][] data = TestUtil.TestData();
        return data;
    }

@Test(dataProvider = "getData")
public void CreateFlow(String firstName, String lastName, String address, String email, String DOB,
                String MobileNumber, String HomeNumber, String PIN, String id, String secondID, String AccountID,
                String number) throws IOException, InterruptedException {
    //Printing all these values inside this method
        }

2 个答案:

答案 0 :(得分:1)

org.testng.internal.reflect.MethodMatcherException: 
Data provider mismatch
Method: CreateFlow([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=2, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=3, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=4, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=5, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=6, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=7, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=8, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=9, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=10, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=11, type=java.lang.String, declaredAnnotations=[]}])
Arguments: [(org.apache.poi.xssf.usermodel.XSSFCell) AakashAuto,(org.apache.poi.xssf.usermodel.XSSFCell) Dummy,(org.apache.poi.xssf.usermodel.XSSFCell) dummy,(org.apache.poi.xssf.usermodel.XSSFCell) gmaAIL.COM,(org.apache.poi.xssf.usermodel.XSSFCell) asdsad,(org.apache.poi.xssf.usermodel.XSSFCell) sads,(org.apache.poi.xssf.usermodel.XSSFCell) asd,(org.apache.poi.xssf.usermodel.XSSFCell) asd,(org.apache.poi.xssf.usermodel.XSSFCell) Dummy,(org.apache.poi.xssf.usermodel.XSSFCell) Dummy,(org.apache.poi.xssf.usermodel.XSSFCell) Dummy,(org.apache.poi.xssf.usermodel.XSSFCell) asd]
    at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
    at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:983)
    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:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

根据上述错误,createFlow()方法期望将String作为参数,但是您传递了Cell,这是不可接受的。

尝试以下修改后的代码:

public static Object[][] TestData() {
    sheet = book.getSheetAt(0);
    int rowCount = sheet.getLastRowNum();
    int cellCount = sheet.getRow(0).getLastCellNum();
    Object[][] data = new Object[rowCount][cellCount];
    for (int i = 0; i < rowCount; i++) {
        for (int j = 0; j < cellCount; j++) {
            // Call 'getStringCellValue()' here instead of using just 'getCell()'
            data[i][j] = sheet.getRow(1).getCell(j).getStringCellValue().trim();
        }
    }
    return data;
}

您需要调用getStringCellValue()方法来获取String数据。

更新:

如果您尝试使用getStringCellValue()从excel读取数值数据,则会出现异常。

为避免出现此异常,您可以在Excel工作表的“数字”单元格中附加'撇号作为前缀。

假设您在excel中有以下格式的数据:

| Ali   |
| 123   |
| Puppy |

您可以使用Ali获得PuppygetStringCellValue()值,但是由于{Number}是Number,因此无法获得123,因此请尝试在此之前附加'在Excel中123,然后按Enter。看起来像这样,您不会收到该错误。

| Ali   |
| '123  |
| Puppy |

如果您要获取数据,而不考虑excel提供的数据类型,并且不想附加',则需要执行以下操作:

public static Object[][] TestData() {
    sheet = book.getSheetAt(0);
    int rowCount = sheet.getLastRowNum();
    int cellCount = sheet.getRow(0).getLastCellNum();
    Object[][] data = new Object[rowCount][cellCount];
    for (int i = 0; i < rowCount; i++) {
        for (int j = 0; j < cellCount; j++) {
            switch (sheet.getRow(1).getCell(j).getCellType()) {
               case Cell.CELL_TYPE_NUMERIC:
                   // Call 'getNumericCellValue()' here instead of using just 'getCell()'
                   data[i][j] = sheet.getRow(1).getCell(j).getNumericCellValue();
                   break;
               case Cell.CELL_TYPE_STRING:
                   // Call 'getStringCellValue()' here instead of using just 'getCell()'
                   data[i][j] = sheet.getRow(1).getCell(j).getStringCellValue().trim();
                   break;
            }
        }
    }
    return data;
}

Cell包中导入import org.apache.poi.ss.usermodel.Cell;

希望对您有帮助...

答案 1 :(得分:0)

@DataProvider(name = "yourDPname")
public Object[][] getData() {
    Object[][] data = TestUtil.TestData();
    return data;
}

@Test(dataProvider = "yourDPname")
public void CreateFlow(String firstName, String lastName, String address,
        String email, String DOB, String MobileNumber, String HomeNumber,
        String PIN, String id, String secondID, String AccountID, String number)
        throws IOException, InterruptedException {
    //Printing all these values inside this method }
}

也许尝试为您的数据提供者分配名称,并替换数据提供者名称而不是方法名称。