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:
- 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.]
- 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
'Ramana' on Fri, 23 May 2008 03:49:03 GMT, sez: Hi Nice to see all the information but how can i get current systems environment information like OS and User name
'Leo' on Mon, 26 May 2008 04:20:08 GMT, sez: Hi,
does anybody know how to use Watir::Container methods like li and lis in watir program?
Thanks,
Leo
'Karthic' on Thu, 12 Jun 2008 11:05:53 GMT, sez: Hi,
Anybody knows how to check the error msg that r showing correct or not?
'Jim' on Sat, 21 Jun 2008 17:05:47 GMT, sez: Don't forget about iMacros for Firefox. It is the free version of a commercial tool and VERY easy to use. I like it a lot. Good support as well.
https://addons.mozilla.org/en-US/firefox/addon/3863
'Venkat' on Wed, 27 Aug 2008 08:03:27 GMT, sez: IE does not open when i run the script
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
'Sarfaraz' on Tue, 07 Oct 2008 04:27:46 GMT, sez: This article is realy very helpful for the beginers of Watir
'MM' on Tue, 14 Oct 2008 10:47:22 GMT, sez: Great script, but How could this be sdapted the form data is submitted using an html Link?
'daca' on Thu, 08 Jan 2009 12:45:42 GMT, sez: Need some help,
when I do this...it flashes what i need to click:
$ie.frame($mf).div(:id, "ext-gen13").li(:index, 2).flash
however if I try to click the same thing
irb(main):079:0> $ie.frame($mf).div(:id, "ext-gen13").li(:index, 2).click
=> 0.312
and nothing happens in the browser? Anybody has any idea how i can get to click this li object?
'vin' on Sun, 12 Apr 2009 10:50:33 GMT, sez: I cant seem to run the script. When I type in the commands in the irb it works fine, but when i type in the file name it says command not found.. any idea why?
'Bond' on Tue, 09 Jun 2009 04:06:46 GMT, sez: If i click on a link after that once it opens the particular webpage i want to access thw URL of that webpage and store it in a variable. Can any body please help me on this
'gayan' on Fri, 19 Jun 2009 04:39:49 GMT, sez: Unable to install watir (Windows)
When I type gem update --system , message display as "Oh no! Unable to find release note"
'"update --system ' is not recognized as internal or external command, operable program or batch file.
So unable to continue of the process.
Can any body please help me on this..
|