Watir in 5 Minutes
In this tutorial, in addition to Watir, we will use two more tools, Interactive Ruby Shell
or IRB and Internet Explorer Developer Toolbar
.
Start IRB.
C:\Documents and Settings\Administrator>irb
irb(main):001:0>
Let Ruby know you want to use Watir.
irb(main):001:0> require "watir"
=> true
Start Internet Explorer (IE).
- new IE window should open
- output is something like this
irb(main):002:0> ie = Watir::IE.new
=> #<Watir::IE:0x2d0c6b8 @url_list=["about:blank"], @typingspeed=0.08, @page_container=#<Watir::IE:0x2d0c6b8 ...>, @error_checkers=[], @down_load_time=0.359
, @rexmlDomobject=nil, @ole_object=nil, @ie=#<WIN32OLE:0x2d0c640>, @logger=#<Watir::DefaultLogger:0x2d0c5f8 @datetime_format="%d-%b-%Y %H:%M:%S", @progname=
nil, @logdev=#<Logger::LogDevice:0x2d0c550 @shift_age=nil, @filename=nil, @dev=#<IO:0x284e7d0>, @shift_size=nil>, @level=2>, @speed=:slow, @activeObjectHigh
LightColor="yellow", @defaultSleepTime=0.1>
Go to Google.
ie.goto "http://www.google.com/"
- Google home page should open
- output is something like this
irb(main):003:0> ie.goto "http://www.google.com/"
=> 1.062
Check if url is http://www.google.com/
ie.url == "http://www.google.com/"
irb(main):004:0> ie.url == "http://www.google.com/"
=> true
Click the link that has text Images (in the upper left corner).
We will flash that link first, just to introduce that very useful Watir feature.
ie.link(:text, "Images").flash
- background of Images link should change several times from white to yellow and back
- output is something like this
irb(main):012:0> ie.link(:text, "Images").flash
=> nil

Now that we are sure we have found the correct link, we will click it.
ie.link(:text, "Images").click
- Image search page should open
- output is something like this
irb(main):005:0> ie.link(:text, "Images").click
=> 0.657
Check there is text The most comprehensive image search on the web on the page.
ie.text.include? "The most comprehensive image search on the web"
irb(main):006:0> ie.text.include? "The most comprehensive image search on the web"
=> 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 IE Developer Toolbar for the first time. To start it, click "Tools > Toolbars > Explorer Bar > IE Developer Toolbar" in already opened IE.

You should see it at the bottom of IE window.

The most useful feature of IE Developer Toolbar is Select Element by Click.

Click Select Element by Click and then click search text field.

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.
ie.text_field(:name, "q").set "Watir"
- text should be entered in search text field
- you should see
irb(main):007:0> ie.text_field(:name, "q").set "Watir"
=> true
Click Search Images button.
ie.button(:value, "Search Images").click
- search results page should open
- you should see
irb(main):008:0> ie.button(:value, "Search Images").click
=> 15.454
Check there is text Watir in search text field.
ie.text_field(:name, "q").value == "Watir"
irb(main):009:0> ie.text_field(:name, "q").value == "Watir"
=> true
Select an item in a selection box.
ie.select_list(:name, "imagesize").select "Large images"
irb(main):010:0> ie.select_list(:name, "imagesize").select "Large images"
=> ""
Check that there is image from Flickr.
ie.image(:src, /flickr/).exists?
irb(main):011:0> ie.image(:src, /flickr/).exists?
=> true
Suggest this page be updated to use the new watir-console.