What is Watir?
A free, open
source functional testing
tool for web applications.
It is a Ruby library which drives Internet
Explorer the same way people do, clicks
links, fills in forms, and presses
Watir also checks results, such as whether
expected text appears on the page.
Because it’s build on Ruby, you have the
power to connect to databases, read data
files, export XML and structure your code
into reusable libraries.
Why use Watir?
(easy to use and learn)
, a full
scripting language, rather than a
Don't just take our word for it,
read what our
users are saying
What the Users say…
“…WATIR is by far the most complete web testing
framework out here
that doesn't cost an arm
and a leg.
And this forum is the most active
and well supported that I have seen. You can
post a question and is just a short period of
time get someone (most of the time the
architects of the framework!) who will steer
you in the right direct, give you a work around
to get over any roadblocks, or add it to the
framework if it doesn't currently exist. That
is the reason I switched and tell everyone
interest (and those not) that Ruby is the way
to go! Save your money, get WATIR!”
What the Users say… …
“…Ruby is an awesome language and Watir is just
It does things that other companies
, Mercury, Segue, et al)
charge thousands and thousands of dollars a
Anyway, now that I'm gettin' the hang
of Ruby and Watir, I'm starting to automate
everything I do on the web. Even the simple
things like logging in to web sites, searches,
you name it.”
What the Users say… … …
“I was able to write a few unit tests using
Watir in about 15 minutes
and I’ve looked at
Ruby code for all around 48 hours. Granted, I
have been object
oriented programming for the
last four years so it’s not like Ruby looks
Russian to me, but I wasn’t able to figure out
JUnit that quickly by a long shot.”
How do I get started?
Install Ruby: Use the
Install the latest Watir “
Follow this Presentation or Read the
, documentation, examples and tests. Figure
out your test case, start typing your script,
and run it!
Some Helpful Tools…
(A good light
weight editor installed with Ruby
in for Ruby, great if you need a
Tips for finding/accessing HTML elements:
IE Developer Tool bar
(Lets you explore the HTML
elements behind the visible elements on a web page by
clicking on the elements
(Excellent FireFox extension for identifying
HTML elements while writing scripts.)
(Watir API for accessing
How do I…
…Navigate the browser?
…Find elements on the page?
…Interact with elements on the page?
…Check output on the page?
…Create and use Methods?
…Create formal test cases?
Navigating the Browser
# Always Load the Watir library at the top of your script
#Start IE and navigate to a given URL
ie = Watir::IE.start('http://news.google.com')
#or..Attach to an existing IE window by title or url
ie = Watir::IE.attach(
ie = Watir::IE.attach(
,/regex matching url/)
#Navigate to a different URL
Finding <HTML> Elements
Common Functions of the IE object:
And many, many more (div, label, image, etc…)…
How’s and What’s
String value of
How’s tell your method how to find the element you’re looking
for. What’s tell your method the value for “how”.
Interacting with Elements
#Set the text field (or text area) specified name specified value.
#Sets the select with to the specified value
#Click the button with the specified value (label)
#Clicks the link matching 'text'
#Accessing elements in a "frame" or "iframe"
#Get the title of the page
#Get all the text displayed on the page
#Get all the HTML in the body of the page
#Return true if ‘text’ is displayed on the page
Creating and using Methods
#Here is an example method for logging into a web
application. Its two parameters are a user and password
(both of which have defaults). It returns an instance of
login(user=“test_admin”,password = “Password123”)
#Now we can easily login with the default user..
ie = login
#or with a unique user
ie = login(“Fred”,”flinstone”)
Creating Formal Test Cases
#includes Ruby's test case functionality
#Assuming our login method is saved in util.rb
#Test cases are contained within classes which extend Ruby’s base test case
MyTest < Test::Unit::TestCase
#Optional, will be run before each test method.
@ie = login()
#call our login function.
#Test methods must begin with "test_“
#click on some link
#verify that the proper page loaded
assert(@ie.contains_text(“My Some Link Page”))
#Optional, will be run after each test method.
Debugging with IRB
IRB = Interactive Ruby Shell
like interface that allows immediate
running of Ruby script
Great for debugging one line at a time, rather
then having to run through an entire script
Great for testing single lines
Try ruby out using an
. This is a great
way to learn ruby basics!
Watir & Ruby Resources
Watir main site:
Watir user guide:
Note: All information contained in the presentation
was compiled from documentation on the
, or from it’s