My evaluation of Watir as an automation tool for my website is complete. I really like this tool! It runs fast - a lot faster than Silk Test that I've used before. The only con right now is there's no record and playback (you have to look into WET or other Watir Maker). So far I haven't had too many issues finding the object on the page.
First start with the online tutorial and FAQ:
Watir works with IE 6 and IE 7, with the assistance of the IE Developer Toolbar. Navigate to your webpage with the toolbar. Create your Watir scripts using the Scite text editor that comes with the Ruby installation (colors make your life a whole lot easier). Save files with a .rb extension. To run, simply find the script.rb and double click it.
With an IE browser window open with the tool bar, click the arrow on the left and select the object on your page. You'll see the attribute - usually an id, name, href, and so on. You can just follow the basic syntax below (comments are followed by the # symbol):
browser.object(:attribute,"value").action
$ie.button(:id,"button1").click
Start of a script:
require 'watir'
$ie = Watir::IE.new
$ie.set_fast_speed #Compare this with $ie.set_slow_speed, you'll see a big difference!
$time = Time.now.strftime("%m/%d/%y-%H:%M:%S") # Get the time now and format to string. Can also try $time = Time.now.to_s
$ie.goto("www.newsqa.com") #Note that $ next to the variable name is not required.
-----------------------------------------------
Watir/Ruby code examples:
Read from a file in Watir script:
-----------------------------------------------
#Write to a file in Watir script: Open the file you want to write to in append mode. If a file exists, it'll append #to it. If not, it creates a new one
File.open('nameYourFile.txt', 'a') do |f|
f.puts "Writing to file. Code gets indented"
end #Make sure to end the loop
f.close # close the file when done writing to it
------------------------------------------------
Bypassing an Internet Explorer 7 security warning:
if $ie.text.include? "Login" #checks that text is included on the page
puts "Login page found, so login!" #puts writes to the command window
else
f.puts "Security Alert must be in the way. Click to bypass"
$ie.link(:id, "overridelink").click
f.puts "Clicked the continue link in IE 7"
end
------------------------------------------------
Bypass Internet Explorer 6 security warning:
------------------------------------------------
Bypassing a Javascript popup warning
These are pretty tricky. After much research, I got it to work by adding this to the script:
require 'watir'
require 'watir/winClicker'
require 'watir\contrib\enabled_popup'
def startClicker( button , waitTime= 9, user_input=nil )
#get a handle if one exists
hwnd = $ie.enabled_popup(waitTime)
if (hwnd)
w = WinClicker.new
puts ("yes there is a popup")
if ( user_input )
w.setTextValueForFileNameField( hwnd, "{user_input}" )
end
#"OK" or whatever the name on the button is
w.clickWindowsButton_hwnd( hwnd, "#{button}" )
puts ("Pop up button is: #{button}")
sleep 2
w=nil
end
end
# Use click_no_wait on the object that triggers the Javascript warning
$ie.link(:id, "deleteProduct").click_no_wait
#In the section of the script where the popup occurs:
startClicker( "OK", 3)
4 comments:
The click_no_wait to handle the javascript popups doesn't seem to work consistently in Vista/certain IE versions on XP per reports in the google groups. I can't seem to get it to work on vista/IE 7 either.
Thanks a lot!!!!
I will need to click this dialog button but in Firefox... any idea how to do it?
Me, too. Any idea how to do this in Firefox?
Post a Comment