CasperJS for Testing with Rails and Rake

It doesn’t matter what testing framework you use, it’s going to have downfalls. It’s not going to be able to test every scenario or it might be cumbersome and slow. The solution is using mu.tiple testing frameworks. This allows the frameworks to test what they test best.

CasperJS is what we decided to use for our functional and cross application testing for a couple of reasons. One is that it is built onto of Phantom JS which I’ve found to be incredibly beneficial and fast for headless browser based testing. Additionally, it allows us to test closer to reality and test with real data were appropriate. Are we getting complaints that our login system is broken? With CasperJS we can test our login system with a real account against our production site. This presents it’s own challenges, specifically related to cleaning up after testing, but it isn’t something we can’t overcome. The only thing lacking was some good documentation for integrating CasperJS into a rails project.

casper is a root level folder that contains everything in our casperJS setup, except for the rake task we use to run the test. Inside of it, we have a folder for docs, a folder for configs, a folder for tests, a utility file,and a grunt file is primarily for loosely linting our code.

As for running our tests, we use rake for everything else, so it makes sense to have a rake task. Two of the goals for the task were: be able to run a single test set or all of our tests from the command and be able to run our test vs. whatever stack made the most sense. This is what we came up with:

namespace :casper do

  desc "Running Login Tests"
  task :login do  
    config = "#{envConfig}"
    puts "Running Login tests using #{config}"
    exec(  "casperjs test --includes=#{Rails.root.join("casper/configs/#{config}")},#{Rails.root.join("casper/util.js")}  --xunit=#{Rails.root.join("casper/test.xml")}   #{Rails.root.join("casper/tests/login.js")}  "      )

  desc "Run All Tests"
  task :all => [:login]
  def envConfig()
    #Set our list of environments
    environments = ['development','staging','production']
    if ( environments.include?( ENV['env'] ) ) then
      return ENV['env'] + '.js'
    elsif ( File.exists?("#{Rails.root.join("casper/configs/local.js")}" ) ) then
      return 'local.js'
      return 'staging.js'


This rake task allows us to have four different configurations. Three of them are standard and are checked in with the rest of the code in our config file. The fourth is an optional local config for individual use.

A string of tests passing. One of the greatest things a developer can see

One additional thing we decided to do was to have each set of tests be runnable by themselves from rake and also runnable with `rake casper:all`. Right now we only have our login being tested, but in the future we intend to add more tests.

Now that we can run our tests, the next step was to wire it in to run often. We created a jenkins task for our tests and installed the xunit plugin to parse the output of our tests. Right now, our tests are quick. Our casper tests only take about 1 minute to run vs over

Thus far, our casperJS tests haven’t caught any bugs. It’s only a matter of time though before it does. Until then, the sense of security in knowing that everything is working as expected makes all this work worthwhile.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>