我正在自动化的工作流程是生成报告。每次生成时报告都需要X个时间(即可能需要10-50秒)。我想不出让驱动程序等到报告生成的好方法,如果没有发生ExpectedCondition,则继续。目前,我可以通过要求驱动程序查找预期条件“您的报告已完成 s ”而故意未通过测试,但问题是它不会从此处继续尝试捕获。这是它将运行的最后一行。
工作流: 我开始报告的主页 - >报告生成器弹出窗口(等待最多50秒) - >两个可能的情景:1。“另一份报告正在运行,稍后再试。” 2.“您的报告已完成”
我的硒代码:
//explicit wait
WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'" + "Your Report is complete" + "')]")));
try{
Assert.assertTrue(driver.getPageSource().contains("Your Report is complete."));
log.info("Your report was generated.");
}
catch(AssertionError ex){
log.error("Your report was not generated.");
throw ex;
}
finally {
driver.close();
// change focus back to old tab
driver.switchTo().window(oldTab);
Thread.sleep(3000);
}
页面成功生成报告时的页面源代码:
<html>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Generating report</TITLE>
<style type="text/css" media="screen">
@import url("public/css/main-new.css");
</style>
<script type="text/javascript" src="public/js/jquery.js"></script>
<script type="text/javascript" src="public/js/Modal/prototype.js" ></script>
<script type="text/javascript" src="public/js/Modal/scriptaculous.js?load=builder,effects" ></script>
<script type="text/javascript" src="public/js/Modal/modalbox.js" ></script>
<script type="text/javascript" src="public/js/tooltip.js" ></script>
<link rel="stylesheet" href="public/css/modalbox.css" type="text/css" media="screen" />
<link rel="stylesheet" href="public/css/tooltip.css" type="text/css" media="screen" />
<script language="javascript" type="text/javascript" src="JSUtility.js"></script>
</HEAD>
<body style="margin: 25px;">
<form method="post" action="LaunchReport.asp" id=form1 name=form1>
<input type="hidden" name="REPORT" value="3diFevJrtGSy9AhdaEtY8Lh5N.xls">
<input type="hidden" name="REPORTFINAL" value="3diFevJrtGSy9AhdaEtY8Lh5N.xls">
<input type="hidden" name="PPVS" value="CELL5572">
<input type="hidden" name="TYPE" value="AUDIT">
<input type="hidden" name="CLASS" value="DEFINED">
<input type="hidden" name="T" value="3">
<input type="hidden" name="MT" value="540">
</form>
<p><b><font class="mdtext">Your Report is complete.</b></font></p>
<p><font class="mdtext">Preparing to download....<br></font></p>
<SCRIPT>
document.form1.submit();</SCRIPT>
</body>
</html>
谢谢!
答案 0 :(得分:4)
我会做这样的事情:
WebDriverWait wait = new WebDriverWait(driver, 50);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'" + "Your Report is complete" + "')]")));
// If you get here, the condition did not timeout and so the
// report was generated.
log.info("Your report was generated.");
}
catch (TimeoutException ex) {
// Timed out, the report was not generated.
log.error("Your report was not generated.");
throw ex;
}
finally {
driver.close();
// change focus back to old tab
driver.switchTo().window(oldTab);
Thread.sleep(3000);
}
如果必须生成AssertionError
,则可以在catch子句中始终执行此操作:
catch (TimeoutException ex) {
// Timed out, the report was not generated.
log.error("Your report was not generated.");
Assert.assertTrue(false);
}