Simple Ajax examples

require 'watir'
require 'test/unit'

# Quick example of Ajax examples using Watir, should work with versions
# 1.5.1.1148 and above.

class Ajax_Example < Test::Unit::TestCase
  include Watir

  def test_scriptaculous_autocompleter
    browser = Browser.start('http://demo.script.aculo.us/ajax/autocompleter')
    browser.text_field(:id, 'contact_name').set('al')

    # need to fire a key press event after setting the text since the js is handling
    # keypress events
    browser.text_field(:id, 'contact_name').fire_event('onkeypress')   

    # the li/lis tags are available in watir 1.5.1
    wait_until {browser.div(:id, 'contact_name_auto_complete').lis.length > 0}
    puts browser.div(:id, 'contact_name_auto_complete').lis.length
    puts browser.div(:id, 'contact_name_auto_complete').li(:index, 5).text
    browser.div(:id, 'contact_name_auto_complete').li(:text, 'Alan Jochen').click
 end
end
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.