gpt4 book ai didi

java - TestNG 中的 DataProvider 使用 Java WebDriver 从 Excel 传递数据

转载 作者:行者123 更新时间:2023-11-29 05:45:52 29 4
gpt4 key购买 nike

我有一个包含用户名和密码的登录页面。我有一个类 XcelParserTestNGLogin 来创建、更新和从 Excel 工作表方法加载。另一个类 Login 是一个 TestNG 类。我正在使用 DataProvider 从 Excel 传递数据。但我得到异常数据提供者试图传递 4 个参数,但该方法需要 2 个

这是我的 TestNG 代码:

public class Login {

private static WebDriver driver;
XcelParserTestNGLogin login1 = new XcelParserTestNGLogin();
Object[][] data1;

/*public Login() throws IOException, InterruptedException {
FileInputStream fis = new FileInputStream("Data//LoginPage.xls");
XcelParserTestNGLogin login1 = new XcelParserTestNGLogin(fis, "Login");

//this.data1 = login1.loadFromSpreadsheet(fis, "Login");
}*/

@BeforeClass
public void test() throws Exception {
System.setProperty("webdriver.chrome.driver",
"C:\\Chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("Any Url");
}
@DataProvider
public Object[][] dp() throws IOException {
//login1.fileName = "Data//Login.xls";
//login1.sheetName = "Sheet1";
FileInputStream fis = new FileInputStream("Data//LoginPage.xls");
String sheetName = "Login";
login1.loadFromSpreadsheet(fis,sheetName);
return login1.getData();
}

@Test(dataProvider = "dp")
public void devLogin(String UserName,String PassWord) throws InterruptedException, IOException {

driver.findElement(By.name("txtUserName")).sendKeys(UserName);
driver.findElement(By.name("txtPwd")).sendKeys(PassWord);
driver.findElement(By.name("btnSignIn")).click();
Thread.sleep(5000);

if (driver.findElement(By.linkText("DashBoard")).isDisplayed()) {
List<String> arrayList = new ArrayList<String>();
arrayList.add("Pass");
HSSFWorkbook workbook = new HSSFWorkbook();
login1.createSheet("Login", workbook, arrayList);
}
else {
try{
Alert alert=driver.switchTo().alert();
String alertText=alert.getText();

Assert.assertEquals("invalid username or password,please try again",alertText);
alert.accept();
}catch(UnhandledAlertException e){
e.printStackTrace();
}
List<String> arrayList = new ArrayList<String>();
arrayList.add("Fail");
HSSFWorkbook workbook = new HSSFWorkbook();
login1.createSheet("Login", workbook, arrayList);
}
}
}

这是我的 XcelParserTestNGLogin() 代码

public class XcelParserTestNGLogin {
private transient Object[][] data;
String fileName,sheetName;

public XcelParserTestNGLogin() {

}

public XcelParserTestNGLogin(InputStream excelInputStream, String sheetName)
throws IOException {
this.data = loadFromSpreadsheet(excelInputStream, sheetName);
}

public Object[][] getData() {
return data;

}

Object[][] loadFromSpreadsheet(InputStream excelFile, String sheetName)
throws IOException {
// TODO Auto-generated method stub
HSSFWorkbook workbook = new HSSFWorkbook(excelFile);
Sheet sheet = workbook.getSheet(sheetName);

int numberOfColumns = countNonEmptyColumns(sheet);
int numberOfRows = sheet.getLastRowNum() + 1;

data = new Object[numberOfRows - 1][numberOfColumns - 1];

for (int rowNum = 1; rowNum < numberOfRows; rowNum++) {
Row row = sheet.getRow(rowNum);
if (isEmpty(row)) {
break;
} else {
for (int column = 1; column < numberOfColumns; column++) {
Cell cell = row.getCell(column);
if (cell == null
|| cell.getCellType() == Cell.CELL_TYPE_BLANK) {
data[rowNum - 1][column - 1] = "";
} else {
data[rowNum - 1][column - 1] = objectFrom(workbook,
cell);
}
}
}
}

return data;
}

private boolean isEmpty(Row row) {
// TODO Auto-generated method stub
Cell firstCell = row.getCell(0);
boolean rowIsEmpty = (firstCell == null)
|| (firstCell.getCellType() == Cell.CELL_TYPE_BLANK);
return rowIsEmpty;
}

/**
* Count the number of columns, using the number of non-empty cells in the
* first row.
*/
private int countNonEmptyColumns(Sheet sheet) {
// TODO Auto-generated method stub
Row firstRow = sheet.getRow(0);
return firstEmptyCellPosition(firstRow);
}

private int firstEmptyCellPosition(Row cells) {
// TODO Auto-generated method stub
int columnCount = 0;
for (Cell cell : cells) {
if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
break;
}
columnCount++;
}
return columnCount;
}

private Object objectFrom(HSSFWorkbook workbook, Cell cell) {
// TODO Auto-generated method stub
Object cellValue = null;
if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
cellValue = cell.getRichStringCellValue().getString();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
cellValue = getNumericCellValue(cell);
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
cellValue = cell.getBooleanCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
cellValue = evaluateCellFormula(workbook, cell);
}

return cellValue;
}

