Well I finally jumped on board and gave Ruby and Watir a try and boy was I impressed. I’m somewhat ashamed to say that I’ve yet to dive into the TDD/Unit test world (I’m currently working on one of those projects that was due a month ago and can’t afford to let it slip any farther, but don’t worry, unit test are planned for version 1.1) but with Watir I can at least now quickly setup some basic UI testing.

Scott Hanselman created a cool C# program called WatirMaker that recorded your actions in IE and generated a Watir script. It was later converted to native Ruby by Michael Kelly and John Hann. Needless to say this makes writing unit test for your Web UI much faster. I decided to put my ruby skills to the test (ok so this stuff was easy but hey, it was my first attempt at ruby) and make a few improvements to the script.

Here is the new script and here is a list of changes I made:

  • Renamed “ie.” to “browser.” in the output to be more FireWatir friendly
  • Added ability to control the output of Debug and Warning Messages
    • wm.setDebugLevel(1) makes it only output warning messages, level 2 includes debug messages
  • fixed "Unknown property or method `document'" errors
    • the original script had a capital “U” by my system had a lower case “u” in the error message so my output scripts had tons of “unknown property or method `document'” printed in the output. I just removed the “u” completely from the error matching to it will now work if the error message regardless of case
  • fixed outputting "browser.goto('javascript:'';')"
    • not sure if this was an issue only on my system or not but I was sometimes getting "browser.goto('javascript:'';')" in the output so I just check for that string before printing the output
  • added support for link identification by text instead of index
    • in the original version when you clicked on a link it would output something like “ie.document.all[ '56' ].click” now it reads the innerText of the <a> tag and outputs “browser.link( :text, 'The Link Text' ).click”
  • added support for regular expression generation of asp.net like id values
    • if WatirMaker is going to use “:id” as the “how” and the “what” contains a “_” then it uses a regular expression match of everything after the last “_”. This makes generating scripts for asp.net pages easier because then if the name of a container changes (like a master page) your script will still work. In cases where there are multiple controls with the same id but different containers you will have to hand tweek the output to get the results you expect.
    • Original output: ie.text_field( :id, 'ctl00_cphBody_txtUsername' ).set( 'username' )
      New Output: browser.text_field( :id, /txtPassword$/ ).set( 'password' )

I tested the new script on Windows XP MCE (which is really the same as XP Pro) using IE 7 beta 3.

If you have any problems or suggested improvements feel free to leave a comment or send me an email. My next improvement will be to have WatirMaker output ruby unit test.

UPDATE: WatirMaker is now hosted on openqa.org.  Here is a direct link:
http://svn.openqa.org/fisheye/browse/watir-recorder/trunk/Ruby/src

kick it on DotNetKicks.com

Posted at 7/23/2006 11:21 PM
Comments [2] - Permalink