本文介绍了Selenium 中的数据提供者与 TestNG 不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 selenium 编写以下代码,但显示以下错误,请告诉我问题出在哪里.

I am writing the below code in selenium and below error is showing, please let me know where is the issue.

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

import org.testng.annotations.DataProvider;import 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,类型=java.lang.String,声明注释=[]},参数{索引=1,类型=java.lang.String,声明注释=[]},参数{索引=2,type=java.lang.Integer, DeclarationAnnotations=[]}]) 参数:[(java.lang.String)anjali,(java.lang.String)pa,(java.lang.String)25.0]在org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:49)在 org.testng.internal.Invoker.injectParameters(Invoker.java:1293)在 org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1187)在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)在 org.testng.TestRunner.privateRun(TestRunner.java:744) 在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.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 在org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) 在org.testng.TestNG.runSuitesLocally(TestNG.java:1226) 在org.testng.TestNG.runSuites(TestNG.java:1144) 在org.testng.TestNG.run(TestNG.java:1115) 在org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)在 org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)在 org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

推荐答案

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

The only problem I see is this, You are reading from excel so may be all the values are coming in String, unless you are converting that to Integer. However, in your test you expect third argument age to be Integer

将类型更改为 String 应该可以解决问题

Changing type to String should resolve the issue

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

以下代码会引发相同的错误.

The following code would raise the same error.

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

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

    System.out.println(age);

}

这篇关于Selenium 中的数据提供者与 TestNG 不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:56