Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 806
Piotr Wójcik Avatar asked Oct 04 '13 13:10

Piotr Wójcik


3 Answers

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.

like image 131
Beowulfenator Avatar answered Oct 03 '22 19:10

Beowulfenator


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

like image 24
Piotr Wójcik Avatar answered Oct 03 '22 19:10

Piotr Wójcik


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!

like image 25
mashwani Avatar answered Oct 03 '22 18:10

mashwani