等待Casper.js中的URL更改? [英] Wait for URL change in Casper.js?
问题描述
Casper.js
中有 waitForUrl()
功能,但是可以 waitForUrlChange()
in Casper.js
?
There is a waitForUrl()
functionality in Casper.js
, but is it possible to waitForUrlChange()
in Casper.js
?
我的意思是检测到更改在 this.getCurrentUrl()
值。我无法预测新的网址值。它可以是任何东西。
I mean detecting a change in this.getCurrentUrl()
value. I can't predict the new url value. It can be anything.
推荐答案
不是内置的,但是你可以自己写很容易:
Not built in, but you can write your own pretty easy:
casper.waitForUrlChange = function(then, onTimeout, timeout){
var oldUrl;
this.then(function(){
oldUrl = this.getCurrentUrl();
}).waitFor(function check(){
return oldUrl === this.getCurrentUrl();
}, then, onTimeout, timeout);
return this;
};
这是一个正确的函数扩展,因为它与其他 wait *
函数(参数是可选的,它等待),它支持构建器模式(也被一些人称为promise模式)。
This is a proper function extension, because it has the same semantics as the other wait*
functions (arguments are optional and it waits) and it supports the builder pattern (also called promise pattern by some).
As Darren Cook 提到,一个可以通过检查CasperJS中是否已存在 waitForUrlChange
以及CasperJS何时更改其API时使用动态参数列表来进一步改进:
As mentioned by Darren Cook, one could improve this further by checking whether waitForUrlChange
already exists in CasperJS and using a dynamic arguments list for when CasperJS changes its API:
if (!casper.waitForUrlChange) {
casper.waitForUrlChange = function(){
var oldUrl;
// add the check function to the beginning of the arguments...
Array.prototype.unshift.call(arguments, function check(){
return oldUrl === this.getCurrentUrl();
});
this.then(function(){
oldUrl = this.getCurrentUrl();
});
this.waitFor.apply(this, arguments);
return this;
};
}
这篇关于等待Casper.js中的URL更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!