【发布时间】:2016-01-23 02:29:30
【问题描述】:
当我运行我的 Selenium 脚本时,我使用的断言方法 sorta 有效。当满足断言(元素存在)时,脚本将写入 xls 文件。如果不满足,脚本不会写入 xls 文件,它会在此处停止测试。
try {
assertEquals(driver.findElement(By.xpath(prop.getProperty("failedlogin"))).getText(), "LOG IN");
//add pass entry to the excel sheet
testresultdata.put("4", new Object[] {
3d, "User should not be able to login with no password", "Login failed", "Pass"
});
} catch (Exception e) {
//add fail entry to the excel sheet
testresultdata.put("4", new Object[] {
3d, "User should not be able to login with no password", "Login failed", "Fail"
});
}
这是我的 excel 编写器方法:
@BeforeClass(alwaysRun = true)
public void setupBeforeSuite(ITestContext context) throws IOException {
//create a new work book
workbook = new HSSFWorkbook();
//create a new work sheet
sheet = workbook.createSheet("Test Result");
testresultdata = new LinkedHashMap < String, Object[] > ();
//add test result excel file column header
//write the header in the first row
testresultdata.put("1", new Object[] {
"Test Step Id", "Action", "Expected Result", "Actual Result"
});
}
还有:
@AfterClass
public void setupAfterSuite(ITestContext context) {
//write excel file and file name is TestResult.xls
Set < String > keyset = testresultdata.keySet();
int rownum = 0;
for (String key: keyset) {
Row row = sheet.createRow(rownum++);
Object[] objArr = testresultdata.get(key);
int cellnum = 0;
for (Object obj: objArr) {
Cell cell = row.createCell(cellnum++);
if (obj instanceof Date) cell.setCellValue((Date) obj);
else if (obj instanceof Boolean) cell.setCellValue((Boolean) obj);
else if (obj instanceof String) cell.setCellValue((String) obj);
else if (obj instanceof Double) cell.setCellValue((Double) obj);
}
}
try {
FileOutputStream out = new FileOutputStream(new File("C:/Users/matt_damon/workspace/project/passfail.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
那么,为什么即使我故意让我的测试失败,excel 编写器也不写失败条目?
【问题讨论】:
标签: java excel selenium try-catch