RubyonRails · Testing · watir

Watir Cheat sheet

Watir Cheat Sheet


Getting Started with Watir

require ‘watir’

Load the Watir library

ie = Watir::IE.start


Start IE using the local timeclock server.


Close IE.

ie = Watir::IE.attach(:title, ‘title‘)

Attach to an existing IE window, so you can drive it with Watir.


Manipulating Controls with Watir

ie.text_field(:name, ‘name‘).set(‘value‘)

Set the text field specified name specified value.

ie.button(:value, ‘value‘).click

Click the button with the specified value (label)

ie.button(:name, ‘name‘).click

Click the button with the specified name.

ie.checkbox(:name, ‘name‘).set

Check the check box named “name”. (Uncheck: clear)

ie.object(:attribute, ‘name‘).flash

Cause the specified control to flash.


A Special Testing Function For Timeclock


require ‘toolkit/testhook’

ensure_no_user_data ‘name

Delete any database records for the named user.


Accessing Page Contents with Watir


Return true if the current page has the specified text somewhere on the page. Otherwise, false.


Return the title of the current page.


Return all the HTML in the body of the page

ie.table(:id, ‘recent_records).to_a

Return an array containing the text in the table’s rows and columns.

ie.table(:id, ‘recent_records’)[2][1].text

Return the text from the first column of the second row of the table id’d ‘recent_records.



assert_equal(expected, test_method)

Test whether the expected value matches the actual value returned by the test method

assert_match(regexp, test_method)

Test whether the regular expression matches the actual value returned by the test method.


Test whether the expression is true.