简体   繁体   English

如何获取casper.js http.status代码?

[英]How to get casper.js http.status code?

I have simple code below: 我有以下简单的代码:

var casper = require("casper").create({
    }),
    utils = require('utils'),
    http = require('http'),
    fs = require('fs');

casper.start();

casper.thenOpen('http://www.yahoo.com/', function() {
    casper.capture('test.png');
});

casper.on('http.status.404', function(resource) {
  this.echo('wait, this url is 404: ' + resource.url);
});

casper.run(function() {
  casper.exit();
});

Is there a way to catch http.status code regardless of what it is? 有没有办法捕获http.status代码,无论它是什么? Right now I can see in the doc showing the way to catch specific code event. 现在我可以在doc中看到显示捕获特定代码事件的方法。 What if I just want to see what it is? 如果我只想看看它是什么怎么办?

How about this (from the Docs ): 怎么样(来自Docs ):

var casper = require("casper").create({
    }),
    utils = require('utils'),
    http = require('http'),
    fs = require('fs');

casper.start();

casper.thenOpen('http://www.yahoo.com/', function(response) {
    casper.capture('test.png');
    utils.dump(response.status);
    if (response == undefined || response.status >= 400) this.echo("failed");
});

casper.on('http.status.404', function(resource) {
  this.echo('wait, this url is 404: ' + resource.url);
});

casper.run(function() {
  casper.exit();
});

I think that this is a bit easier since 1.0. 我认为这比1.0更容易一些。

This is how i achieved it: 这就是我实现它的方式:

casper.test.begin("load google!", function (test) {
    casper.start();

    casper.open("http://www.google.co.uk");

    casper.then(function () {
        var res = this.status(false);
        test.assert(res.currentHTTPStatus === 200, "homepage returns a 200 status code");
    });

    casper.run(function() {
        this.test.done();
    });
});

The test module has an assertHttpStatus method. 测试模块有一个assertHttpStatus方法。 From the 1.1.0-DEV Documentation 来自1.1.0-DEV文档

casper.test.begin('casperjs.org is up and running', 1, function(test) {
    casper.start('http://casperjs.org/', function() {
        test.assertHttpStatus(200);
    }).run(function() {
        test.done();
    });
});
casper.start('http://google.fr/', function() {
    var res = this.status(false);
    this.echo(res.currentHTTPStatus);
});

casper.run();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM