Unboxing is our regular feature where someone at Box UK spends a few hours playing with a new framework, library, tool or technique and blogs about his or her experience.
Although the Watir-Webdriver browser automation tool has been available for some time, it’s still very much in active development. We have been using it at Box UK for our automated tests, where it’s quickly become an invaluable tool so I thought I’d write a brief introduction to it, to highlight some of the things that make it so useful.
What is it?
According to the Watir-Webdriver github page, it is a ‘Watir implementation on Webdriver’. This is a fair description but it obviously assumes you know what both Watir and Webdriver are. So what is it really?
Watir (pronounced ‘water’) stands for ‘Web Application Testing in Ruby’. Watir started out life as an IE automation tool but has since grown into a family of related tools for different browsers such as FireWatir (for Firefox), SafariWatir and OperaWatir. The tools in the Watir ‘family’ use a common API for accessing page elements which are scripted using Ruby.
Webdriver is a framework for automated testing of web applications. Webdriver has adopted the approach of supporting the most suitable control to automate each browser from one common interface. The Webdriver project has merged with Selenium and is a key component of the current Selenium 2.0 development.
So in essence, Watir-Webdriver provides the Watir API for accessing page elements as an interface to Webdriver. This means there is now one common Watir tool for all Webdriver supported browsers – IE, Firefox, Chrome and the headless HTML Unit browser. But that’s enough description for now – let’s take a look at what the tool can do.
Getting to grips
OK, I’m going to have to ‘fess up here. We’ve been using Watir-Webdriver for a while so it’s not exactly new to me. That said, I have installed it on a number of environments so I know that getting started with the tool is relatively straightforward. Assuming Ruby is already installed it should be a case of running the following on the command line:
gem install watir-webdriver
The gem should handle installing its dependencies (such as the selenium-webdriver gem), so once the installation is completed the user should be good to go for Firefox and IE.
For the Chrome and HTML Unit headless browsers, the installation and running is a little trickier. For Chrome support you’ll need to download the Chromedriver server from http://code.google.com/p/selenium/downloads/list and install it by adding it to your path.
For the HTML Unit headless browser, the Selenium Standalone jar is needed from http://code.google.com/p/selenium/downloads/detail?name=selenium-server-standalone-2.0b2.jar. The server will need to be running before the HTML Unit driver can be used. It is possible to call this from within the script itself though.
The equivalent of the ‘Hello World’ app in the browser automation world is a Google search script, so I’ll start with exactly that:
browser = Watir::Browser.new :firefox
browser.text_field(:name, "q").value="Unboxing Box UK"
browser.button (: type, "submit").click
To run the script in IE or Chrome it is just a case of changing the: Firefox to: IE or: Chrome and the script will run just the same.
To get the same script to run in HTML Unit, I find that the best option is to launch the Selenium Standalone server from within your script, like so:
server = Selenium::Server.new("path/to/selenium-server-standalone.jar" ,
:background => true)
browser = Watir::Browser.new(:remote, :url => "http://127.0.0.1:4444/wd/hub",
:desired_capabilities => :htmlunit)
browser.text_field(:name, "q").send_keys "Unboxing Box UK"
browser = Watir::Browser.new(:firefox)
browser.frame(:id, "content_ifr").send_keys [:control, "a"], :backspace
browser.frame(:id, "content_ifr").send_keys("Hello World")
The concern with using software that is still in development such as Watir-Webdriver is whether or not it is really ready for use on a real project. We’ve been using it extensively since January at Box UK and our experience has been that it is stable enough for day to day use, particularly for use with Firefox.
There are some features that were available in Watir that aren’t yet available in Watir-Webdriver. Notably for me, the ability to attach to an open browser window is missing. That said, adding Watir-Webdriver to our test automation toolkit hasn’t prevented us from using Watir when we’ve needed to. Personally, I’m now using Watir-Webdriver as my browser automation tool of choice.
Want to see more of Watir-Webdriver?
If you’re already a Watir user and considering trying Watir-Webdriver, it’s worth checking the comparison with the 1.x API
For all things related to Watir, I recommend Alistair Scott’s WatirMelon blog at http://watirmelon.com and these posts in particular which discuss Watir-Webdriver: