【问题标题】:Data Provider Mismatch in Selenium with TestNG and JavaSelenium 中的数据提供程序与 TestNG 和 Java 不匹配
【发布时间】:2021-11-05 18:18:47
【问题描述】:

这里有一些问题,请您指导我如何解决它?

这是我的 Excel 读取代码

import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel {
    public String unicode;
    public String[][] readExcel() throws IOException {
        XSSFWorkbook excelObject = new XSSFWorkbook("./data/unicode1.xlsx");
        XSSFSheet excelSheet = excelObject.getSheet("Sheet1");
        
        int rows = excelSheet.getLastRowNum();
        int columns = excelSheet.getRow(0).getLastCellNum();
        
        String[][] data = new String [rows][columns];

        for (int i = 1; i <= rows; i++) {
            XSSFRow row = excelSheet.getRow(i);
            for (int j = 0; j < columns; j++) {
                XSSFCell cell = row.getCell(j);
                String cellValue = cell.getStringCellValue();
                data[i-1][j] = cellValue;
            }
        }
    return data;
    }
}

我的测试用例代码

import java.io.IOException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.github.javafaker.Faker;
import commonFeatures.CommonThings;
import commonFeatures.ReadExcel;

public class SignUpWithUnicode extends CommonThings{
    
    Faker faker = new Faker();

    @Test(dataProvider = "fetchData")
    public void signUpWithUnicode(String unicode) throws InterruptedException {
        invokeBrowser("chrome", "https://master.signup");
        type(locateEle("xpath","//input[@id='first_name-id']"),faker.name().firstName());
        type(locateEle("xpath","//input[@id='last_name-id']"),faker.name().lastName());
        type(locateEle("xpath","//input[@id='last_name-id']"),faker.name().lastName());
        type(locateEle("xpath","//input[@id='client_user_email-id']"),faker.internet().emailAddress());
        type(locateEle("xpath", "//input[@id='password-id']"), "12345");
        type(locateEle("xpath", "//input[@id='degree-id']"), unicode);
        click(locateEle("xpath", "//span[contains(text(),'SUBMIT')]"));
        closeBrowser();
    }
        
    @DataProvider (name = "fetchData")
    public String[][] getData() throws IOException {
        ReadExcel excel = new ReadExcel();
        return excel.readExcel();   
    }
}

这是错误

[RemoteTestNG] detected TestNG version 7.0.0
FAILED: signUpWithUnicode
org.testng.internal.reflect.MethodMatcherException: 
[public void testCases.SignUpWithUnicode.signUpWithUnicode(java.lang.String) throws java.lang.InterruptedException] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation).
Data provider mismatch

我在 Excel 工作表中使用 Unicode 字符,因此我尝试使用 Object 而不是 String,但仍然出现此错误。然后我尝试使用 Excel 中的字符串来查找错误,但仍然没有成功。

【问题讨论】:

  • 我不熟悉数据提供者。我可以看到您将data 作为Multidimensional Array 返回,这意味着String[][],但您将数据作为String 传递,这意味着String unicode。那是正确的方法吗?这就是为什么它返回Data provider mismatch 所以通过String[][] unicode
  • 谢谢!几乎成功了,Eclipse 只是说我需要CharSequence[] unicode
  • @NandanA TestNG 数据提供者以不同的方式工作。如果您返回Object[][],那么测试方法的参数数量应该等于二维数组中的列数。行数代表测试用例的数量。示例:如果您返回String[3][5],则测试方法将被调用 3 次,并且测试方法中应存在 5 个参数。

标签: java selenium testing testng testng-dataprovider


【解决方案1】:

TestNG 数据提供程序以不同的方式工作。如果您返回Object[][],那么测试方法的参数数量应该等于二维数组中的列数。行数代表测试用例的数量。

示例:如果您返回String[2][3],则测试方法将被调用 2 次,并且测试方法中应存在 3 个参数。

@DataProvider (name = "fetchData")
public String[][] getData()  {   
    return new String[][] { { "a", "b", "c" }, { "x", "y", "z" } };   
}


@Test(dataProvider = "fetchData")
public void testMethod(String one, String two, String three) {
    System.out.println(one + two + three);
}

这里的输出是

abc
xyz

在您的情况下,由于数据是从 excel 加载的,因此无法预先确定数据的长度。所以在测试方法中有一组特定的参数是行不通的。因此,您可以做的是将 excel 读取方法返回的二维数组放入另一个二维数组中:

@DataProvider (name = "fetchData")
public String[][] getData() throws IOException {
    ReadExcel excel = new ReadExcel();
    // we return 1 row with 1 column
    return new String[][] { { excel.readExcel() } };   
}


@Test(dataProvider = "fetchData")
public void signUpWithUnicode(String[][] data) throws InterruptedException {
      // here you could loop the data and work on it...
}

【讨论】:

    【解决方案2】:

    错误是说Data provider mismatch

    您在ReadExcel 类中以Multi dimensional array 的形式返回数据,但在测试signUpWithUnicode 中以String 的形式传递数据,因此请更改参数类型。

    @Test(dataProvider = "fetchData")
        public void signUpWithUnicode(String[][] unicode) throws InterruptedException {
       //code
      }
    

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 1970-01-01
      • 2021-10-16
      • 2017-12-07
      • 2020-06-14
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多