Running Watir cross-browser using Internet Explorer, Firefox and Celerity

Since reading about Celerity, I’ve been excited by the idea that I might be able to take my Watir scripts and run them more quickly by using a browser simulator. It also raised the possibility of taking scripts and running them in different environments and on different platforms, because Celerity runs under JRuby without a browser. It makes http requests directly, and simulates javascript behaviours without actually rendering pages.

I had the hope that this would give me an option to get quicker feedback from my automation. Unfortunately, this hasn’t turned out to be the case for the applications I’m currently testing. Simulation of the javascript on the page seemed to take longer than the real browser. I got quicker feedback by turning off images, blocking certain external hosts and using Internet Explorer. I still hold out hope that Celerity may improve performance, or that the applications I’m testing will be less javascript intensive.

Once I set to getting my Watir scripts working with Celerity, I needed to overcome a few issues. The last of these was the number of differences between Celerity’s behaviour and Watir’s behaviour. Resolving this turned out to be reasonably simple, and the same strategy for getting consistent behaviour between Celerity and Watir driving IE turns out to solve some other problems with differences in behaviour in Watir’s drivers for IE and Firefox.

My first cut at this is by no means complete, with a bunch of unnecessary duplication that can be easily factored out. For now however, it gets the job done, and may be instructive for others.

The strategy is simple –

– Wrap the browser in your own class.
– Dynamically require the appropriate library for the driver you need
– Create your own methods that provide consistent behaviour for the different browsers. In my case, I needed to be able to get the actual html content of the page after it had been modified by scripts running on the page. Each driver (IE, Firefox, Celerity) required a different method to be called to achieve consistency, but using this approach, I can call browser.html and get (almost) the same result from all browsers and drivers.
– Intercept any calls to methods that you haven’t implemented and pass them through to the actual browser object.

This is the first time I’ve touched Ruby’s method_missing method, and it was pretty painless.

There are a few issues getting JRuby up and running with Celerity on windows as well. I’ll describe these shortly in another post. I’ve also recently had issues installing the latest version of Watir, and will detail the solutions to these as well.

Sample code is below. Note that you can actually do without assigning the browser to a global variable, but I provide this example just to show how you can fit this new solution into the ‘standard’ Watir approach. If you modify things so that you can specify the driver from the command line, you should be able to run tests using something like –

jruby main.rb –driver :celerity
ruby main.rb –driver :watir_ie
ruby main.rb –driver :watir_ff


----- driver.rb ------
require 'singleton'
require 'rubygems'

class Driver
include Singleton

def set_driver(driver)
@driver=driver
if @driver==:celerity
require 'celerity'
@browser=Celerity::Browser.new
end
if @driver==:watir_ie
require 'watir'
Watir::Browser.default='ie'
@browser=Watir::Browser.new
end
if @driver==:watir_ff
require 'watir'
Watir::Browser.default='firefox'
@browser=Watir::Browser.new
end
return @browser
end

def html
return @browser.document.body.innerhtml if @driver==:watir_ie
return @browser.xml if @driver==:celerity
return @browser.html if @driver==:watir_ff
end

def initialize
@browser=nil
end

def method_missing(method, *args)
@browser.send(method, *args)
end
end

----- main.rb ----
require 'driver.rb'

driver=:watir_ff
#driver=:watir_ie
#driver=:celerity

Driver.instance.set_driver(driver)
$browser=Driver.instance
$browser.goto("http://www.quinert.com/blog/")
puts $browser.html

One comment on “Running Watir cross-browser using Internet Explorer, Firefox and Celerity”

  1. Alister Scott says:

    Nice work. This looks neat.

Leave a Reply

Your email address will not be published. Required fields are marked *