Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Watir On Rails

Watir On Rails

If you’re running Windows, you’ll need to gem install watir

If you’re fortunate enough to be on OS X, you can gem install safariwatir though be aware that Safari support is still in development and needs more work.

Installation

You have probably already done this, but just in case… script/plugin install svn://rubyforge.org/var/svn/watir-on-rails

Generating Tests

You can use the generator to create skeleton Watir tests in test/watir script/generate watir Login

Running Tests

Watir tests can be executed by running rake test:watir You’ll need to have a test server running.

Decorating Watir with a DSL

Similar to Rails Integration Tests, WatirOnRails provides developers with the means to add singleton methods to the browser instance, allowing for the development of a site-specific language. The WatirOnRails open_browser method is mixed into the TestCases generated by Watir. This method will return an instance of a Watir browser. Similar to Rails IntegrationTest’s open_session, it takes an optional block, yielding the browser instance. A simple WatirOnRails test method might look like this…

def test_login
  browser = open_browser
  browser.goto("/login")
  browser.text_field(:name, "user[name]").set("dave")
  browser.password(:name, "user[password]").set("test")
  browser.button(:index, 1).click
  assert_match /Welcome Dave/, browser.div(:id, "banner").text
end

...while a slightly more advanced WatirOnRails test method using a site- specific language might look like this…

def test_login    
  browser = open_browser
  browser.login_with :username => "dave", :password => "test" 
  assert_match /Welcome Dave/, browser.div(:id, "banner").text
end
def open_browser
  super do |browser|
    def browser.login_with(args)
      goto("/login")
      text_field(:name, "user[name]").set(args[:username])
      password(:name, "user[password]").set(args[:password])
      button(:name, "login").click
    end      
  end
end

SEND FEEDBACK!

Dave Hoover dave.hoover@gmail.com http://redsquirrel.com/dave/

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly