Autonomous Machine

Steer WebDriver from Ruby with Backseat

I've been wanting to try out WebDriver for a while now, and this week I finally had a day to devote to kicking its tires. So far, I like pretty much everything about it. Oh, except that right now you have to write your tests in Java.

Backseat is a little Ruby library that uses Rjb to wrap the WebDriver classes in a nice interface. My goal is to integrate with Rspec eventually, but for now, only the basics work.

Here's an example (you can see the original Java source at the WebDriver site):

require 'backseat'
include Backseat

Backseat.load!('/Users/jimb/src/webdriver/trunk') # path to webdriver root

driver = Backseat::Driver.new(:firefox)

driver.get('http://www.google.com/webhp?complete=1&hl=en')

element = driver.find_element input(:name => 'q')
element.send_keys('Cheese')

wait :until => lambda { driver.has_child?(table(:class => 'gac_m')) &&
                        driver.find_element(table(:class => 'gac_m')).displayed? }

driver.find_elements(td(:class=> 'gac_c')).each do |e|
  puts e.text
end

driver.close # quit Firefox

For the most part, I've stuck to the original WebDriver API, but there are a few differences. One is the usage of a set of helpers for building XPath locators (used as arguments to find_element above), and the wait method which sure beats the way it was done in Java. There will be API changes going forward as I start using this for some real testing.

I have, of course, set up a repo at Github.