【发布时间】: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