1

你如何检查是否有 casper (+phantom) 的重定向?试试看 :

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

它适用于 casper+slimer,但不适用于 casper+phantom。

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

输出 :HTTP/1.1 301 Moved Permanently

输出 casper+slimer :PASS HTTP status code is: 301

输出 casper+phantom:FAIL #current: 200, #expected: 301-> 问题?

差异更纤细/幻影->When PhantomJS receives a redirection as HTTP response, it doesn’t call the onResponseReceive with the start status,slimerJS calls it

4

1 回答 1

1

好的,有关更多信息,请参阅Redirects not follow #442,如果您仍想测试 301,这里有一个解决方案:

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

但我最终选择以这种方式(更容易)测试重定向:

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

我只是检查当前 URL 是否与我的重定向相对应...具有属性“openUrl”和“urlRedirected”的对象,没关系-在循环中-...

于 2014-05-13T16:35:07.900 回答