Piotr Wu
Piotr Wu

Reputation: 1362

Casperjs catch console.log and console.error

I'am trying to catch site console.log and console.error by casperjs. In the case of console.log I have working code:

casper.on('remote.message', function(message) {
    this.echo('remote message caught: ' + message);
});

But I can't figure out how to catch console.error. I need this for catching any resources error (like images not found).

Upvotes: 9

Views: 8812

Answers (3)

Beowulfenator
Beowulfenator

Reputation: 2300

There's also the page.error handler:

casper.on("page.error", function(msg, trace) {
     this.echo("Error: " + msg, "ERROR");
});

Depending on which errors you need to catch, this one may be better.

Upvotes: 10

mashwani
mashwani

Reputation: 65

You can use the following event to get remote errors:

casper.on("resource.error", function(resourceError) {
    this.echo("Resource error: " + "Error code: "+resourceError.errorCode+" ErrorString: "+resourceError.errorString+" url: "+resourceError.url+" id: "+resourceError.id, "ERROR");
});

Works like charm!

Upvotes: 3

Piotr Wu
Piotr Wu

Reputation: 1362

Ok it's weird to answer my own question but I found a solution on a coderwall blog posted by dpashkevich:

casper.on('resource.received', function(resource) {
    var status = resource.status;
    if(status >= 400) {
        casper.log('Resource ' + resource.url + ' failed to load (' + status + ')', 'error');

        resourceErrors.push({
            url: resource.url,
            status: resource.status
        });
    }
});

Works brilliant

Upvotes: 3

Related Questions