Jeff Kreeftmeijer

Capybara ate Swinger

2011-02-07

About three months ago, I released Swinger to add RSpec driver swapping to Capybara.

This idea wasn’t new, Capybara had doing it for Cucumber for quite some time. Cucumber has this nice tagging thing that allows you to hook in and do cool stuff like that.

RSpec didn’t have a feature like that until 2.0, when it released the metadata feature. That made it possible to pass arbitrary metadata to examples or example groups, allowing you to tag examples. Like in Cucumber. But better.

Using that, I built Swinger for a big project we were working on at 80beans. It was still a bit of a monkey patch, since RSpec didn’t really provide a way to access an example’s metadata in the around hook, but it worked pretty well.

Not long after relasing it as a separate gem, I turned Swinger into a Capybara pull request. It was still a monkey patch, so @dchelimsky did a patch for RSpec to expose the metadata in around hooks, which made it possible for @jncoward to eventually built a cleaner version of the driver swapping idea into Capybara.

Driver Swapping in Capybara, without the crap

Now, since Capybara version 0.4.1 0.4.1.1, it supports driver swapping out of the box. Here’s how to get it working.

First, in the file where you set up Capybara (that’s spec/acceptance/acceptance_helper.rb if you’re using Steak). Include Capybara’s RSpec helper file:

require 'capybara/rspec'

Now, just like with Swinger, you can tag your examples with a specific :driver:

it "does fancy stuff", :driver => :selenium do
  # test fancy stuff
end

Of course you can also use this on example groups:

context "fancy stuff", :driver => :rack_test do
  # fancy examples
end

If you set Capybara.javascript_driver, you can even tag examples that need a javascript capable driver like this:

Capybara.javascript_driver = :selenium

it "does fancy stuff", :js => true do
  # test fancy stuff
end

This officially means I stopped maintaining Swinger and I urge you to update Capybara to 0.4.1.1. Thanks to everyone who helped out building Swinger and getting it into Capybara, it certainly made my test suites a lot faster.