Selenium与TestNG和Java中的数据提供程序不匹配错误
您能否就从Selenium Java实现Excel流时出现的数据提供程序不匹配错误的可能原因提出建议Selenium与TestNG和Java中的数据提供程序不匹配错误,java,selenium,testng,testng-dataprovider,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.St
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;
}
根据上述错误,createFlow()
方法期望字符串作为参数,但您正在传递的是不可接受的单元格
请尝试以下修改的代码:
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()
获取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;
}
公共静态对象[][]TestData(){
sheet=book.getSheetAt(0);
int rowCount=sheet.getLastRowNum();
int cellCount=sheet.getRow(0.getLastCellNum();
对象[][]数据=新对象[rowCount][cellCount];
对于(int i=0;i
从导入org.apache.poi.ss.usermodel.Cell导入Cell
代码>包
我希望它能帮助
@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 }
}
可能尝试将名称分配给数据提供程序,并将数据提供程序名称替换为方法名称。Hi,您可以在此处共享将这些TestData()作为参数传递的其他代码吗?基本上它表示类型不匹配。@KajalKundu还添加了其余代码。调用TestData方法看起来不错。我在这里看不到任何问题。请检查您的excel表是否所有值都是文本格式?我猜DOB和mobile number值的格式不正确。我可能错了。@CRUZE您在哪里声明了“sheet”变量?谢谢@Ali。。。。。可以通过添加getStringCellValue()方法来解决此问题。但我在excel中同时使用了数字和字符串,并且通过使用getStringCellValue(),我得到了另一个整数字符串不匹配的异常。是的,如果您试图从excel工作表中将数字作为字符串读取,您将得到该异常。我将在几分钟内更新处理该场景的代码。。。因为它解决了你的问题,请接受我的答案,如果它对你有效或有帮助的话,请投票表决。。。谢谢,您好,尝试了第二种方法getCellType(),再次得到相同的数据不匹配错误。是的,它确实会失败。检查您的public void CreateFlow(字符串firstName、字符串lastName、字符串地址、字符串电子邮件、字符串DOB、字符串MobileNumber、字符串homernumber、字符串PIN、字符串id、字符串secondID、字符串AccountID、字符串number)抛出IOException、InterruptedException{//在此方法中打印所有这些值}
方法参数,因为它们都是字符串
类型,并且这里没有数值
类型,这就是脚本失败的原因。如果您遵循第一种方法,那么您就不会面临这个问题……是的……得到了……我在第二种方法中也使用了DataFormatter类进行了一些修改……而且它的工作方式非常好哦…:-)谢谢您的帮助和调试…:-)
| Ali |
| '123 |
| Puppy |
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;
}
@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 }
}