HP. HP. - 6 months ago 98
Javascript Question

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? 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?

Answer

How about this (from the 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();
});