Unboxing: Watir-Webdriver

Overview
In this latest Unboxing Sean Robbins explores Watir-Webdriver, a browser automation tool for effective cross-browser testing.
Author
Sean Robbins
Date

About Unboxing

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.

Watir-Webdriver

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:

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:

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:

One of the benefits I’ve noticed about using Watir-Webdriver is that when possible, Webdriver uses native OS events. This means that handling JavaScript events in the browser can be a lot easier than it was with Watir. For example, the standard .click action provided will, on Windows and Linux environments, fire the same event as a user mouse click

Webdriver’s interaction with browser elements offers some significant advantages over other automation tools I’ve used. For example, Watir’s handling of JavaScript WYSIWYGs such as TinyMCE was always slightly tricky. Watir-Webdriver can handle these types of UI elements with relative ease – it’s possible to write to a TinyMCE Frame with little fuss, such as in the following script:

There’s a known issue that prevents this script from running successfully with Chromedriver and I couldn’t get this to play nicely with HTML Unit (although I didn’t really expect it to) but compared to some previous solutions for automating applications that use these sorts of JavaScript heavy interfaces, Webdriver seems to be a real advance.

Going deeper

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

https://github.com/jarib/watir-webdriver/wiki/Comparison-with-Watir-1.X

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:

http://watirmelon.com/2010/04/10/watir-selenium-webdriver/

http://watirmelon.com/2010/12/14/watir-webdriver-a-detailed-introduction/

Add your comment

If provided, we will link to this from your name