private Object getNumericCellValue(final Cell cell) {
Object cellValue;
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = new Date(cell.getDateCellValue().getTime());
} else {
cellValue = cell.getNumericCellValue();
}
return cellValue;
}

private Object evaluateCellFormula(final HSSFWorkbook workbook,
final Cell cell) {
FormulaEvaluator evaluator = workbook.getCreationHelper()
.createFormulaEvaluator();
CellValue cellValue = evaluator.evaluate(cell);
Object result = null;

if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
result = cellValue.getBooleanValue();
} else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
result = cellValue.getNumberValue();
} else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) {
result = cellValue.getStringValue();
}

return result;
}
public void updateExcel(final InputStream excelFile, String SheetName,
List<String> list) {
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = null;
if (workbook.getSheetIndex(SheetName) > 0) {
sheet = workbook.getSheet(SheetName);
if (list != null && list.size() != sheet.getLastRowNum()) {
workbook.removeSheetAt(workbook.getSheetIndex(SheetName));
createSheet(SheetName, workbook, list);
} else {
createSheet(SheetName, workbook, list);
}
}

}
void createSheet(String SheetName, HSSFWorkbook workbook, List<String> list) {
// TODO Auto-generated method stub
String[] Heading = {"UserName", "Password",
"Result" };
Sheet sheet = workbook.createSheet(SheetName);
HSSFRow row = null;
HSSFCell cell = null;

row = (HSSFRow) sheet.createRow(0);
for (int cellNum = 0; cellNum < Heading.length; cellNum++) {
cell = row.createCell(cellNum);
cell.setCellValue(Heading[cellNum]);
}
for (int rowNum = 1; rowNum <= list.size(); rowNum++) {
String[] cellVals = {"uname",
"pswd", list.get(rowNum - 1) };

row = (HSSFRow) sheet.createRow(rowNum);
for (int cellNum = 0; cellNum < cellVals.length; cellNum++) {
cell = row.createCell(cellNum);
if (!(cellNum == cellVals.length))
cell.setCellValue(cellVals[cellNum]);
else
cell.setCellValue(true);
}
}
try {
FileOutputStream out = new FileOutputStream("Data//LoginPage.xls");
workbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

最佳答案

请记住,对象数组左侧第一个方括号的长度表示您的测试方法将被调用多少次。

第二个方括号的长度表示您的测试方法应该有多少个参数。根据您的测试方法的签名,您的数组声明应该是 obj = new Object[maxRows][2]

关于java - TestNG 中的 DataProvider 使用 Java WebDriver 从 Excel 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15873406/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com