Pages

Friday, 19 July 2013

Headless Watir

One of the most common complaints in my teams sprint retros is the speed of our BDD test suite. We use a Cucumber/Ruby/Watir/Firefox stack to run our tests. The slowest component in this is powering a full web browser.
My base line will be a small subset of our tests which in Firefox takes 22.8 seconds to execute 26 steps.

HttpUnit
HttpUnit is an in-memory browser written in Java. It is not based on any real-life browser so issues in Cukes may not represent real-life issues.

First I looked into Celerity (a Ruby wrapper for HttpUnit) however this seamed to require a switch to JRuby and alas some of the other gems we use do not support JRuby. :(  HttpUnit however is supported by Selenium.
To make use of this first you need to download the Selenium Server jar and kick it off.

$> java -jar selenium-server-standalone-2.33.0.jar

Then configure Watir to use it:

require 'watir-webdriver'
include Selenium

capabilities = WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
browser = Watir::Browser.new(:remote, :url => 'http://127.0.0.1:4444/wd/hub', :desired_capabilities => capabilities)

browser.goto 'http://google.com'
# Do stuff with the browser object

browser.close


One thing that tripped me up was that browser.window.resize_to causes an exception to be in Ruby.
Faster than Firefox: No. It took 31 seconds to run my 26 steps.
Compatibility: All the (fairly simple) steps passed; however when running it against a large test suite I had many failures.
Setup: The additional set up is a bit of a pain as you require an additional application running during the tests.
Screenshots: No

PhantomJS
PhantomJS is a WebKit based headless browser. It is approximately a Headless Safari.

PhantomJS is very easy to set up, just download the executable and make sure it is in your path. Then change the browser to be PhantonJS.

require 'watir-webdriver'

browser = Watir::Browser.new :phantomjs

browser.goto 'http://google.com'
# Do stuff with the browser object

browser.close


Faster than Firefox: Yes. It took 18.9 seconds to run my 26 steps.
Compatibility: All the (fairly simple) steps passed; however when running it against a large test suite I had a few failures.
Setup: Simple, just make sure you have PhantomJS installed and in your path.
Screenshots: Yes

SlimerJS
SlimerJS is a Gecko based mostly-headless browser. Gecko is the engine behind Firefox so should have a very good compatibility with our existing tests. Alas it does not currently support WebDriver so I can not currently swap it using my existing test suite. One note on this is that it is "nearly headless". The way Gecko works is that it requires something on screen. However it does not draw to this so should be faster than using Firefox.


No comments:

Post a Comment