我想在excel文件中使用hashmap值來寫入testcase狀態,但它並沒有將數據寫入excel。這裏是我的代碼 -Selenium Java - 將hashmap值寫入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表格。
HemaSai的價值是:失敗 值MoulikaNimmala的是:通過 值凱沙夫的是:失敗
請幫我解決這個問題..
預先感謝您。
什麼是在excelFile結果? –
用戶名密碼\t結果 MoulikaNimmala \t $ Ai11081988 HemaSai \t sai11081988 凱沙夫\t sai30041990 –
這是一排?我無法想象此刻哪些是行和哪些單元格 –