Last Updated: February 25, 2016
·
14.9K
· david morrow

Scrape Site for JS Errors with Casper.js

I recently needed to make reorganize javascript files on a huge Rails app. I wanted to be sure that I had not broken javascript dependancies with the reorganization, so I needed to crawl the site for js errors each time I restructured the load strategy for js.

I ended up using CasperJs to accomplish this. It was pretty easy.

First install Casper

brew install casperjs

This is what my script ended up looking like

var casper = require('casper').create();
var errors = [];

// log into the site first
casper.start('http://localhost:3000', function() {
  this.fill('form[action="/session"]',
  {
  'login[email]': 'username',
  'login[password]': 'password'
  }, true);
});

// add as many pages as you would like...
casper.thenOpen('http:localhost:3000/accounts/1/products');

casper.on("page.error", function(msg, trace) {
  this.echo("Error:    " + msg, "ERROR");
  this.echo("file:     " + trace[0].file, "WARNING");
  this.echo("line:     " + trace[0].line, "WARNING");
  this.echo("function: " + trace[0]["function"], "WARNING");
  errors.push(msg);
});

casper.run(function() {
  if (errors.length > 0) {
    this.echo(errors.length + ' Javascript errors found', "WARNING");
  } else {
    this.echo(errors.length + ' Javascript errors found', "INFO");
  }
  casper.exit();
});

Then run the script, in my case I called it "check_for_errors.js"

casperjs check_for_errors.js

The script will output any errros found on the pages you hit.

5 Responses
Add your response

s/CaserJS/CasperJS :)

over 1 year ago ·

@n1k0 thanks! have fixed. Love your library, the docs are fantastic!!

over 1 year ago ·

@dperrymorrow thanks!

PS: you've fixed it as "CapserJS" ;)

over 1 year ago ·

Thanks for sharing! Often it's also handy to check if any external resources failed to load:

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
        });
    }
});
over 1 year ago ·

Nice!

@chrisullyott - I don't see why you couldn't

over 1 year ago ·