Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent redirects in CasperJS?

I am using CapserJS 1.1.0-beta3 combined with PhantomJS 1.8.2.

I call an url which respond with a redirect (HTTP 302). PhantomJS automatically follows the redirect, but in my usecase PhantomJS should not follow the redirect.

The debug output of the redirect looks like:

[debug] [phantom] Navigation requested: url=https://foo.com/bar.jsp, type=Other, willNavigate=true, isMainFrame=true    

How can I configure PhantomJS/CapserJS to not follow redirects?

like image 402
Lavezzi Avatar asked Nov 19 '14 15:11

Lavezzi


1 Answers

There is a little workaround necessary. So you need to first identify which URL is the redirect. With resource.received you receive the response to the first request which contains the URL where it should be redirected to. But we can't do anything from this event handler. We therefore save the target URL which is identified as a redirect target for later.

Now the underlying headless browser (PhantomJS or SlimerJS) follows the redirect by requesting the new resource, but now resource.requested provides us with the tools to abort the request (sadly this is not documented in CasperJS). So the final script looks like this:

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

var redirectURLs = [],
    doLog = true;

casper.on("resource.requested", function(requestData, networkRequest){
    if (doLog) console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData) + "\n");
    if (redirectURLs.indexOf(requestData.url) !== -1) {
        // this is a redirect url
        networkRequest.abort();
    }
});

casper.on("resource.received", function(response){
    if (doLog) console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response) + "\n");
    if (response.status === 301) { // use your status here
        redirectURLs.push(response.redirectURL);
    }
});

casper.start("https://stackoverflow.com/q/27021176").run(function(){
    this.echo("DONE");
    this.exit();
});

This is adapted from my answer A: How to configure Poltergeist or PhantomJS to not follow redirects?

You could do the same as with the linked PhantomJS version directly in CasperJS by exchanging page for casper.page, but CasperJS has a few advantages. You can add multiple handlers to the same events with the casper.on notation and most all you can decide if all resources are treated the same way or just page loads. So you can exchange resource.received for page.resource.received and resource.requested for page.resource.requested.

like image 172
Artjom B. Avatar answered Nov 12 '22 23:11

Artjom B.