Michael MacDonald

Javascript Testing with Cucumber, Capybara and env.js

In Rails, Testing on June 12, 2010 at 9:05 am

Tim Riley was excited when he discoved capybara and wrote an excellent blog post about Javascript Testing with Cucumber and Capybara. I was excited too but it took until I was sitting in a hallway at railsconf recharging my laptop before I found the time to clone Tim’s demo app and give capybara a try. I was impressed! I ran the tests and Firefox opened up in the background, ran the test and closed almost in a blink of the eye. Wow, that was fast. So much faster than how I’ve previously tested my javascript with selenium and webrat. And no complicated setup or configuration.

Now, let me introduce you to capybara-envjs, a new gem that Joseph Wilk described in his talk at railsconf. It is a capybara driver for the envjs gem. Instead of using the selenium driver and launching a browser to perform the tests, it uses envjs, a simulated browser environment. That means it will be faster right? A quick experiment showed that it was. Another benefit is with running your javascript tests on a continuous integration server.

I’ve only just begun to play with this new setup but it looks promising. I’ve forked Tim’s original demo app and updated it to use capybara-envjs. To get it working, I added the following to cucumber’s env.rb file (in features/support):

Capybara.javascript_driver = :envjs

And then I replaced the capybara gem in the Gemfile with:

gem 'capybara-envjs', :require => 'capybara/envjs'

Easy! Clone it and give capybara-envjs a try.

  1. […] for alternatives. The culprit in the slowdown here was Selenium. I came across a blog post by RubyFlare the was talking about a much faster alternative to Selenium called envjs. You need to install the […]

  2. In example you are using @javascript in articles.feature but in support/env.rb the javascript driver declared is envjs. I didn’t understand what to do. Actually I am new to capybara and even rails.Please show the right path.

    • Hi Gaurav,

      The @javascript tag is what you need to add to any of your cucumber scenarios/features that use javascript. The envjs is the driver that Capybara uses to carry out these javascript related scenarios. That’s the basic difference.

      I hope that helps.



  3. Quick heads-up in case anyone else is using Ruby 1.9:

    One the Gems (env-js perhaps?) requires Johnson, and Johnson doesn’t compile with Ruby 1.9 right now:

    I’ll try this again in a month or so. 🙂

  4. […] There are some alternatives but they dont work all the time or for every situations. For instance: Capybara with envjs […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: