Here is how to set up Capybara with RSpec 2 and Ruby on Rails 3. Further down I’ll also show you how to drive Selenium through Capybara. The only assumption is that you have a bare-bones Rails 3 application set up.
First, add Capybara to your Gemfile:
1 2 3 4
bundle install (and if you have not installed RSpec, also run
rails generate rspec:install to set up your spec directory).
spec/spec_helper.rb, add two
require calls for Capybara
near the top:
1 2 3 4 5 6 7
You’re all set! Capybara’s DSL will be available in all RSpec examples in the
spec/integration directory. So, for
spec/integration/home_spec.rb you might write:
1 2 3 4 5 6 7 8
rake spec to run it.
And now with Selenium!
To run any example or example group through Selenium, just set
1 2 3 4 5 6 7 8 9 10
If you run
rake spec now, a Firefox should automatically fire up and
run your test with Selenium.
You could make Capybara run all your tests in Selenium by setting
Capybara.default_driver = :selenium, but I don’t recommend doing this:
You can transparently switch between using Rack::Test (the default driver)
since both are driven through the exact same Capybara DSL, and your tests will
run much faster if you use Selenium only for those tests that actually require
Update: You will also need DatabaseCleaner, since
transactional fixtures do not work with Selenium. Add the
database_cleaner gem to the :test group of your Gemfile and put the
following code into
1 2 3 4 5 6 7 8 9 10 11
See the comments at the bottom for a longer discussion.
If you haven’t done so, I recommend that you read the Capybara README (especially the section titled “The DSL”) to familiarize yourself with Capybara. It might seem a bit daunting at first, but reading it will help you get started so much faster.