Jér*_*nge 5 javascript casperjs
有一个waitForUrl()在功能Casper.js,但有可能waitForUrlChange()在Casper.js?
我的意思是检测this.getCurrentUrl()价值的变化.我无法预测新的网址值.它可以是任何东西.
它有一个事件处理程序
casper.on('url.changed',function(url) {
casper.echo(url);
});
Run Code Online (Sandbox Code Playgroud)
这是它的文档:http://casperjs.readthedocs.org/en/latest/events-filters.html#url-changed
但是,正如Artjom B.所提到的,这不会涵盖函数扩展将处理的所有情况.当你不需要它作为控制流的一部分时,它才真正合适,但只是想在它发生时反应性地刮掉一些值.
不是内置的,但你可以写自己很容易:
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;
};
Run Code Online (Sandbox Code Playgroud)
这是一个正确的函数扩展,因为它具有与其他wait*函数相同的语义(参数是可选的并且它等待)并且它支持构建器模式(也被某些人称为promise模式).
正如Darren Cook 所提到的,可以通过检查waitForUrlChangeCasperJS中是否已经存在以及使用动态参数列表来确定CasperJS何时更改其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;
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5351 次 |
| 最近记录: |