Babysteps in WATIR

That nutty kid Scott Hanselman is always talking about technologies I don't understand.

In the last year or so he's mentioned 'Watir' (pronounced 'Water') a bunch of times, and I've mostly ignored it.

His most recent mention of Watir gave some fairly easy steps for getting started with it. So I did.

This post is shamelessly inspired by his post. I'm going to walk quickly, but thoroughly over the first few steps of using Watir.

So here's the 3 minute guide to watir (includes a cheatsheet).

[yeh, it continues...]

What is it?

Watir is a tool you can use to automate internet explorer. It's name is an acronym for:

'Web Application Testing In Ruby'.

You pronounce it 'Water'. I don't know why. But you do.

Watir can be used to:

  • test your web applications
  • screen scrape a web site
  • impress your friends
  • create a spider, bot or spamatron. (please not a spamatron).
  • automate a repetitive web-based task
  • give you a practical starting point with ruby

Watir is very easy to tinker with.

You first need to:

  1. Install Ruby (using the one click installer for windows (27 Meg, sorry!))

    [Don't have windows? Want to download something smaller? then install ruby from here.]

  2. Install Watir using the one click installer. (It's the first executable file listed on that page. Approx. 800 KB.)

    [Here's a link straight to the file: watir-1.4.1.exe.]

Installed both of those? Ruby, then Watir? Good for you!

And we're coding already!

Now open the windows shell (cmd.exe), and type 'irb' to launch the interactive ruby shell.

(if this fails, then ruby may not have updated your PATH environment variable correctly. It happened to me. it could happen to you.)

C:\>irb <-- start the interactive ruby shell, 'irb'
irb(main):001:0> require 'watir' <-- tell irb you'll be using the 'watir' library
=> true
irb(main):002:0> ie = Watir::IE.new <-- open an IE browser, in a var called 'ie'
=> #<Watir::IE:0x2bad438 @url_list=[], @typingspeed=0.08, @form=nil, @ie=#<WIN32
OLE:0x2bad3d8>, @logger=#<Watir::DefaultLogger:0x2bad378 @progname=nil, @logdev=
#<Logger::LogDevice:0x2bad318 @filename=nil, @dev=#<IO:0x27ce7d0>, @shift_size=n
il, @shift_age=nil>, @level=2, @datetime_format="%d-%b-%Y %H:%M:%S">, @error_che
ckers=[#<Proc:0x029198e8@./watir.rb:1135>], @defaultSleepTime=0.1, @activeObject
HighLightColor="yellow", @enable_spinner=false>
irb(main):003:0> ie.goto("http://google.com") <-- tell 'ie' to browse to google
=> 2.797
irb(main):004:0> ie.text_field(:name, "q").set("Yukihiro Matsumoto")

                ^^^^-- put the text 'Yukihiro Matsumoto' in the textbox named 'q'
=> true
irb(main):005:0> ie.button(:name, "btnG").click <-- click the button called 'btnG'
=> nil
irb(main):006:0>

Alternatively, you can type those commands into a file with a '.rb' extension.

(I've grouped the commands together (below) so you can copy and paste them:

require 'watir'
include Watir
ie = Watir::IE.new
ie.goto("http://google.com")
ie.text_field(:name, "q").set("Yukihiro Matsumoto")
ie.button(:name, "btnG").click

Paste them into notepad and then save the file with a '.rb' extension.

Run it from the command line by typing '[filename].rb' (where, um, '[filename]' is the name you chose for the file, got it?)

You should see internet explorer open up, the text 'Yukihiro Matsumoto' typed into the search box (which will turn yellow), and then the Search button turn yellow as it is clicked -- showing you the results of the search.

Amazing stuff, and much quicker than just running the search yourself.

When you really get into it, you'll want to do a lot more with Watir than I've shown you here. But hopefully now you've got the tools to get you started.

A decent guide to using watir is provided here: watir user guide and of course, if you wish to automatically record a series of ie actions as a watir script -- use the new and improved tool from Scott Hanselman and Rutger Smit, called Watir Recorder++

One odd thing is that a lot of the cheat sheets in the ruby world are published as MS Word documents. So i've written a very short cheatsheet, just for using watir. It tells you how to reference each of the main types of controls.

Here is my cheatsheet for Watir.

 

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.