Selenium-WebDriver vs Sahi

Jan 17, 2013

selenium.logoJari Bakken has a couple of nice example tests that show some of the differences between a Selenium-WebDriver test and a Watir-Webdriver test. I thought I would create another example to show how the same code could be written in Sahi.

Both of the following scripts perform the same test. The test itself is simple:

  1. Go to the Google Translate page
  2. Click the Detect Language button
  3. Select Norwegian as your language
  4. Log the button's text
  5. Enter the word "ost" into the text field
  6. Verify "cheese" is the returned translateion

Selenium-Webdriver Example...

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get ""

wait = => 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

Sahi Example...



\_log("The selected button text is: " + \_getText(\_div("gt-sl-gms")));

\_setValue(\_textarea("source"), "ost");

\_assertEqual("cheese", \_getText(\_span("result\_box")));

The difference is pretty dramatic... at least in test size. Sahi handles all your waits for you so there's no need to clutter up your tests with wait code. Accessor code is also less verbose in Sahi. Overall, I would argue the Sahi code is much more readable but code beautry, like beauty in general, is in the eye of the beholder. I.E. your millage may vary...

Note: I used an assert instead of just logging the returned translateion... same dif

UPDATE: I've also added Geb to the fray!