量角器 - 通用等待 URL 更改 [英] Protractor- Generic wait for URL to change
问题描述
在之前的问题中,我看到等待 url 更改的一个好方法是使用:
In previous questions I have seen that a nice way to wait for the url to change is to use:
browser.wait( function() {
return browser.getCurrentUrl().then(function(url) {
return /myURL/.test(url);
});
}, 10000, "url has not changed");`
但我正在尝试一种方法,我可以将 myURL 作为变量传递(以防我需要将它与其他网站一起使用)并且不起作用.
But I am trying to have a method that I can pass myURL as a variable (in case I need to use it with other sites) and is not working.
我正在我的页面对象文件中尝试这个:
I am trying this in my Page Object file:
this.waitUrl = function(myUrl) {
browser.wait( function(myUrl) {
return browser.getCurrentUrl().then(function(url, myUrl) {
return myUrl.test(url);
});
}, 10000, "url has not changed");
};
是否有任何想法,如果可能,以及如何做到这一点?
Any ideas if this is even possible and how to do it if so?
推荐答案
更新(2016 年 7 月): 使用 Protractor 4.0.0 你可以用 urlIs
和 urlContains
内置预期条件.
Update (July 2016): with Protractor 4.0.0 you can solve it with urlIs
and urlContains
built-in Expected Conditions.
原答案:
不要在then
函数内部传递myUrl
,它可以在页面对象函数作用域中使用:
Don't pass myUrl
inside the then
function, it is available from the page object function scope:
browser.wait(function() {
return browser.getCurrentUrl().then(function(url) {
return myUrl.test(url);
});
}, 10000, "url has not changed");
我想把它定义为一个预期条件:
function waitUrl (myUrl) {
return function () {
return browser.getCurrentUrl().then(function(url) {
return myUrl.test(url);
});
}
}
这样你就可以这样使用它:
So that you can then use it this way:
browser.wait(waitUrl(/my.url/), 5000);
这篇关于量角器 - 通用等待 URL 更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!