Thursday, March 27, 2008

Dynamic Finders with Watir

If you've used rails, then you've probably seen the dynamic finders that active record provides. I thought it'd be interesting to see if that kind of behavior could be added to Watir. It turned out to be a nice little exercise.

I'm not sure how it will play in real test code, but I'm going to try it out over the next couple of days.

Here is an example of what you can do:
require 'watir'
require 'dynamic_finder.rb'

include Watir

ie = IE::start_process('www.google.com')
p ie.div_by_id(/gbar/).id # ie.div(:id => /gbar/)

p ie.span_by_class(/gb1/).text # ie.span(:class => /gb1/)
p ie.span_by_class_and_index(/gb1/, 2).text # ie.span(:class => /gb1/, :index=>2)

p ie.span_by_index(2).text # ie.span(:index => 2)
p ie.text_field_by_index(1).name # ie.text_field(:index,1)

ie.text_field_by_name("q").set('watir')
ie.button_by_value('Google Search').click

2.times do
ie.link_by_text(/Watir Tutorial/).click
end

ie.close

You can find the source files for the change and the example posted above here. Enjoy!


No comments: