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?

HP.
  • 19,226
  • 53
  • 154
  • 253

4 Answers4

12

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();
});
thtsigma
  • 4,878
  • 2
  • 28
  • 29
  • Is there a way to get "load.failed" status though? I know it's not in http.status but I tried to figure out how to know when there is no network connectivity. I could do casper.on event but I like to handle the logic within thenOpen() itself. – HP. Jul 29 '13 at 05:18
  • I'm not sure how you would go about that. You could do something like if (response.status >= 400) Would that be along the lines of what you're looking for? – thtsigma Jul 29 '13 at 05:57
  • When there is no network connection, response == undefined in Windows. But I am not sure about other OS. – HP. Jul 29 '13 at 06:24
  • Made a minor edit. I don't have a machine that I can disconnect from being online ATM. Not sure if that's what you are wanting or not. If noone else comes around and has a solution, I'll try testing on another machine sometime tomorrow. – thtsigma Jul 29 '13 at 06:31
  • That's cool. It got the answer. Use this `(response == undefined || response.status == null)` within thenOpen() – HP. Jul 29 '13 at 06:56
  • 1
    I just found out if 404 occurs, response == undefined so response.status doesn't exists. How to get 404? See here http://stackoverflow.com/questions/18074093/get-response-status-404-in-casper-js-within-open – HP. Aug 06 '13 at 07:23
4

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();
    });
});
Ian Hunter
  • 9,466
  • 12
  • 61
  • 77
3

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();
    });
});
swifty
  • 1,127
  • 1
  • 10
  • 23
  • By the way, `casper.open` should probably be `casper.thenOpen` – Artjom B. Jan 13 '15 at 10:37
  • @ArtjomB. Yes is does (to answer your first question). Why should I use .thenOpen() ? – swifty Jan 13 '15 at 14:33
  • Because it is semantically better, although it seems to work here. `open` is a non-asynchronous function. You use it in the top nesting level (disregarding `casper.test.begin`). It is regarded as good form not to mix asynchronous and blocking calls. – Artjom B. Jan 13 '15 at 14:41
0
casper.start('http://google.fr/', function() {
    var res = this.status(false);
    this.echo(res.currentHTTPStatus);
});

casper.run();
abdel
  • 665
  • 1
  • 11
  • 25