Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

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

Webrat: Rails Acceptance Testing Evolved – Bryan Helmkamp

Intro

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

Links:

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

API

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/environment.rb
config.gem "webrat", :version => ">= 0.4.4"

#test/test_helper.rb
Webrat.configure do |config|
  config.mode = :rails
end

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")
end

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.

Selenium

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
  #...
end

setup do |session|
  session_host "localhost:3000"
end


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

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
end

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

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

get
post, put, delete head
request

Questions

  • 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.

Comments
Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *