GoGaRuCo '09 – Webrat: Rails Acceptance Testing Evolved – Bryan Helmkamp

April 19, 2009 Chad Woolley

Webrat: Rails Acceptance Testing Evolved – Bryan Helmkamp


Bryan Helmkamp ( more about him ). He works at WePlay. He is the co-author of “The RSpec Book”


Audience Poll: How many people know and use webrat (50%+ using, 25% using)

Webrat is an awesome tool everyone should use, but it doesn’t run on the iPhone, not written in Scala, and there is no Pr0n.

GoGaRuCo '09 - Webrat: Rails Acceptance Testing Evolved - Bryan Helmkamp

Beer Disclaimer

If you have questions that aren’t answered, find him for a beer after the conference


Writing a rails integration test how we test websites, traditional methods are brittle and tedious.

Webrat code is more readable than traditionally written integration tests

It behaves like a browser:

  • “visit” method – the equivalent typing in a URL to the browser.
  • “click_link” – like clicking a link (duh)
  • “fill_in” – fills in a field
  • “assert_contain” – perform assertions

Uses nokogiri to parse the response HTML and run assertions on it.

How to use it?

config.gem "webrat", :version => ">= 0.4.4"

Webrat.configure do |config|
  config.mode = :rails

Webrat’s Core API

  • visit
  • click_link
  • fill_in
  • check and uncheck
  • choose
  • select
  • attach_file
  • click_button

Webrat works with RSpec, Shoulda and Cucumber
Webrat and cucumber do not have to be used together. You can use Webrat with whatever testing framework you want to use.
Webrat also works with Rails, Sinatra and Merb.

Webrat can do input matching to labels. Webrat encourages you to put good labels on your form fields.

GoGaRuCo '09 - Webrat: Rails Acceptance Testing Evolved - Bryan Helmkamp

Webrat verifies HTTP Status codes for you behind the scenes.

  • Automatically makes sure you don’t get 500 HTTP server errors.
  • Also ensures that fields exist

Verify HTML content

response.should contain("Hello, world!")
response.should have_selector("li", :class => "new", :count => 2)
response.should_not have_xpath(".//meta[@name='robots']")

response.should have_selector("#album li:nth-child(3)") do |li|
  li.should have_selector("img", :src => photo_path(@photo))
  li.should contain("Vacation Photo")

When things go wrong Webrat uses ”’save_and_open_page”’ and writes out the current response body and brings up the page.

Webrat Adapters

  • Rails
  • Merb
  • Sinatra
  • Selenium
  • Mechanize (can use to scrape other websites i.e. google)

Evil plot is to write adapters so he can test everything at once.


Webrat started as an alternative to selenium, but it now includes a selenium adapter. Bryan suggests not using selenium whenever possible. You should start with the traditional webrat mode and not the selenium mode. Unfortunately, it’s the only way to test a real browser, such as javascript.

# selenium doesn't support transactional fixtures
class ActiveSupport::TestCase
  self.use_transactional_fixtures = false

setup do |session|
  session_host "localhost:3000"

Webrat.configure do |config|
  config.mode = :selenium

Webrat talks to the selenium client gem to talk to the Selenium RC server which then drives a browser (IE, safari, and/or firefox).

Sometimes you’ll need to an action differently when a browser is actually present. These commands let you write code for a specific context:
webrat.simulate – traditional webrat simulation mode
webrate.automate – drive a real browser

# is not run in normal webrat mode
  webrat.automate do

# is not run in selenium webrat mode
webrat.simulate do

The ‘selenium’ object is exposed from the selenium client gem. You can use it directly within your tests.

Automating a real web browser is SLOW

One more thing…

rack::test – gives you a quick way to generate requests to any rack enabled application
Makes it easy to test complicated routing of requests through rack apps. For example, a rails metal that routes to a sinatra app.

Rack::Test API

post, put, delete head


  • Q: How do you deal with links with the same names?
  • A: Webrat has a method called “within” which allows you to scope your selectors to be inside an HTML tag.

  • Q: Does it have support for checking HTML validity?

  • A: No, that is outside of the scope of core webrat. However, we have talked about the ability to capture

  • Q: Where does the output of save_and_open_page go?

  • A: Into Rails’ tmp directory

  • Q: Selenium client has some interesting screen grab is that being exposed?

  • A: Webrat has that in Selenium mode now.

About the Author


More Content by Chad Woolley
GoGaRuCo '09 – Lightning Talks
GoGaRuCo '09 – Lightning Talks

Lightning Talks Bosco is introducing the speakers. Come to the ruby meetup! Jeff Smick - Blather Simpl...

GoGaRuCo '09 – CouchDB + Ruby: Perform Like a Pr0n Star – Matt Aimonetti
GoGaRuCo '09 – CouchDB + Ruby: Perform Like a Pr0n Star – Matt Aimonetti

CouchDB + Ruby: Perform Like a Pr0n Star - Matt Aimonetti Intro Matt Aimonetti is part of the Merb team...


Subscribe to our Weekly Newsletter

Thank you!
Error - something went wrong!