让驱动程序等待条件为真并在条件为假时继续的最佳方法是什么?
What's the best way to make the driver wait until a condition is true and continue if it's false?
我正在自动化的工作流程是为了生成报告。每次生成报告都需要 X 的时间(即可能需要 10-50 秒)。如果 ExpectedCondition 没有发生,我想不出让驱动程序等到报告生成并继续的好方法。目前,我可以通过要求驱动程序寻找预期条件 "Your Report is completes" 来故意使测试失败,但问题是它不会从这里继续到 try catch。这是最后一行 运行.
工作流程:
我开始报告的主页 -> 报告生成器弹出窗口 window(最多等待 50 秒) -> 两种可能的情况:1. "Another report is running, try again later." 2. "Your Report is complete"
我的硒代码:
//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>
谢谢!
我会这样做:
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);
}
我正在自动化的工作流程是为了生成报告。每次生成报告都需要 X 的时间(即可能需要 10-50 秒)。如果 ExpectedCondition 没有发生,我想不出让驱动程序等到报告生成并继续的好方法。目前,我可以通过要求驱动程序寻找预期条件 "Your Report is completes" 来故意使测试失败,但问题是它不会从这里继续到 try catch。这是最后一行 运行.
工作流程: 我开始报告的主页 -> 报告生成器弹出窗口 window(最多等待 50 秒) -> 两种可能的情况:1. "Another report is running, try again later." 2. "Your Report is complete"
我的硒代码:
//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>
谢谢!
我会这样做:
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);
}