gpt4 book ai didi

java - 断言 try/catch block 未按预期工作

转载 作者:行者123 更新时间:2023-12-01 16:55:35 28 4
gpt4 key购买 nike

当我运行 Selenium 脚本时,我使用的断言方法有点可以工作。当满足断言(元素存在)时,脚本将写入 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 编写器也不写入失败条目?

最佳答案

检查 aseertEquals() 实际执行的操作。最有可能的是它抛出java.lang.AssertionError,它不是java.lang.Exception的子类。

这至少是 JUnit 的 Assert.assert*() 方法所做的事情。

关于java - 断言 try/catch block 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33304267/

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