opensoul.org

Capybaras eating cucumbers

(or, testing static sites with Cucumber and Capybara)

While working on an app built purely in HTML and Javascript, we needed a good way to write integration tests. We played around with a few different approaches, including “functional” tests using one of the Javascript unit testing libraries. But for now, we settled on using Selenium.

OMG you’re crazy!

No, we’re not. The latest version of Cucumber comes with capybara, which makes it super simple to use Selenium. Capybara just uses any rack app, so we made a simple rack app that serves our static files. So here is what we ended up with in features/support/env.rb:

require 'rubygems'
require 'spec'
require 'cucumber'
require 'rack'
require 'capybara'
require 'capybara/dsl'

Capybara.app = Rack::Builder.new do
  map "/" do
    use Rack::Static, :urls => ["/"], :root => 'public'
    run lambda {|env| [404, {}, '']}
  end
end.to_app

require 'capybara/cucumber'
require 'capybara/session'

Capybara.default_selector = :css
Capybara.default_driver = :selenium

Now just copy over the web_steps.rb that cucumber generates from another project, and proceed as normal.

cucumber, selenium, and testing May 11, 2010

2 Comments

  1. Jamie Hill Jamie Hill May 11, 2010

    Good stuff… I didn’t have much luck getting Selenium to play with Cucumber back along, must give it another shot.

  2. Manly Code Manly Code December 5, 2010

    I’m trying to get into some web development with Node.js and this will be extremely useful. Thanks!

Post a Comment

Comments use textile. Anonymous comments will be deleted.

My name is Brandon Keepers. I like to build things, usually in Ruby or JavaScript. I work at GitHub and live in Holland, MI.

Popular Posts