uzaaaw
Last Updated: February 25, 2016
·
13.65K
· dperrymorrow
37b092a324da25d53fd63d780692b174

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.

Say Thanks
Respond

5 Responses
Add your response

4659
C0240424425f5ca75071b478c0dc29b9

s/CaserJS/CasperJS :)

over 1 year ago ·
4678
37b092a324da25d53fd63d780692b174

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

over 1 year ago ·
4687
C0240424425f5ca75071b478c0dc29b9

@dperrymorrow thanks!

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

over 1 year ago ·
9015
D42a7264714dee5006b9c99d2567a320

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 ·
9838
Icflorescu

Nice!

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

over 1 year ago ·