I’ve put up a couple of posts saying that I’m going to look into watir. Well, here I am, looking into watir. And what do I see? Myself! Get it? Hahahahahaha!!!!!! You see what I did there? Heeeehehehehhehheeeeeheeeheeehee!!!!! Errr hrrrmmmm…. Excuse me.
In the next few articles covering watir, I’m going to be using “SafariWatir”, the watir implementation for the Mac – I only use windows when I’m get paid to. The code in the test cases will be the same, I’ll just be needing to ‘require’ a different library (that’s java/RFT-speak for “import”), that’s all.
Installation
So, I followed the instructions to get it installed (located here: http://safariwatir.rubyforge.org/). Ruby apps generally use a package management-like system to get themselves installed. The package manager is called “Gems”. Since I’ve already got Ruby on Rails installed on my machine, I had ruby gems already installed. So the total amount of work required for installation was running this on the command line:
sudo gem install safariwatir --include-dependencies
Well, there’s an improvement already! RFT takes ages to install (although it is very easy to do), and QTP is a pain to install. There really isn’t too much more to say about that, apart from it’s easy. Very easy. Nice. No licenses to agree to, no license servers, no careful storage of documents and CDs, just a single command line. Being a fan of simplicity and clean implementations, I am impressed.
First Script
Next, I want some sample code. There’s a very simple script on the same page as the install instructions (http://safariwatir.rubyforge.org/). Here it is:
require 'rubygems'
require 'safariwatir'
browser = Watir::Safari.new
browser.goto(“http://google.com”)
browser.text_field(:name, “q”).set(“pragmatic obtiva”)
browser.button(:name, “btnI”).click
puts “FAILURE” unless browser.contains_text(“Agile Retrospectives”)
Even before running the code to see what it does, the fact that the language used is Ruby makes it readily understandable just by looking at it. Side note: Ruby is a cool language – you’ll need to know it to use watir.
So, we have some code. Copy it into a text file and name it something – “thing.rb” in my case. Once saved, execute the file. Again, in my case this was done by running:
ruby thing.rb
Safari (the equivalent of Internet Explorer) opened, and the google homepage came up. It works! Victory! It really is that easy! Wow.
First Script Error
But there is a problem… I got the following back in the console:
ruby thing.rb
/usr/local/lib/ruby/gems/1.8/gems/safariwatir-0.2.3/./safariwatir/scripter.rb:436:in `execute': Unable to locate TextField element with name of q (Watir::Exception::UnknownObjectException)
from /usr/local/lib/ruby/gems/1.8/gems/safariwatir-0.2.3/./safariwatir/scripter.rb:144:in `focus'
from /usr/local/lib/ruby/gems/1.8/gems/safariwatir-0.2.3/./safariwatir.rb:296:in `set'
from thing.rb:6
Interesting. Even with only a cursory knowledge of ruby and watir, the problem is fairly obvious: it can’t find the text field called “q”. After looking at the source for google’s homepage, I can verify that there is a text field called “q”. So what went wrong? Hmmm….. Run it again… ….This time it works!?
And now my feet are back on the ground. The reason it failed is obvious. The line of code that looks for the text field (browser.text_field(:name, "q").set("pragmatic obtiva")) is executed before the text field exists. When I run the script a second time, the text field does exist as the window from the previous run is still there and the page comes up from the cache – much quicker than getting the page off the tubes.
Fixing the script
In RFT, I’d put in txtQ.waitForExistence(); and this script defect would be fixed. Does watir have an equivalent? Hmmm… can’t find one that works. Supposedly, I can with the following line:
browser.wait
But that doesn’t work.
Another thing… Each time I run the test (well, all times but the first), safariwatir doesn’t manage to type out the whole “pragmatic obtiva” string. The search value has been “pragmati”, “pragmatic obti”, “pragmatic ob”, and “pragmatic obtiv”. This is not good. If an automated test is not repeatable, it isn’t worth anything. What is going on?
Temporary setback
It’s late, I’m off to bed. I’ll try again tomorrow. So far, I’m impressed. I think the problems are having are more due to my ignorance of watir/ruby/safariwatir than the software – that’s usually the way.
Hi Nat, I’m the author of SafariWatir. Thanks for posting your experiences as you explore it. It looks like you’re having some timing issues when you’re running your test script, which is a problem with my implementation, not anything that you’re doing wrong. I’ll try to reproduce the problem. What OS X version/hardware are you running?