如何在webdriver中有效地使用流畅的wait命令?

时间:2014-12-10 11:50:16

标签: java eclipse selenium webdriver

我有一个场景,我想在页面上点击合约编号的超链接以打开另一个窗口。我最初遇到的问题是,此合同号在页面上显示之前需要几秒钟。因此,我认为我会利用流畅的等待每隔几秒轮询元素上的搜索,直到它变得可见(我想避免使用Thread.Sleep)。但是,我遇到了解决这个问题的问题。我在下面详细介绍了我做过的事情

1)请参阅下面的我试图点击的合同的截图(254052)。这是合约,它在页面上显示之前需要几秒钟 screenshot

相关的HTML如下:

<div id="tabular-breakdown" class="tablesorter" style="display: block;">
<table class="tablesorterReport" data-currentpage="0" data-totalcount="696" data-totalpages="35">
<thead>
<tbody>
    <tr>
        <td>
            <a class="" target="_blank" href="/DibsAndrew/CreditControl/AgreementDetail.aspx?contractno=254052">254052</a>

2)所以,我想要实现的是等待合同号显示在列表上,之后我会点击它。我已经为fluentWait编写了代码,如下所示:

    public static void fluentWaitOnContractSelect(InternetExplorerDriver driver)
{
    FluentWait<InternetExplorerDriver> wait = new FluentWait<InternetExplorerDriver>(driver);
    wait.withTimeout(8000, TimeUnit.MILLISECONDS);
    wait.pollingEvery(10, TimeUnit.MILLISECONDS);
    wait.ignoring(NoSuchElementException.class);
    wait.ignoring(StaleElementReferenceException.class);

    WebElement contractSelect = wait.until(new Function<InternetExplorerDriver, WebElement>(){

        public WebElement apply(InternetExplorerDriver driver) {
            WebElement contract = driver.findElement(By.xpath(".//*[@id='tabular-breakdown']/table/tbody/tr[1]/td[1]/a"));
            String value = contract.getAttribute("innerHTML");
            if(value.contains("_blank"))
            {
                contract.click();
                return contract;            
            }
            else
            {
                System.out.println("Value is " + value);
                return null;
            }
        }
    });
    System.out.println("Webelement value is " + contractSelect.getTagName());

}

3)然而,当我运行测试时,我在控制台中收到错误,如下所示:

“exception in thread “main” org.openqa.selenium.TimeoutException: Timed out after 8 seconds     waiting for genericControls.waitCommands$1@1b68b9a4
Build info: version: ‘2.43.1’, revision: ‘5163bce’, time: ‘2014-09-10 16:27:58′
System info: host: ‘MBD0150′, ip: ‘192.168.55.49’, os.name: ‘Windows 8.1′, os.arch: ‘amd64′,   os.version: ‘6.3’, java.version: ‘1.8.0_25′
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:259)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
at genericControls.waitCommands.fluentWaitOnContractSelect(waitCommands.java:24)
at   genericControls.contractFunctions.openCloseContractForReportWithBarChart(contractFunctions.java:41)
at Reports.collections.breachReportCompletedSelectContract(collections.java:55)
at Reports.programMain.main(programMain.java:117)
Caused by: org.openqa.selenium.StaleElementReferenceException: Element is no longer valid     (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 57 milliseconds
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Session ID: e9003b03-0004-4846-a0b9-6991cadac9b0
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.getAttribute(RemoteWebElement.java:123)
at genericControls.waitCommands$1.apply(waitCommands.java:28)
at genericControls.waitCommands$1.apply(waitCommands.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)”

有没有人对我可能出错的地方有任何想法?

非常感谢

&#39;蚀-10/12/14 39#; enter image description here

1 个答案:

答案 0 :(得分:0)

您正在检查innerHTML元素的a。您应该针对outerHTML进行测试。 innerHTML值将从不包含属性,因此永远不会包含您要测试的_blank值。 (好吧,除非你有一个名为_blank的合同,这似乎不太可能。)让我们举一个例子:

<a class="" target="_blank" href="/DibsAndrew/CreditControl/AgreementDetail.aspx?contractno=254052">254052</a>

以上innerHTML254052,因为innerHTML仅涵盖元素内部的内容。 outerHTML是上面的整个字符串,因为outerHTML涵盖元素本身及其内容。

好的,我上面建议的那样可以解决眼前的问题。但是,我强烈建议您将测试更改为比匹配HTML 中的字符串更好的内容。例如,如果仅在加载契约后在其父单元格中添加a元素,则应测试单元格中是否存在a元素。