Selenium-WebDriver vs Watir-WebDriver in Ruby

One question that I often see arise is what is the difference between using watir-webdriver and the selenium-webdriver ruby bindings?

I imagine that the best person to answer this is Jari Bakken, as he wrote both! He did an excellent demo at the Selenium Conference highlighting the differences between the ruby webdriver bindings (selenium-webdriver) and the watir webdriver bindings (watir-webdriver), which I’ve included below.

To do a Google Translate test in selenium-webdriver:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get "http://translate.google.com/"

wait = Selenium::WebDriver::Wait.new(:timeout => 5)

language_button = wait.until {
  element = driver.find_element(:id => "gt-sl-gms")
  element if element.displayed?
}

language_button.find_element(:tag_name => "div").click

menu = wait.until {
  element = driver.find_element(:id => "gt-sl-gms-menu")
  element if element.displayed?
}

langs = menu.find_elements(:class => "goog-menuitem")

norwegian = langs.find { |lang| lang.text == "Norwegian" }
norwegian.find_element(:tag_name => "div").click

puts language_button.text

driver.find_element(:id => "source").send_keys("ost")

result = wait.until {
  result = driver.find_element(:id => "result_box").text
  result if result.length > 0
}

puts result
driver.quit

the same functionality in watir-webdriver looks like:

require 'watir-webdriver'

browser = Watir::Browser.new :firefox
browser.goto "http://translate.google.com/"

language_button = browser.span(:id => "gt-sl-gms")
language_button.when_present.div.click

menu = browser.div(:id => "gt-sl-gms-menu")
menu.when_present.div(
  :class => /goog-menuitem/,
  :text => "Norwegian"
).div.click

puts language_button.text

browser.text_field(:id => "source").set("ost")

result_box = browser.span(:id => "result_box")
browser.wait_until { result_box.text.length > 0 }

puts result_box.text
browser.close

So whilst these achieve the same thing, I find the selenium-webdriver example is more verbose, and and less readable too.

If I was implementing automated tests using WebDriver in ruby, there’s no doubt that I would choose watir-webdriver over selenium-webdriver.

11 thoughts on “Selenium-WebDriver vs Watir-WebDriver in Ruby

  1. I think they are similar,as you know watir is more close than selenium for webdriver.waitr can drive browser but for selenium version 1,it have to set up a proxy server. the code style is not so import,because there are so many BDD/DSL tools

  2. Isn’t there more to it than just the api?

    Isn’t selenium-webdriver the ‘future’? I mean the main selenium project is merging with webdriver for 2.x release.

    If watir-webdriver relies on selenium 1.x under the covers then I don’t think it should be recommended.

    I could be wrong, because I am new to Ruby/Cucumber and I find the whole eco-system really confusing!!!

    • Watir-WebDriver is a WebDriver implementation therefore is not linked to Selenium 1.x at all.

  3. Hello Alister,

    In my code, I “require watir-webdriver” only.
    I sometimes get some errors pasted below…
    Dont these messages mean that watir-webdriver is using selenium-webdriver under the covers ?

    ****************************************************************************************************
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/response.rb:50:in `assert_ok’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/http/common.rb:58:in `new’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/http/common.rb:58:in `create_respons
    e’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/http/default.rb:64:in `request’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/http/common.rb:39:in `call’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/bridge.rb:450:in `raw_execute’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/bridge.rb:428:in `execute’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/remote/bridge.rb:302:in `getElementTagName’

    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.14.0/lib/selenium/webdriver/common/element.rb:44:in `tag_name’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.4.0/lib/watir-webdriver/locators/element_locator.rb:320:in `validate_eleme
    nt’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.4.0/lib/watir-webdriver/locators/element_locator.rb:38:in `locate’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.4.0/lib/watir-webdriver/elements/element.rb:283:in `locate’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.4.0/lib/watir-webdriver/elements/element.rb:259:in `assert_exists’
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.4.0/lib/watir-webdriver/elements/element.rb:64:in `text’
    ****************************************************************************************************

    • Yes, definitely, when you install ‘watir-webdriver’ you get ‘selenium-webdriver’ too.

  4. Thanks Alister.

    I’ve been using ‘watir-webdriver’ for a couple of weeks now… its working very well for me.

  5. I am using cucumber. We are using Selenium IDE to record test cases and then export them to RSpec(webdriver) format. Can Selenium IDE export test case to watir-web driver format instead of just web driver format. As watir-web driver provides headless option compared to webdriver, we would like to go for the same..

    Please help….

    • No, selenium doesn’t (and won’t) support watir-webdriver.
      You can run selenium webdriver in headless the same as watir-webdriver.

  6. I’m new to Ruby scene, even more so to the automation scene. From what I’ve found in Selenium you can set an ‘Implicit’ wait with the WebDriver. Then you never have to worry about creating ‘Explicit’ waits and their corresponding functions.

    The Selenium::WebDriver::find_element function will then allow however long you set for your ‘Implicit’ timeout before failing. I haven’t looked under the hood for Watir, but I’m sure Watir has some predetermined setting before it will timeout as well.

    Of course this is with Selenium 2 WebDriver, so this may not be there for Selenium 1 stuff. Either way, good job with the two examples posted.

Comments are closed.