I had wanted to learn to use Selenium to automate browser-based testing, but a QA person at work gave a brown bag on WATIR. Given the number of browsers I could use it with, I decided to play around with it.
The WATIR site has a page on installation which is pretty straight-forward (I didn’t bother with supporting Safari on my Mac, and the Windows install was pretty straight forward as well). Installing the plugins for FireFox referenced on the install page was also straightforward.
You do need to start Firefox up initially using the -jssh option (for IE on Windows, it comes up automatically). Here is how I did it for Mac:
cd into /applications/Firefox.app/Contents/MacOS
For Windows 7:
cd into Program Files (x86)\Mozilla Firefox
I brought up Ruby’s IRB to start playing around. On my Mac, I could execute commands in the IRB, but in my ruby script file, it was failing on the following:
For the scripts, I needed to add require ‘rubygems’ first. On Windows, I needed to do this for both IRB and ruby scripts:
To bring Firefox, I did the following (you can just comment out the first line to bring up IE):
Watir::Browser.default = “firefox”
b = Watir::Browser.start “http://mysite.com”
The browser popped up to this site. I needed to login, so I specified the name and password by finding the element by id and then specifying the text to be typed in:
In the browser, it was almost as if an invisible person typed in the text. Next, I needed to the “Sign In” button, but there was no id associated with it, so I had to click it after finding it by its value:
b.button(:value, ‘Sign In).click
Now the home page came up. I wanted to create a new Foo, so I needed to get to the Foo page, which is referenced by a link and is called “New Foo” on the page:
b.link(:text, ‘New Foo’).click
I was now on the new page, and starting following the steps to fill out the fields to create the new Foo. However, I was getting the following error:
C:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:56:in `assert_ex ists': Unable to locate element, using :id, "username" (Watir::Exception::Unknow nObjectException) from C:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:288 :in `enabled?' from C:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:60: in `assert_enabled' from C:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/input_elements .rb:327:in `set' from watir_fun.rb:6
But was the entity created successfully? Because none of the displayed fields had a unique id (and this was only a quick experiment), I simply checked with something like the following:
Obviously, for production readiness this is not acceptable, so we would likely had keys or some easier way to access via XPATH and so forth. The point is, WATIR gives you an easy way to automate interacting with a browser and seeing what the results are.
In the end, I found picking up WATIR to be quite straightforward. Back when I was at my current company previously, I used to go through a short manual test script before checking it to make sure my changes didn’t break anything. This weekend (for fun), I hope to code up this former script in WATIR in just a few hours and start having us use it in Development next week.
Additionally, the WATIR web-site is helpful, well-organized, and I hear the help and mailing lists are quite responsive and friendly.