Dashboard > Watir > ... > Quick Start > Watir in 5 Minutes
Watir Log In View a printable version of the current page.
Watir in 5 Minutes
Added by Zeljko, last edited by Alan Baird on Jun 15, 2008  (view change)
Labels: 
(None)

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.

  • open command prompt
  • type
irb
  • you should see
C:\Documents and Settings\Administrator>irb
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

Start Internet Explorer (IE).

  • type
ie = Watir::IE.new
  • 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.

  • type
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/

  • type
ie.url == "http://www.google.com/"
  • you should see
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.

  • type
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.

  • type
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.

  • type
ie.text.include? "The most comprehensive image search on the web"
  • you should see
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.

  • type
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.

  • type
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.

  • type
ie.text_field(:name, "q").value == "Watir"
  • you should see
irb(main):009:0> ie.text_field(:name, "q").value == "Watir"
=> true

Select an item in a selection box.

  • type
ie.select_list(:name, "imagesize").select "Large images"
  • you should see
irb(main):010:0> ie.select_list(:name, "imagesize").select "Large images"
=> ""

Check that there is image from Flickr.

  • type
ie.image(:src, /flickr/).exists?
  • you should see
irb(main):011:0> ie.image(:src, /flickr/).exists?
=> true
Quick Start Guide
Wasn't this just great? Want to learn more? Continue at Watir Tutorial.

Suggest this page be updated to use the new watir-console.

Site running on a free Atlassian Confluence Community License granted to OpenQA. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.6 Build:#812 Aug 06, 2007) - Bug/feature request - Contact Administrators