Monday 23 May 2011

Lessons from a WATIR to WATIR-Webdriver port - #5 Finally real Grid support!

My port from Watir to Watir-Webdriver has taken a bit of a back seat recently, and instead I have been working on a new project using Selenium-Webdriver (AKA Selenium 2).  I think a prefer Selenium mainly because I am writing in Java and I get compilation errors and better IDE support! Also, who can forget Selenium Grid? Watir has been crying out for a grid, and yes, I know there is "Watirgrid" but let's be honest, it's doesn't hold a candle to Selenium Grid.  To use "watirgrid" you have to re-write tests to take advantage of it.

Although Selenium-Webdriver does not work with Selenium Grid, I have been able to use a pre-beta version of Selenium Grid 2, and it's great. Easy to use, and with minimal effort I achieved things which previously had taken a lot of effort with Watir.

However, back to promoting Watir! With my Selenium Grid 2 up and running, I thought "mmm, if Watir-Webdriver uses the same Webdriver code as Selenium-Webdriver, can I use the grid with Watir?"  The answer is yes!

This great overiew of Watir-Webdriver (http://watirmelon.com/2010/12/14/watir-webdriver-a-detailed-introduction/) mentions running via Remote Webdriver Server.  Although the blog entry refers to the Remote Webdriver Server for headerless testing is can still be used for browsers.

Assuming you had started a locally hosted Remote Webdriver Server on port 4444, a test for firefiox would look something like this:


require "rubygems"
require "watir-webdriver"
require "watir-webdriver/extensions/wait"
b = Watir::Browser.new(:remote, :url => "
http://127.0.0.1:4444/wd/hub", :desired_capabilities => :firefox)
b.goto "
www.google.com"www.google.com"
b.text_field(:name => "q").set "Watir-WebDriver"
b.button(:name => "btnG").click
b.div(:id => "resultStats").wait_until_present
puts "Displaying page: '#{b.title}' with results: '#{b.div(:id => "resultStats").text}'"
b.close

Here comes the good part! If I change the URL to a Selenium Grid 2 hub machine, then my test actually runs on the grid. I could have 10 tests running in paralell on my machine, but the browser under test would actually be on different machines for each test.
Looking back on this entry, I really have explained this all quite poorly. In my defence, it is getting late.

Instead of listening to me, try the following;

1. Goto http://selenium-grid.seleniumhq.org/ , understand what a grid gives you and get jealous that it is only for Selenium!

2. Think about how great your Watir tests would be if you had a grid.

3. Get the Selenium 2 Grid source code, build it and set up a simple grid

4. Run a Watir-Webdriver test against your grid

5. Get very excited

Obviously you still need a parallel running solution to help you leverage the grid to the full potential of distributed test runs . As my new project is in Java, I'm using TestNG which comes with "free" multi-threading!