Protractor- Generic等待URL改变

Jam*_* Be 9 javascript testing automation asynchronous protractor

在之前的问题中,我已经看到等待url更改的好方法是使用:

browser.wait( function() {
    return browser.getCurrentUrl().then(function(url) {
        return /myURL/.test(url);
    });
}, 10000, "url has not changed");`
Run Code Online (Sandbox Code Playgroud)

但我试图有一个方法,我可以将myURL作为变量传递(如果我需要将其与其他网站一起使用)并且无法正常工作.

我在我的Page Object文件中尝试这个:

this.waitUrl = function(myUrl) {
    browser.wait( function(myUrl) {
        return browser.getCurrentUrl().then(function(url, myUrl) {
            return myUrl.test(url);
        });
    }, 10000, "url has not changed");
};
Run Code Online (Sandbox Code Playgroud)

如果这是可能的任何想法,如果这样做怎么办?

ale*_*cxe 11

更新(2016年7月):使用Protractor 4.0.0,您可以使用urlIsurlContains内置预期条件来解决它.


原始答案:

不要传递函数myUrl内部then,它可以从页面对象函数范围获得:

browser.wait(function() {
    return browser.getCurrentUrl().then(function(url) {
        return myUrl.test(url);
    });
}, 10000, "url has not changed");
Run Code Online (Sandbox Code Playgroud)

我会把它定义为预期条件:

function waitUrl (myUrl) {
    return function () {
        return browser.getCurrentUrl().then(function(url) {
            return myUrl.test(url);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这样你就可以这样使用它:

browser.wait(waitUrl(/my\.url/), 5000);
Run Code Online (Sandbox Code Playgroud)


Rma*_*moe 6

对于那些想要Protractor 4.0.0 到 5.3.0示例的人

您可以像这样使用“ExpectedConditions”...

var expectedCondition = protractor.ExpectedConditions;
// Waits for the URL to contain 'login page'.
browser.wait(expectedCondition.urlContains('app/pages/login'), 5000);
Run Code Online (Sandbox Code Playgroud)

如果您想通过 e2e 测试验证这一点。

it('should go to login page', function() {
    loginPage.login();
    const EC = protractor.ExpectedConditions;

    browser.wait(EC.urlContains('app/pages/login'), 5000).then(function(result) {
        expect(result).toEqual(true);
    });

});
Run Code Online (Sandbox Code Playgroud)