org.testng.internal.reflect.MethodMatcherException:数据提供者不匹配

时间:2019-02-11 17:30:22

标签: selenium-webdriver testng

我有一个xls文件,其中包含2列测试数据。我想为每一列都有一种测试方法。

现在,我知道如何使用dataprovider读取xls文件并将测试数据提供给测试方法。但是,如何将同一个数据提供程序用于多个测试?

我有以下代码,一旦我运行此代码,就会报错 我的代码是:

@Test(dataProvider="TestData")
public void test1(String Username ) throws InterruptedException
{           
driver.findElement(By.xpath("//*[@id=\"UserName\"]")).sendKeys(Username);               
}

@Test(dataProvider="TestData")
public void test2(String Password)
{   
driver.findElement(By.xpath("//*[@id=\"Password\"]")).sendKeys(Password);
driver.findElement(By.xpath("//[@id=\"columns\"]/div/div[1]/form/div[1]/div[2]/div[4]/input")).click();
}




@DataProvider (name="TestData")
public  Object [][] readexcel(Method method) throws BiffException, IOException 
        {

File f=new File("E:\\Selenium\\Eclips\\NCPDP\\src\\TestData\\TestData.xls");
            Workbook w = Workbook.getWorkbook(f);
            Sheet s = w.getSheet("TestData");
            int rows = s.getRows();
            int columns =s.getColumns();

            String inputData[][]=new String[rows-1][columns];
            try {
                for(int i=1;i<rows;i++)
                    {
                    for(int j=0;j<columns;j++)
                        {
            Cell c=s.getCell(j,i);
            inputData[i-1][j]=c.getContents();

                        }

                    }

        }
        catch(Exception e){
            e.getMessage();
        }

        return inputData;
        }  

输出:

[RemoteTestNG]检测到TestNG版本6.14.2 在端口47795上启动ChromeDriver 2.45.615291(ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) 仅允许本地连接。 二月12,2019 1:15:28下午org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到的方言:OSS 失败:test1 org.testng.internal.reflect.MethodMatcherException: 数据提供者不匹配 方法:test1([Parameter {index = 0,type = java.lang.String,clarifiedAnnotations = []}]) 参数:[(java.lang.String)TESTQA41,(java.lang.String)Ncpdp @ 1234]     在org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)     在org.testng.internal.Parameters.injectParameters(Parameters.java:796)     在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982)     在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)     在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)     在org.testng.TestRunner.privateRun(TestRunner.java:648)     在org.testng.TestRunner.run(TestRunner.java:505)     在org.testng.SuiteRunner.runTest(SuiteRunner.java:455)     在org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)     在org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)     在org.testng.SuiteRunner.run(SuiteRunner.java:364)     在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)     在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)     在org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)     在org.testng.TestNG.runSuitesLocally(TestNG.java:1137)     在org.testng.TestNG.runSuites(TestNG.java:1049)     在org.testng.TestNG.run(TestNG.java:1017)     在org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

失败:test2 org.testng.internal.reflect.MethodMatcherException: 数据提供者不匹配 方法:test2([Parameter {index = 0,type = java.lang.String,clarifiedAnnotations = []}]) 参数:[(java.lang.String)TESTQA41,(java.lang.String)Ncpdp @ 1234]     在org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)     在org.testng.internal.Parameters.injectParameters(Parameters.java:796)     在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982)     在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)     在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)     在org.testng.TestRunner.privateRun(TestRunner.java:648)     在org.testng.TestRunner.run(TestRunner.java:505)     在org.testng.SuiteRunner.runTest(SuiteRunner.java:455)     在org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)     在org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)     在org.testng.SuiteRunner.run(SuiteRunner.java:364)     在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)     在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)     在org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)     在org.testng.TestNG.runSuitesLocally(TestNG.java:1137)     在org.testng.TestNG.runSuites(TestNG.java:1049)     在org.testng.TestNG.run(TestNG.java:1017)     在org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

================================================     默认测试

测试运行:2,失败:2,跳过:0

================================================ 默认套件

运行的总测试次数:2,失败:2,跳过:0

1 个答案:

答案 0 :(得分:0)

如果您的DataProvider返回两个参数,

您需要将这两个参数传递给@Test批注:

您的解决方案:

@Test(dataProvider="TestData")
public void test1(String Username,String Password) throws Exception
{           
driver.findElement(By.xpath("//*[@id=\"UserName\"]")).sendKeys(Username);          
driver.findElement(By.xpath("//*[@id=\"Password\"]")).sendKeys(Password);
driver.findElement(By.xpath("//[@id=\"columns\"]/div/div[1]/form/div[1]/div[2]/div[4]/input")).click();
}