| For up to date version of this page, download free version of Watir book, or go directly to updated page. |
Watir in 5 Minutes
In this tutorial, in addition to Watir, we will use two more tools, Interactive Ruby Shell (IRB) (IRB) and Firebug: A Firefox Web Development Tool. IRB is already installed with Ruby, but Firebug isn't, so please install it now.
This tutorial uses FireWatir so that it works on Windows, Linux and Mac OS platforms.
Start IRB.
- open command prompt
- type
irb
- you should see
irb(main):001:0>
Let Ruby know you want to use Watir.
- type
require "watir"
- you should see
irb(main):001:0> require "watir" => true
- If you get an error, type
require "rubygems"
Start Firefox
- type
Watir::Browser.default = 'firefox' b = Watir::Browser.new
- a new Firefox window should open
- output is something like this
irb(main):002:0> b = Watir::Browser.new => #<FireWatir::Firefox:0x33ed73c url="" title="Mozilla Firefox Start Page">
Go to Google.
- type
b.goto "http://www.google.com/"
- Google home page should open
- output is something like this
irb(main):003:0> b.goto("http://www.google.com/")
=> #<FireWatir::Firefox:0x33ed73c url="" title="Google">
Click the link that has text Images (in the upper left corner).
We will check that the link exists first:
- type
b.link(:text, "Images").exist?
- output is:
irb(main):004:0> b.link(:text, "Images").exist? => true
Now we will click it.
- type
b.link(:text, "Images").click
- Image search page should open
- output is something like this
irb(main):005:0> b.link(:text, "Images").click => 0
Check there is text Business Solutions on the page.
- type
b.text.include? "Business Solutions"
- you should see
irb(main):027:0> b.text.include? "Business Solutions" => true
Enter text Watir in search text field.
To do this, we have to find out some attribute of text field, like id or name, so Watir can find it on the page. We will use Firebug for the first time. To start it, click the little bug icon in the lower right hand corner of Firefox.
![]()
You should see this at the bottom of the Firefox window:

The most useful feature of Firebug for us is the 'Click an element in the page to inspect':

Click this button and then click the 'search' text field on the page:

Now we see that search text field has name q, and that is how we will tell Watir to find it. Finally, enter text Watir in search text field.
- type
b.text_field(:name, "q").set "Watir"
- text should be entered in search text field
- you should see
irb(main):007:0> b.text_field(:name, "q").set "Watir" => 0
Click Search Images button.
- type
b.button(:value, "Search Images").click
- search results page should open
- you should see
irb(main):029:0> b.button(:value, "Search Images").click => 0
Check there is text Watir in search text field:
- type
b.text_field(:name, "q").value == "Watir"
- you should see
irb(main):009:0> b.text_field(:name, "q").value == "Watir" => true
Check that there is image from this wiki:
- type
b.image(:src, /openqa/).exists?
- you should see
irb(main):011:0> b.image(:src, /openqa/).exists? => true