让驱动程序等到条件成立并在条件成立时继续的最佳方法是什么?

Jas*_*ept 3 java testng selenium webdriver selenium-webdriver

我正在自动化的工作流程用于生成报告。每次生成报告需要 X 时间(即可能需要 10-50 秒)。如果未发生预期条件,我想不出让驱动程序等到报告生成并继续的好方法。目前,我有意问司机去寻找预期的条件下测试失败“你的报告是完整的小号”,但问题是,它不会继续在尝试捕捉从这里开始。这是它将运行的最后一行。

工作流程: 我开始报告的主页 -> 报告生成器弹出窗口(最多等待 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);
}
Run Code Online (Sandbox Code Playgroud)

页面生成报表成功时的页面源码:

<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>
Run Code Online (Sandbox Code Playgroud)

谢谢!

Lou*_*uis 5

我会做这样的事情:

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);
}
Run Code Online (Sandbox Code Playgroud)

如果您必须生成一个,AssertionError您总是可以在 catch 子句中执行此操作:

catch (TimeoutException ex) {
    // Timed out, the report was not generated.
    log.error("Your report was not generated.");
    Assert.assertTrue(false);
}
Run Code Online (Sandbox Code Playgroud)