【问题标题】:tell Protractor to wait for the page before executing expect告诉量角器在执行期望之前等待页面
【发布时间】:2015-03-04 15:30:41
【问题描述】:

当我单击导出按钮时,它会对我们的端点进行 REST 调用,然后几秒钟后,我收到了响应,然后我也呈现了表格。不幸的是,我读到每个调用都是异步的,这意味着即使表尚未呈现,我的期望也会被执行。我写的期望检查字符串是否在桌子上,但它失败了,因为它还不在那里。解决这个问题的正确方法是什么?

it('should generate global user report', function() {
    element(by.css('button#exportButton')).click();

    expect(element(by.css("th[name*=Date]")).getText()).
        toEqual('Date');
})

控制台上的错误是

NoSuchElementError: No element found using locator: By.cssSelector("th[name*=Date]")

我注意到该表尚未呈现,这就是它失败的原因。

【问题讨论】:

    标签: angularjs selenium testing protractor e2e-testing


    【解决方案1】:

    Protractor 1.7 引入了一个名为"Expected Conditions" 的功能,可以在此处应用。

    等待元素变得可见

    var EC = protractor.ExpectedConditions;
    var elm = element(by.css("th[name*=Date]"));
    
    browser.wait(EC.visibilityOf(elm), 5000);
    expect(elm.getText()).toEqual('Date');
    

    【讨论】:

    • 酷!我现在要试试。谢谢!
    • 出现错误 - TypeError:无法调用未定义的方法“visibilityOf”。我运行 protractor --version 并得到 1.3.1 看起来我需要更新我的量角器二进制文件。对吗?
    • @devwannabe 正确,无论如何 1.3.1 太旧了 :)
    • 在 2015 年 5 月 5 日撰写此评论时与新版本相关的唯一正确答案。谢谢
    【解决方案2】:

    我在等待动态元素出现时遇到问题。让驱动程序等待它出现或显示。最后的数字是超时时间。

    element(by.css('button#exportButton')).click();
    var header = element(by.css("th[name*=Date]"));
    browser.driver.wait(function() {
        return header.isPresent();
    }, 1000);
    expect(header.getText()).toEqual('Date');
    

    在测试完全稳定之前,我必须等到它出现并显示出来。你可以这样做:

    var header = element(by.css("th[name*=Date]"));        
    browser.driver.wait(function() {
        return header.isPresent().then(function(present) {
            if (present) {
                return header.isDisplayed().then(function(visible) {                   
                    return visible;
                });
            } else {
                return false;
            }
        });
    }, 1000);
    

    【讨论】:

    • 下车后我会尝试您的解决方案。量角器到 1.8 的更新也适用于上面发布者的代码。由于使用了'then',我也会使用你的代码
    • 使用我的旧量角器也可以!谢谢艾伦·亚克尔! :)
    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 2019-10-03
    • 1970-01-01
    相关资源
    最近更新 更多