javascript - CasperJS : assertHttpStatus() : 301 works with slimer, 不带幻像

标签 javascript phantomjs casperjs http-status-codes slimerjs

如何检查 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/

相关文章:

php - 通过单击网页上的按钮运行/执行 CasperJS 脚本

Javascript 检查索引是否等于数字

javascript - 通过ajax将关联PHP数组传递给javascript时保留其顺序

javascript - 如何将 Spring Mvc Controller 5 连接到 jsp 文件?

php - exec() 和 phantomjs 问题与绝对路径

gradle - geb.Browser尝试使用FirefoxDriver代替PhatomJSDriver

javascript - 在共享主机上运行 phantomjs 程序?

xpath - 将XPath与CasperJS QuerySelectorAll一起使用不起作用

javascript - 使用 CasperJS 自动删除 Facebook 群组帖子

javascript - WebApi 2 中的同步 XMLHttpRequest