10

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? Right now I can see in the doc showing the way to catch specific code event. What if I just want to see what it is?

4

4 回答 4

12

这个怎么样(来自文档):

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();
});
于 2013-07-29T04:28:15.240 回答
4

测试模块有一个assertHttpStatus方法。来自 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();
    });
});
于 2015-03-11T19:33:42.243 回答
3

我认为自 1.0 以来这要容易一些。

这就是我实现它的方式:

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();
    });
});
于 2015-01-13T10:32:50.557 回答
0
casper.start('http://google.fr/', function() {
    var res = this.status(false);
    this.echo(res.currentHTTPStatus);
});

casper.run();
于 2016-05-09T16:53:08.627 回答