Wait for the URL in Casper.js to change?

There Casper.jsis a function in waitForUrl(), but is it possible waitForUrlChange()in Casper.js?

I mean the definition of a change in value this.getCurrentUrl(). I cannot predict the new URL value. It could be anything.

+4
source share
2 answers

Not built-in, but you can write your own quite easily:

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

This is the correct extension of the function because it has the same semantics as other functions wait*(the arguments are optional, and it waits), and it supports the builder pattern (also called the promise pattern by some).

, , , waitForUrlChange CasperJS , 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;
    };
}
+6

casper.on('url.changed',function(url) {
casper.echo(url);
});

: http://casperjs.readthedocs.org/en/latest/events-filters.html#url-changed

, Artjom B., , . , , , , .

+7

All Articles