小编典典

Selenium与TestNG中的数据提供者不匹配

java

我正在用硒编写以下代码,并且显示以下错误,请让我知道问题出在哪里。

导入org.testng.annotations.DataProvider; 导入org.testng.annotations.Test;

public class testngexcel {

    public static ExcelReader excel = null;
    @Test(dataProvider = "newdata")
    public void testData(String username, String password, Integer age) {
        System.out.println(username + " - " + password + " - " + age);
    }

    @DataProvider(name = "newdata")
    public static Object[][] getData() {

            if (excel == null) {
                excel = new ExcelReader("C:\\Users\\Anjali.Nautiyal\\Desktop\\selenium\\testngdata.xlsx");
            }

            String sheetName = "login";
            int rows = excel.getRowCount(sheetName);
            int cols = excel.getColumnCount(sheetName);

            Object[][] data = new Object[rows - 1][cols];

            for (int rowNum = 2; rowNum <= rows; rowNum++) {
                for (int colNum = 0; colNum < cols; colNum++) {
                    data[rowNum - 2][colNum] = excel.getCellData(sheetName, colNum, rowNum);
                }
            }
            return data;

错误:

失败:testData
org.testng.internal.reflect.MethodMatcherException:数据提供者不匹配方法:testData([Parameter
{index = 0,type = java.lang.String,clarifiedAnnotations = []},Parameter
{index = 1,type = java .lang.String,clarifiedAnnotations = []},参数{index =
2,type = java.lang.Integer,clarifiedAnnotations =
[]}])参数:[(java.lang.String)anjali,(java.lang.String
)pa,(java.lang.String)25.0],位于org.org的org.testng.internal.Invoker.injectParameters(Invoker.java:1293),位于org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:49)
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)位于org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)处的.testng.internal.Invoker.invokeTestMethods(Invoker.java:1187)
org.testng。org.testng.SuiteRunner.runTest(SuiteRunner.java:380)的org.testng.TestRunner.run(TestRunner.java:602)的org.testng.SuiteRunner.runTest(SuiteRunner.java:380)的org.testng.SuiteRunner.runSequentially(SuiteRunner)
.java:375)org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)org.testng.SuiteRunner.run(SuiteRunner.java:289)org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)在org.testng.TestNG.runSuitesLocally(TestNG.java:1226)的org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)的org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)处。
org.testng.TestNG.run(TestNG.java:1115)的org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)的TestNG.runSuites(TestNG.java:1144)org.testng.remote.RemoteTestNG
.initAndRun(RemoteTestNG.java:230)在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)


阅读 208

收藏
2020-11-26

共1个答案

小编典典

我看到的唯一问题是,您是从excel中读取的,因此可能所有值都已输入String,除非您将其转换为Integer。但是,在测试中,您希望第三个参数ageInteger

更改类型以String解决问题

@Test(dataProvider = "newdata")
public void testData(String username, String password, String age) {
        System.out.println(username + " - " + password + " - " + age);
}

以下代码将引发相同的错误。

@DataProvider(name = "newdata")
public static Object[][] getData() {
    return new Object[][]{
            {"20"},
            {"30"}
    };
}

@Test(dataProvider = "newdata")
public void testData(Integer age) {

    System.out.println(age);

}
2020-11-26