Integrating Capybara with RSpec

Let's turn our irb session into an RSpec spec to finish our initial tour of Capybara. We'll put the contents into a file called smithsonian_spec.rb that looks like this:

require 'capybara/rspec' Capybara.current_driver = :selenium # switch to chrome based on environment variable. e.g.: # $ BROWSER=chrome rspec smithsonian_spec.rb if ENV['BROWSER'] == 'chrome' Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new(app, browser: :chrome) end # use poltergeist headless driver/browser based on environment variable, e.g. # $ BROWSER=poltergeist rspec smithsonian_spec.rb elseif ENV['BROWSER'] == 'poltergeist' puts "Using poltergeist headless driver..." require 'capybara/poltergeist' Capybara.register_driver ...

Get RSpec Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.