Webrat 0.1.0 released

Last week I pushed Webrat 0.1.0 out the door. So far the response has been great, and I’ve already received a couple patched (Thanks, David!). Here’s a quick usage example (from the README):

1
2
3
4
5
6
7
8
def test_sign_up
    visits "/"
    clicks_link "Sign up"
    fills_in "Email", :with => "good@example.com"
    select "Free account"
    clicks_button "Register"
    ...
  end

Behind the scenes, this will perform the following work:

  1. Verify that loading the home page is successful
  2. Verify that a “Sign up” link exists on the home page
  3. Verify that loading the URL pointed to by the “Sign up” link leads to a successful page
  4. Verify that there is an “Email” input field on the Sign Up page
  5. Verify that there is an select field on the Sign Up page with an option for “Free account”
  6. Verify that there is a “Register” submit button on the page
  7. Verify that submitting the Sign Up form with the values “good@example.com” 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.

SVN is at http://svn.eastmedia.net/public/plugins/webrat/. Check out the full README.

4 Responses to “Webrat 0.1.0 released”

  1. # Tim Connor Says:

    Awesome, Bryan. I need to add a acceptance/full-stack-integration test, but don’t want the hassle of having to run watir/selenium. I was considering throwing something together with mechanize/hpricot, but now I don’t need to (and this is more full-featured than I’d do for a one-off). :D

  2. # Austin Putman Says:

    Hey, I’m working on a post covering the state of view testing and would love to include something about webrat. Working from your README it is not clear how to integrate this library into my spec suite.

    is it appropriate to make a new webrat session like so: @webrat = ActionController::Integration::Session.new

    and then run examples like this?

    it "can log in" do
      @webrat.visits "/login" 
      @webrat.fills_in "login", :with => "user" 
      @webrat.fills_in "password", :with => "l00ser" 
      @webrat.clicks_button "Log in" 
      @webrat.response.body.should match(/Users/)
    end
  3. # Matt Van Horn Says:

    I just started using this today, and it is pretty neat. It seemed to be missing anyhting for radio buttons, so I patched my install with this (in sessions.rb)

    def selects_radio_button(field, options = {})
      value = options[:choice]
    end
    radios = find_field_by_name_or_label(field)
    return flunk("Could not find radio button #{field.inspect}") if radios.nil?
    radios = [radios].flatten
    if radios.size > 1
      radios = radios.select{|r| r.attributes["type"]  "radio" && (r.attributes["value"]  value || r.attributes["value"].blank?)}
      return flunk("Could not find valid radio button #{field.inspect}") if radios.empty?
    end
    radio = radios.first
    return flunk("#{field.inspect} is not a valid radio button") unless radio.attributes["type"]=="radio" 
    add_form_data(radio, radio.attributes["value"] || "on")

    and I put this in webrat steps:

    When "selects choice '$choice' for $radio" do |choice, radio|
      selects_radio_button radio, :with => choice
    end

    It’s ugly and not tested very well but it’s a start.

  4. # Marcus Ahnve Says:

    I would like to use webrat with merb – any plans on releasing webrat as a gem?