[英]Casperjs, how to only proceed after receiving response from an ajax call
我想让casperjs进行ajax调用,但是等待我服务器的结果。 这可能需要3分钟,但我可以通过查看运行我的脚本的结果来判断,casper.then函数超时并且仅在30秒后继续运行。 我试过把casper.options.waitTimeout = 180000 / * 3分钟* /; 在我的代码中,它确实有效,并且我尝试使用这段代码,无论我的api调用结果如何,它似乎每次都要等待3分钟。
我也知道,evaluate函数每次都返回一个布尔值,无论如何,这都行不通,因为我需要在我脚本的其余部分返回的api调用数据。 如何让这个功能等待3分钟? 幕后有很多事情发生,所以是的,我需要等待这么久。
var casper = require('casper').create();
casper.start('mysite.html', function() {
});
casper.then(function() {
result = getSomethingFromMyServerViaAjax( theId );
});
这是我尝试过的另一种方法,似乎总是等待3分钟,无论ajax调用的速度如何。
casper.waitFor(function check() {
return this.evaluate(function() {
return result = getSomethingFromMyServerViaAjax( theId ) /* this takes up to 3 minutes */;
});
}, function then() {
casper.log("Doing something after the ajax call...", "info");
this.die("Stopping here for now", "error");
}, 180000 );
我已经在其他地方测试了我的ajax调用,只要响应在30秒内返回,但是如果它没有casper只是跳过那个阻塞并且每次都继续运行。
你快到了。 您需要触发长时间通话。 它似乎是同步的所以我把它放在setTimeout
。 结果是在一段时间后写入window.resultFromMyServerViaAjax
。
this.evaluate
也是同步的,但在执行之后,将安排wait
步骤并定期测试是否设置了window属性。
var casper = require('casper').create(),
theId = "#whatever";
casper.start('mysite.html');
casper.then(function() {
// trigger
this.evaluate(function(theId){
window.resultFromMyServerViaAjax = null;
setTimeout(function(){
window.resultFromMyServerViaAjax = getSomethingFromMyServerViaAjax(theId);
}, 0);
}, theId);
this.waitFor(function check() {
return this.evaluate(function() {
return !!window.resultFromMyServerViaAjax;
});
}, function then() {
casper.log("Doing something after the ajax call...", "info");
}, function onTimeout() {
this.die("Stopping here for now", "error");
}, 180000 );
});
casper.run();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.