用casperjs检查网页中的js错误

来源:互联网 发布:媒体网络 编辑:程序博客网 时间:2024/06/05 20:50

https://coderwall.com/p/uzaaaw

 

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

原创粉丝点击