Question

How do you check if there is a redirection with casper (+phantom) ? Try it :

casper.test.begin('\n********* check 301 : ***********', function(test){
    casper.start('http://www.linternaute.com/ville/rennes2/ville-35238', function(response){
        this.test.assertHttpStatus(301);    
    })
    .run(function() {
            this.test.comment('--- Done ---\n');
            test.done();
    });
});

It works with casper+slimer but not with casper+phantom.

curl -i http://www.linternaute.com/ville/rennes2/ville-35238

output : HTTP/1.1 301 Moved Permanently

output casper+slimer : PASS HTTP status code is: 301

output casper+phantom : FAIL #current: 200, #expected: 301 -> issue?

Differences slimer/phantom -> When PhantomJS receives a redirection as HTTP response, it doesn’t call the onResponseReceive with the start status,slimerJS calls it

Was it helpful?

Solution

Ok, for more information see Redirects not followed #442, if you still want to test 301, here a solution :

casper.on('resource.received', function(resource) {
    if(resource.url === 'http://www.linternaute.com/ville/rennes2/ville-35238'){
        this.test.assertEquals(301, resource.status);
    };
});

But I finally chose to test redirection in this manner (easier) :

casper.test.begin('\n********* check 301 : ***********', 1, function(test){
    casper.start('http://www.linternaute.com/ville/rennes2/ville-35238', function() {
        this.test.assertEquals(this.getCurrentUrl(), 'http://www.linternaute.com/ville/rennes/ville-35238');
    })
    .run(function() {
            this.test.comment('--- Done ---\n');
            test.done();
    });
});

I just check the current URL correspond to my redirection... An object with properties 'openUrl' and 'urlRedirected' and it's okay -in a loop-...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top