Friday, March 26, 2010

An example on Google Suggest case with Watir

One of my friend ask if Watir can give a demo on "Google suggest test" (selenium treat it well,I guess). However, I was not finding an example on line for this case. Due to I am a fan of Watir, so I want to know this as well, here comes mine version, Enjoy!

require "watir"

# setup time out threshold
waiter = Watir::Waiter.new(8)

ie = Watir::IE.new

ie.goto("http://www.google.com/")

ie.text_field(:name, 'q').set('dev')


begin
#wait until find the google suggestion list, otherwise wait until timeout
waiter.wait_until {ie.row(:class, /gac_/).exists?}


rescue Exception => e
puts " Timeout, there is no suggestion list for this Keyword search"
puts " now end the test"

# sleep 2 seconds so that let human take a look :)
sleep 2

else
num_suggestions = ie.table(:class, 'gac_m').row_count_excluding_nested_tables()
rand_pick = rand(num_suggestions - 1)+1;
puts clickable_text = ie.table(:class, 'gac_m').row_values(rand_pick)
ie.cell(:text,"#{clickable_text}").click();

flag = ie.contains_text(/#{clickable_text}/);
if flag == nil
f = File.new("Error_page" + Time.now.to_i.to_s + ".html", "w+")
f << ie.html
raise "Error pages detected"
end

ensure
puts "pretty cool ending"
ie.close()

end


BTW, here is what I get from Selenium IDE(Xpath it powerful however it should be the last choice for me), seems useless for me:

selenium.open("/");
selenium.type("q", "ab");
selenium.click("//tr[4]/td");
selenium.waitForPageToLoad("30000");


Sometimes, I am a little bit sad about less people talked about Watir, they are moving to Selenium more and more, i am not against Selenium/Webdriver, however i do not want to see Watir group growth slow down.Go Watir Go!

1 comment: