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



'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..



'nitram' on Wed, 30 Sep 2009 12:59:18 GMT, sez:

Tree in Watir??????



'nitram' on Wed, 30 Sep 2009 13:01:46 GMT, sez:

Like entering a tree with Watir?



'Vinod M K' on Mon, 18 Jan 2010 05:16:36 GMT, sez:

Hi,

We have an application which is totally built on C# and .NET custom controls. We would like to use WatiR tool for evaluating the automation feasibility for this application.

Hence we would like to know if your tool supports these custom controls.

We also would like to know if you can do a Proof of Concept (PoC) for one functional flow for us. (i.e., Login and Logout of the application).

I would really appreciate your thoughts on the same.

Thanks,
Vinod.M.K
vinod@mindlance.com



'dsan' on Mon, 31 May 2010 01:09:54 GMT, sez:

Please tell me how to access a windows remote session with an IP address using watir.
thanks.



'Sateesh' on Sat, 18 Sep 2010 11:09:55 GMT, sez:

For IE we are giving IE.new what is the keyboard if we want to do it in mozilla



'Newbi' on Mon, 20 Sep 2010 16:53:11 GMT, sez:

I have the recorder installed but have no idea how to save the script or where it gets saved when I close my browser - any help you can offer please?



'rohan' on Fri, 24 Aug 2012 04:57:09 GMT, sez:

Hi
How do we access an element inside a form inside an iframe ?

I tried it but even IE DOM cant recognize the element, how will Watir do it? I just know that the element exists because the source code of the frame has its name.

thanks
RS




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. Incivility is not tolerated, such comments are deleted.

 

I'm the co-author of TimeSnapper, 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.

 

NimbleText - FREE text manipulation and data extraction

NimbleText is a Powerful FREE Tool

I wrote this, and use it every day for:

  • extracting data from text
  • manipulating text
  • generating code

It makes you look awesome. You should use NimbleText, you handsome devil!

 

Articles

The Canine Pyramid The Canine Pyramid
Humans: A Tragedy. Humans: A Tragedy.
ACK! ACK!
OfficeQuest... Gamification for the Office Suite OfficeQuest... Gamification for the Office Suite
New product launch: NimbleSET New product launch: NimbleSET
Programming The Robot from Diary of a Wimpy Kid Programming The Robot from Diary of a Wimpy Kid
Happy new year 2014 Happy new year 2014
Downtime as a service Downtime as a service
The Shape of Your Irrationality The Shape of Your Irrationality
This is why I don't go to nice restaurants any more. This is why I don't go to nice restaurants any more.
A flowchart of what programmers do at work all day A flowchart of what programmers do at work all day
The Telepresent Man. The Telepresent Man.
Interview with an Ex-Microsoftie. Interview with an Ex-Microsoftie.
CRUMBS! Commandline navigation tool for Powershell CRUMBS! Commandline navigation tool for Powershell
Little tool for making Amazon affiliate links Little tool for making Amazon affiliate links
Extracting a Trello board as markdown Extracting a Trello board as markdown
hgs: Manage Lots of Mercurial Projects Simultaneously hgs: Manage Lots of Mercurial Projects Simultaneously
You Must Get It! You Must Get It!
AddDays: A Very Simple Date Calculator AddDays: A Very Simple Date Calculator
Google caught in a lie. Google caught in a lie.
NimbleText 2.0: More Than Twice The Price! NimbleText 2.0: More Than Twice The Price!
A Computer Simulation of Creative Work, or 'How To Get Nothing Done' A Computer Simulation of Creative Work, or 'How To Get Nothing Done'
NimbleText 1.9 -- BoomTown! NimbleText 1.9 -- BoomTown!
Line Endings. Line Endings.
**This** is how you pivot **This** is how you pivot
Art of the command-line helper Art of the command-line helper
Go and read a book. Go and read a book.
Slurp up mega-traffic by writing scalable, timeless search-bait Slurp up mega-traffic by writing scalable, timeless search-bait
Do *NOT* try this Hacking Script at home Do *NOT* try this Hacking Script at home
The 'Should I automate it?' Calculator The 'Should I automate it?' Calculator

Archives Complete secretGeek Archives

TimeSnapper -- Automated Screenshot Journal TimeSnapper: automatic screenshot journal

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
Universal Troubleshooting checklist Universal Troubleshooting Checklist
Top 10 SecretGeek articles Top 10 SecretGeek articles
ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Realtime CSS Editor, in a browser RealTime Online CSS Editor
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker



[powered by Google] 

How to be depressed How to be depressed
You are not inadequate.



Recommended Reading


the little schemer


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

Recommended blogs

Jeff Atwood
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
OJ Reeves
Eric Sink

InfoText - amazing search for SharePoint
LogEnvy - event logs made sexy
Computer, Unlocked. A rapid computer customization resource
Aussie Bushwalking
BrisParks :: best parks for kids in brisbane
PhysioTec, Brisbane Specialist Physiotherapy & Pilates
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

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