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?
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();
});
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();
});
});
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();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With