Jeff Kreeftmeijer

Capybara driver swapping on RSpec with Swinger

2010-11-01
Swinger is not maintained anymore, since its functionality is built into Capybara. Check out Capybara ate Swinger for more information.

If you’ve been doing the acceptance testing dance for a while, you probably know it can get slow pretty fast. Not necessarily in every driver, but since you wanted awesome javascript stuff in your application you’ve equipped Capybara with Selenium to actually run your tests in an actual browser. Ouch.

The javascript interaction in your application is minimal, but can’t be tested without Selenium. The worst part is that it’s only ten specs. Wouldn’t it be awesome to only use Selenium for those instead of the whole suite?

If you’re using Cucumber, Capybara’s got your back, making it possible to use Cucumber’s tagging feature to simply specify which driver you want to use per example, effectively allowing your Capybara to have multiple partners in one session.

But since you like to think you’re a rebel and rather test your application in pure Ruby, you’re using plain RSpec (maybe even with Steak). So you need to do stuff like this when you want to switch drivers:

# `scenario` is an alias for `it`, provided by Steak

scenario 'get a nice greeting' do
  Capybara.current_driver = :selenium
  visit homepage
  page.should have_content 'Welcome'
  Capybara.reset_current_driver
end

Swinger

Swinger is a really simple Capybara extension that aims to make this a bit less horrible to look at. Using the new metadata feature in RSpec 2, you can simply set your :driver per scenario or context:

scenario 'get a nice greeting', :driver => :selenium do
  visit homepage
  page.should have_content 'Welcome'
end

context 'when logged in', :driver => :rack_test do

  scenario 'get a nice greeting' do
    visit homepage
    page.should have_content 'Welcome back'
  end

  scenario 'see the logout link' do
    visit homepage
    page.should have_content 'Logout'
  end

end

It also adds the using_driver method to Capybara, allowing you to execute a block using a specific driver. This is especially useful when you’re (still) not on RSpec 2 and can’t use the new metadata feature:

scenario 'get a nice greeting' do
  Capybara.using_driver :selenium do
    visit homepage
    page.should have_content 'Welcome'
  end
end

Installing Swinger is easy. Simply add it to your Gemfile and require it in spec_helper.rb.

A patch for Capybara?

Let me know how you like it and be sure to create an issue on Github, send me a Github message or an email if you have any ideas or run into any issues. Of course you can always fork the project and send me a pull request or a patch via email.

Oh, one last thing: I know this is a pretty minimal gem, but I wanted to just get it out there to use it myself and see if it needs any improvement before maybe turning it into a patch for Capybara.

Would you like this to be part of Capybara or do you prefer keeping this a separate gem? Please let me know!