问题描述
您能否建议数据提供程序不匹配错误的可能原因,该错误是在从 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
}
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 |
您可以使用getStringCellValue()
获得Ali
、 Puppy
值,但无法获得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;
}
从import org.apache.poi.ss.usermodel.Cell;
导入Cell
import org.apache.poi.ss.usermodel.Cell;
包裹。
我希望它有帮助...
2楼
@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 }
}
也许尝试为您的数据提供者分配名称并替换数据提供者名称而不是方法名称。