Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 562
HP. Avatar asked Jul 29 '13 00:07

HP.


3 Answers

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();
});
like image 200
thtsigma Avatar answered Oct 23 '22 15:10

thtsigma


The test module has an assertHttpStatus method. From the 1.1.0-DEV Documentation

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();
    });
});
like image 20
Ian Hunter Avatar answered Oct 23 '22 16:10

Ian Hunter


I think that this is a bit easier since 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();
    });
});
like image 3
swifty Avatar answered Oct 23 '22 15:10

swifty