22 Jul 2013 · Semaphore News

    Exploring CasperJS and Continuous Integration

    2 min read
    Contents

    All Ruby developers are probably familiar with Capybara – the tool for interacting with web pages that we all use and love. A similar thing exists in the JavaScript world – CasperJS.

    CasperJS is a navigation scripting and testing utility for PhantomJS.

    Since the setup is really simple, I wondered if CasperJS can be used for testing a Rails application and more so, how hard it is to run those tests on Semaphore.

    Simple example

    Here is a single test that visits the posts page and checks if everything is in order:

    javascript
    // test/javascripts/posts.js
    casper.test.begin('Posts', 2, function suite(test) {
      casper.start("http://0.0.0.0:3000/posts", function() {
        test.assertTextExists("Posts");
        test.assertTextExists("Here you can find all posts");
      });
    
      casper.run(function() {
        test.done();
      });
    });
    

    The full example repository can be found here.

    Semaphore setup

    The setup includes a few steps:

    – preparing the Rails application
    – installing CasperJS (PhantomJS is already available on Semaphore)
    – loading test data
    – running the Rails application server
    – executing CasperJS tests

    This translates nicely into several Semaphore build commands:

    bundle install --deployment --path vendor/bundle
    bundle exec rake db:setup
    bundle exec rake db:test:prepare
    git clone git://github.com/n1k0/casperjs.git ~/casperjs
    cd ~/casperjs
    sudo ln -sf pwd/bin/casperjs /usr/local/bin/casperjs
    cd -
    bundle exec rails server -p 3000 -e test &
    sleep 30 # wait for server to run
    casperjs --direct test test/javascripts/posts.js
    

    Result

    The result looks pretty green:

    It’s worth noting that in this setup CasperJS tests don’t run in isolation: there are no database transactions so state persists across the whole test suite.

    The right tool for the job

    Is CasperJS a good fit for testing Rails applications on Semaphore? Probably not… There are other tools that can serve that purpose. But if you are looking for a tool for smoke testing a live instance of your app or you are more into JavaScript than Ruby, CasperJS is worth looking into.

    If you do want use CasperJS in a Rails project, consider crafting a Rake task that can serve a similar purpose as the build commands from above.

    What cool and unusual tools are you using in your project? Anything interesting in your Semaphore build commands? Let us know!

    Leave a Reply

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