gpt4 book ai didi

java - Selenium Java - 将 HashMap 值写入 Excel

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:41 24 4
gpt4 key购买 nike

我正在尝试使用 HashMap 值将测试用例状态写入 Excel 文件,但它不会将数据写入 Excel。这是我的代码 -

    public void readData() throws Exception {
String filePath = System.getProperty("user.dir") + "\\Test Data";
String fileName = "editSubscriptions.xls";
String sheetName = "datapool";

File file = new File(filePath + "\\" + fileName);
FileInputStream fis = new FileInputStream(file);
Workbook workbook = null;

String fileExtName = fileName.substring(fileName.indexOf("."));

if (fileExtName.equals(".xlsx")) {
workbook = new XSSFWorkbook(fis);
} else if (fileExtName.equals(".xls")) {
workbook = new HSSFWorkbook(fis);
}
Sheet sheet = workbook.getSheet(sheetName);
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
HashMap<String, String> hm = new HashMap<String, String>();
for (int i = 1; i < rowCount + 1; i++) {
Row row = sheet.getRow(i);
for (int j = 1; j < row.getLastCellNum(); j++) {
System.out.println(row.getCell(j).getStringCellValue());
driver.findElement(By.linkText("Login")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
driver.findElement(By.id("username")).sendKeys(row.getCell(0).toString());
driver.findElement(By.id("password")).sendKeys(row.getCell(1).toString());
driver.findElement(By.name("submit")).click();
Thread.sleep(10000);
try {
driver.findElement(By.linkText("Logout")).click();
WebDriverWait wait1 = new WebDriverWait(driver, 10);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Login")));
hm.put(row.getCell(0).toString(), "Pass");
} catch (Exception ex) {
hm.put(row.getCell(0).toString(), "Fail");
driver.get("http://www.openclinica.com");
}
}
}
Set<String> keys = hm.keySet();
for (String key: keys){
System.out.println("Value of "+key+" is: "+hm.get(key));

String filePath1 = System.getProperty("user.dir") + "\\Test Data";
String fileName1 = "editSubscriptions1.xls";
String sheetName1 = "datapool";

File file1 = new File(filePath1 + "\\" + fileName1);
FileInputStream fis1 = new FileInputStream(file1);
Workbook workbook1 = null;

String fileExtName1 = fileName1.substring(fileName1.indexOf("."));

if (fileExtName1.equals(".xlsx")) {
workbook1 = new XSSFWorkbook(fis1);
} else if (fileExtName1.equals(".xls")) {
workbook1 = new HSSFWorkbook(fis1);
}
Sheet sheet1 = workbook1.getSheet(sheetName1);
int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum();

for (int i=1; i < rowCount1; i++){
Cell cell = sheet1.getRow(i).createCell(2);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(hm.put(key, hm.get(key)));
}
}
}

我想使用 hashmap 键将“通过”或“失败”状态添加到 Excel 工作表中。我可以在控制台中打印状态,但这不会转换为 Excel 工作表。

盒马赛的值(value)是:失败MoulikaNimmala 的值为: 通过Keshav 的值为:失败

请帮我解决这个问题..

提前谢谢您。

最佳答案

int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum();

for (int i=1; i < rowCount1; i++){
Cell cell = sheet1.getRow(i).createCell(2);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(hm.put(key, hm.get(key)));
}

根据 doc,你的循环应该从索引 0 开始。

看起来你永远不会进入循环,因为你从索引 1 开始,但如果你从一张空表开始,你的行计数初始为 0。

编辑:在评论之后,我会寻求这样的解决方案(未经测试!)

 Set<String> keys = hm.keySet();
String filePath1 = System.getProperty("user.dir") + "\\Test Data";
String fileName1 = "editSubscriptions1.xls";
String sheetName1 = "datapool";

File file1 = new File(filePath1 + "\\" + fileName1);
FileInputStream fis1 = new FileInputStream(file1);
Workbook workbook1 = null;

String fileExtName1 = fileName1.substring(fileName1.indexOf("."));

if (fileExtName1.equals(".xlsx")) {
workbook1 = new XSSFWorkbook(fis1);
} else if (fileExtName1.equals(".xls")) {
workbook1 = new HSSFWorkbook(fis1);
}
Sheet sheet1 = workbook1.getSheet(sheetName1);
int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum(); //set rowNumber to the last already set one
for (String key: keys){ //for every key write the name in column 1 and the result in column 2
rowcount1++; //start at the next free row

Cell cell1 = sheet1.createRow(rowCount1).createCell(1);
Cell cell2 = sheet1.getRow(rowCount1).createCell(2);
cell1.setCellType(Cell.CELL_TYPE_STRING);
cell2.setCellType(Cell.CELL_TYPE_STRING);
cell1.setCellValue(key));
cell2.setCellValue(hm.get(key)));
}
}

关于java - Selenium Java - 将 HashMap 值写入 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29863331/

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