Skip to content Skip to sidebar Skip to footer

Casperjs, How To Only Proceed After Receiving Response From An Ajax Call

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, casp

Solution 1:

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 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(functioncheck() {
        returnthis.evaluate(function() {
            return !!window.resultFromMyServerViaAjax;
        });
    }, functionthen() {
       casper.log("Doing something after the ajax call...", "info");
    }, functiononTimeout() {
       this.die("Stopping here for now", "error");
    }, 180000 );
});
casper.run();

Post a Comment for "Casperjs, How To Only Proceed After Receiving Response From An Ajax Call"