Using Capybara with RSpec Outside Cucumber

If you want to try using Capybara for browser automation on it’s own, here’s a simple script to get you started:

require 'rubygems'

require 'capybara'
require 'capybara/dsl'

Capybara.default_driver = :selenium
Capybara.app_host = "http://www.google.com"

require "rspec/expectations"
class Google
  include Capybara
  include RSpec::Matchers

  def search_for(text)
    visit "/"
    fill_in "q", :with => text
    click_button "Search"
  end

  def ensure_results_contain(expected_text)
    page.should have_content(expected_text)
  end
end

google = Google.new
google.search_for("Matt Wynne")
google.ensure_results_contain("Tea")

To make this work you’ll need to install the capybara and rspec Ruby gems:

gem install capybara rspec

Published by Matt

I write software, and love learning how to do it even better.

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *