Some fun with Watir

March 09, 2006

I like Watir for web testing. Combined with the Firefox Web Developer extension to determine the forms, areas, buttons names, I find it very efficient.

Although not the original intent, you can also use it to write small utilities relying on publicly available websites. For instance, let’s translate something through a translation website:

require 'watir'
include Watir

AVAILABLE_TRANSLATORS = {
  :fr_to_en => "65544",
  :en_to_fr => "524289"
}

def translate(word, translation)
  ie = IE.start(
    'http://elmundo.reverso.net/textonly/default.asp')
  ie.text_field(:name, 'source').set(word)
  ie.radio(:value, AVAILABLE_TRANSLATORS[translation]).set
  ie.wait
  result = ie.text_field(:name, 'target').value
  ie.close
  result
end

puts translate('today is a sunny day!', :en_to_fr)

Well that’s not exactly a SOAP call, but it works pretty well!