Skip to content
ryanjm edited this page Jan 4, 2012 · 14 revisions

Webrat lets you quickly write robust and thorough acceptance tests for a web application. By leveraging the DOM, it can run tests similarly to an in-browser testing solution without the associated performance hit (and browser dependency). The result is tests that are less fragile and more effective at verifying that the app will respond properly to users.

Webrat does not natively handle Javascript. If your application uses Javascript, you might want to make use of Webrat’s Selenium support (see Webrat::Selenium in the RDoc).
Alternatively, you may want to investigate standalone Selenium or watir.

Synopsis


def test_sign_up
  visit "/"
  click_link "Sign up"
  fill_in "Email", :with => "[email protected]"
  select "Free account"
  click_button "Register"
  ...
end

Behind the scenes, this will perform the following work:

  • Verify that loading the home page is successful
  • Verify that a “Sign up” link exists on the home page
  • Verify that loading the URL pointed to by the “Sign up” link leads to a successful page
  • Verify that there is an “Email” input field on the Sign Up page
  • Verify that there is an select field on the Sign Up page with an option for “Free account”
  • Verify that there is a “Register” submit button on the page
  • Verify that submitting the Sign Up form with the values “[email protected]” and “Free account” leads to a successful page

Take special note of the things not specified in that test, that might cause tests to break unnecessarily as your application evolves:

  • The input field IDs or names (e.g. “user_email” or “user[email]”), which could change if you rename a model
  • The ID of the form element (Webrat can do a good job of guessing, even if there are multiple forms on the page.)
  • The URLs of links followed
  • The URL the form submission should be sent to, which could change if you adjust your routes or controllers
  • The HTTP method for the login request

A test written with Webrat can handle these changes smoothly.

Clone this wiki locally