Watir-WebDriver with GhostDriver on OSX: headless browser testing

GhostDriver has been released which means it is now easy to run reliable headless WebDriver tests on Mac OSX.

Steps to get working on OSX

  1. First make sure you have homebrew installed
  2. Run
    brew update

    then

    brew install phantomjs

    which should install PhantomJS 1.8.1 or newer

  3. Run irb and start using GhostDriver!
    require 'watir-webdriver'
    b = Watir::Browser.new :phantomjs
    b.goto "www.google.com"
    b.url #"http://www.google.com.au/"
    b.title #"Google"

I’ve tested it on a large test suite (123 scenarios) and it behaves the same as other browsers with full JavaScript support. It took 8m13s in total: surprisingly it is slightly slower than ChromeDriver (7m30s) in my testing, but a little faster than the Firefox Driver (9m33s).

Well done to all involved in this project. It’s great to see a reliable, realistic headless browser with full JavaScript support for WebDriver finally released.

And yes, in case you’re wondering, it does screenshots!

Setting text in Chromedriver is rather slow

A colleague contacted me today to see if there’s a way to increase the speed of a text_field.set operation in watir-webdriver when using Chrome on Mac OSX. I hadn’t noticed it before but Chrome seems to enter character by character into text fields which is noticeably slower than Firefox which seems to ‘set’ the field at once. Jari Bakken suggested I try Chrome native events which I didn’t even realize existed on Mac OSX.

So, I took Jari’s suggestion and I created a benchmark of a .set operation conducted ten times in a row on Firefox, and Chrome with and without native events. The results looked like:

                        user       system     total        real
set firefox             0.160000   0.020000   0.180000  ( 8.205473)
set chrome non-native   0.100000   0.010000   0.110000  (44.980334)
set chrome native       0.100000   0.010000   0.110000  (31.501647)

You can see that Chrome is drastically slower than Firefox in setting text fields, but native events does seem to provide a roughly 25% speed improvement. I think I will stick to running my watir-webdriver tests against Firefox unless specifically needed.

Sample Code for Benchmarks

Setting text using Chrome with native events enabled

require 'selenium-webdriver'
require 'watir-webdriver'
require 'bench'

d = Selenium::WebDriver.for :chrome, native_events: true
b = Watir::Browser.new d
b.goto 'bing.com'

benchmark 'set chrome native' do
  b.text_field(name: 'q').set 'ghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghgh'
end

run 10
b.close

Setting text using Chrome with native events disabled

require 'selenium-webdriver'
require 'watir-webdriver'
require 'bench'

b = Watir::Browser.new :chrome
b.goto 'bing.com'

benchmark 'set chrome non-native' do
  b.text_field(name: 'q').set 'ghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghgh'
end

run 10

b.close

Setting text using Firefox (native events aren’t available on OSX)

require 'selenium-webdriver'
require 'watir-webdriver'
require 'bench'

b = Watir::Browser.new :firefox
b.goto 'bing.com'

benchmark 'set firefox' do
  b.text_field(name: 'q').set 'ghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghgh'
end

run 10

b.close

Using the super-duper ChromeDriver with Watir-WebDriver (updated)

Anyone who has used Watir-WebDriver (or WebDriver for that matter) to test Google Chrome will know that it’s been pretty unreliable, and pretty much unusable in the past… until now.

The ChromeDriver executable was released recently which enables WebDriver to control Google Chrome natively using the Wire protocol. This means your Watir-WebDriver tests will run super fast, and super reliably.

For Mac OSX:

  1. Get the latest Watir-WebDriver gem
    gem update watir-webdriver
  2. Download the ChromeDriver binary from the Chromium site, and copy it to your path (such as /usr/local/bin/)
  3. This will work with any current version of Chrome
  4. Change your Watir-WebDriver script to use Watir::Browser.new :chrome and voila, super fast Chrome, with no weird first tab opened in the background. This is the key to tell if you’re doing it right, your app should appear in the first tab of Chrome, not the second.

I haven’t done this on Windows yet, so your mileage may vary, but I imagine it wouldn’t be too different.