【问题标题】:protractor asnchronus execution issue量角器异步执行问题
【发布时间】:2016-05-13 02:54:05
【问题描述】:

我已经使用/学习量角器一个多月了。

我知道量角器文档说,它等待角度调用完成 (http://www.protractortest.org/#/),它将确保所有 步骤同步执行等等。

但我不这么认为。或者至少,我在我的脚本中没有找到这种方式 很多时候量角器会提前运行,例如,如果我单击链接,获取当前 url,然后验证 url。

大多数情况下,URL 值将是陈旧的,即点击链接后它没有被执行。下面是我的页面对象和相应测试的代码示例。

请建议如何确保,所有测试步骤都按顺序执行

Page Object
this.getSPageLink(){
    return element(by.xpath("//a[@title='S']"));
};
this.getLPageLink(){
    return element(by.xpath("//a[@title='L']"));
};
this.goToSPage = function() {
    (this.getSPageLink()).click();
    *//ERROR here, sometimes second click (below) doesn't wait for first 
    click to complete, and complains that link for 2 click (below) is 
    not   found*
    (this.getSLPageLink()).click();
    return browser.currentURL();
    *//ERROR HERE url line (e) is sometimes executed before*
}

Test Class
it('::test SL  page', function() {
        pageObject.goToSpage();
        var currentURL=browser.getCurrentURL();
        expect(currentURL).toContain("SLink");
        *//ERROR HERE value in this variable "currentURL" is most of the 
        times Stale*
});

it('::test SL2  page', function() {
        pageObject.goToLpage();
        var currentURL=browser.getCurrentURL();
        expect(currentURL).toContain("Link");
        console.log("this line is always executed first"));
        //ERROR here , this print line is always executed first
});

【问题讨论】:

    标签: javascript angularjs protractor


    【解决方案1】:

    这相当令人困惑,Protractor 文档对此不是很清楚,但 Protractor 仅在使用 getrefresh 命令时才等待页面同步。在您的情况下,您正在使用 click 命令加载新页面,因此 Protractor 在继续之前不会等待新页面同步。 See the reasoning 来自 Protractor 开发人员的这种行为。

    因此,在您的情况下,您需要在测试或页面对象中处理该问题。无论哪种情况,您都可以在点击后使用类似的东西:

    browser.wait(function () {
        return browser.driver.getCurrentUrl().then(function (url) {
            return /SLink/.test(url);
        });
    }, 10000);
    

    这将尽可能频繁地轮询当前 URL(实际上大约每 500 毫秒一次)长达 10 秒。当它在当前 URL 中找到“Slink”字符串时,它将继续执行下一个命令。

    【讨论】:

    • 感谢您的精彩解释。
    • 另外,是否有一种首选方法可以确保所有测试行(IT 块)都连续执行?例如,console.log 总是首先执行,而不是按照它出现的顺序。
    • 在 webdriverjs/protractor 绑定中实际上没有 500 毫秒的轮询间隔..stackoverflow.com/a/34377095/771848。谢谢。
    • @alecxe 这就是我在原帖中“尽可能多地”写的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多