如何检查 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 <a href="http://www.linternaute.com/ville/rennes2/ville-35238" rel="noreferrer noopener nofollow">http://www.linternaute.com/ville/rennes2/ville-35238</a>
输出:HTTP/1.1 301 Moved Permanently
输出casper+slimer:PASS HTTP status code is: 301
输出casper+phantom:FAIL #current: 200, #expected: 301
-> 问题?
Differences slimer/phantom -> 当 PhantomJS 收到 HTTP 响应重定向时,它不会调用带有启动状态的 onResponseReceive,slimerJS 会调用它
最佳答案
好的,有关更多信息,请参阅 Redirects not followed #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”的对象,没问题 - 在循环中 -...
关于javascript - CasperJS : assertHttpStatus() : 301 works with slimer, 不带幻像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23513587/