Babysteps in WATIR
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

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.





'Simon' on Tue, 01 Aug 2006 06:43:43 GMT, sez:

Another really useful quick guide that saves me from having to look into something myself. Fantastic. Keep it up!



'al' on Tue, 01 Aug 2006 08:23:29 GMT, sez:

Forget ajax - Command shells, gotos and hex are all the rage in web development this year



'Des Traynor' on Tue, 01 Aug 2006 09:12:34 GMT, sez:

Excellent guide Leon,
Cheers.



'Sunil' on Tue, 01 Aug 2006 09:47:38 GMT, sez:

I have also created a sample test file



'Optimus' on Tue, 01 Aug 2006 21:36:26 GMT, sez:

I tried using some water on my browser to get rid of the stains. I thought I'd start with the IE logo, very stubborn one that one. But alas now my screen doesn't work anymore...

Honestly... who user IE anymore anyway? ;)



'lb' on Tue, 01 Aug 2006 21:43:29 GMT, sez:

sounds like you need 'firewatir,' dom, i mean optimus.

it's not as polished as watir, but works on firefox.



'Wesley Shephard' on Wed, 02 Aug 2006 16:06:57 GMT, sez:

I had a problem with installing Watir with the one click ("could not write file /watir/AutoItX.chm") so an alternate method is to go to the Ruby Gems Package Manager interface (installed by the main Ruby installer) and type "install watir". Wait for the quick download and it is done.



'Abijith' on Tue, 02 Jan 2007 05:16:16 GMT, sez:

i need to know if it is possible to add a wait statement while editing a WATIR script that was recorded in watir ++ recorder



'lb' on Tue, 02 Jan 2007 07:51:07 GMT, sez:

>a wait statement

as in, something to make the script sleep for a while?
you'd want to research how this is done in Ruby. Watir is just Ruby.

cheers
lb



'MyDNN' on Thu, 03 May 2007 11:11:57 GMT, sez:

I had the same problem installing that Wesley did. I had clicked on the link to open the Watir installer and clicked 'Run.' When I saved the install to my desktop, it was able to install all the files correctly.

Tim



'Venu9955' on Mon, 31 Mar 2008 23:31:43 GMT, sez:

Hi,
Nice to see all the information. Here i want to know that is Watir useful to test BACK button?... I have seen some where that it doesn't support like.. could anyone clear this?
If my page is having 2 BACK buttons, one is at Menu bar and another one is in page text...then whichone comes in to this priority?
Thanks in Advance,
Venu9955



'lb' on Mon, 31 Mar 2008 23:37:30 GMT, sez:

@Venu9955
If there's a button in your page that says "Back" then you can use watir to find and click this button.

let's say it is called 'btnBACK' -- then this script would click it:

require 'watir'
include Watir
ie = Watir::IE.new
ie.goto("http://YOURPAGEHERE.com")
ie.button(:name, "btnBACK").click



'Venu 9955' on Tue, 01 Apr 2008 02:52:49 GMT, sez:

Nice to see the solution to my query.
Thanks for ur reply...:)
Venu



'hi lb' on Tue, 01 Apr 2008 03:01:07 GMT, sez:

Hi friend,

Somewhere i have seen the information that Watir can not work for Back to page button like... thats what i put the query..

Pls clarify this...

Thanks,
Venu9955



'lb' on Tue, 01 Apr 2008 03:13:00 GMT, sez:

@'Venu 9955'

Watir does indeed allow you to click the browser's back button. There is a command called "back()" that does exactly this.

From the documentation:
(see http://wtr.rubyforge.org/rdoc/classes/Watir/IE.html#M000255 )

back() :
Go to the previous page - the same as clicking the browsers back button an WIN32OLERuntimeError exception is raised if the browser cant go back


===

The place you may have heard that doesn't do 'back' is that WatirMaker -- a tool for automatically recording Watir scripts, released by Scott Hanselman -- didn't originally include support for "back."

See http://www.hanselman.com/blog/IntroducingWatirMakerRecordingForRubybasedWatir.aspx

The tool may now support this.

cheers
lb




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Who reads this legal bit anyhow?

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

TimeSnapper won last year's Developer Competition at Larkware.com, and is used by over 10,000 people.

Articles

Coding Koan: the power of one Coding Koan: the power of one
Behavior Driven Development: As Human As Possible Behavior Driven Development: As Human As Possible
What To (Really) Do If You Find Out Your Parents Are Using Vista (redux) What To (Really) Do If You Find Out Your Parents Are Using Vista (redux)
What To Do If You Find Out Your Parents Are Using Vista What To Do If You Find Out Your Parents Are Using Vista
Sample Code From Text-Adventure Game Platforms Sample Code From Text-Adventure Game Platforms
TimeSnapper 3.0 -- an interactive, bubbling cauldron of possibilities TimeSnapper 3.0 -- an interactive, bubbling cauldron of possibilities
The laptop compubody sock The laptop compubody sock
Everything that's bad for you is suddenly good for you! Everything that's bad for you is suddenly good for you!
Everything I know about Code Reviews I learnt from Star Wars (and JCooney) Everything I know about Code Reviews I learnt from Star Wars (and JCooney)
Syntax highlighting of strings Syntax highlighting of strings
Google AppEngine: evil virus or viral evil? Google AppEngine: evil virus or viral evil?
Workflow software: I'm calling the bluff. Workflow software: I'm calling the bluff.

Archives .: secretGeek :: Complete Archives :.
25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Top 10 SecretGeek articles Top 10 SecretGeek articles

Downloads

TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 2.5: with password protection

ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Next Action NextAction
Managing the top of your mind



[powered by Google] 

Thai Erawan, Brisbane Restaurant, delicious thai food in paddington Thai Erawan, Brisbane Restaurant
World's Simplest Code Generator (html edition) World's Simplest Code Generator
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker
How to be depressed How to be depressed
You are not inadequate.



Recommended Reading

The Best Software Writing I
The Business Of Software (Eric Sink)


Recommended blogs

Jeff Atwood
Reginald Braithwaite
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Joel Pobar
Eric Sink
Joel Spolsky
Des Traynor

Aggregated Links

programming.reddit.com
dzone
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
n links today
new and notable
morning coffee
learning .net
weekly link post
(my del.icio.us account)

LinkedIn profile
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy