Casperjs,如何仅在收到来自ajax调用的响应后继续进行 [英] Casperjs, how to only proceed after receiving response from an ajax call

查看:111
本文介绍了Casperjs,如何仅在收到来自ajax调用的响应后继续进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望casperjs进行ajax调用,但要等待服务器的结果。这可能需要3分钟的时间,但是从运行脚本的结果来看,我可以判断出casper.then函数超时,并且恰好在30秒后继续运行。我尝试将casper.options.waitTimeout = 180000 / * 3分钟* /;在我的代码中有效,并且我尝试使用此代码块,无论我的api调用结果如何,它似乎每次都等待3分钟。

I want casperjs to make an ajax call but wait for the result from my server. This might take up to 3 minutes, but I can tell from looking at the results of running my script, casper.then function times out and just keeps going after exactly 30 seconds. I've tried putting casper.options.waitTimeout = 180000 /* 3 minutes */; in my code, which does work, and I've tried using this block of code, which seems to wait 3 minutes every single time regardless of the result of my api call.

我也知道,无论何时,每次都只对Evaluate函数返回一个布尔值,这将不起作用,因为我需要在我的脚本的其余部分。如何让此功能等待3分钟?幕后发生了很多事情,所以是的,我需要等待很长时间。

I also know that the evaluate function returns only a boolean every time no matter what, and this won't work as I need the api call data that is returned in the rest of my script. How can I get this function to wait for 3 minutes? And there is a lot going on behind the scenes, so yes, I need to wait this long.

var casper = require('casper').create();

casper.start('mysite.html', function() {
});

casper.then(function() {
  result = getSomethingFromMyServerViaAjax( theId );
});

这是我尝试过的另一种方法,无论其速度如何,似乎总是要等待3分钟

This is the alternate method that I've tried that seems to always wait 3 minutes regardless or speed of the ajax call coming back.

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 );

只要响应在30以下,我就已经在其他地方测试过我的Ajax调用并且可以正常工作秒钟,但如果没有,则只需跳过该块并每次都继续。

I have tested my ajax call elsewhere and it works, as long as the response comes back in under 30 seconds, but if it doesn't casper just skips that block and keeps going every time.

推荐答案

您快到了。您需要触发长时间运行的通话。看来它是同步的,所以我将其放在 setTimeout 中。一段时间后将结果写入 window.resultFromMyServerViaAjax

You were nearly there. You need to trigger your long running call. It seems that it is synchronous so I put it inside setTimeout. The result is written after some time into window.resultFromMyServerViaAjax.

this.evaluate 也是同步的,但是执行后, wait 步骤已安排好,并定期测试是否设置了窗口属性。

this.evaluate is also synchronous, but after it is executed, the wait step is scheduled and tests periodically whether the window property is set.

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();

这篇关于Casperjs,如何仅在收到来自ajax调用的响应后继续进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