Thursday, January 08, 2009

How to handle the popup window using watir scripts

sometimes, we will meet the popup window issue with our automation scripts, it can not catch the window easily, so use the AutoItX3, we can solve this kind of problem well:
require "watir"
require "win32ole"

def check_for_popup_window

flag =0
autoit = WIN32OLE.new('AutoItX3.Control')

while (flag ==0)

# check the popup window status, set the timeout =1

flag = autoit.WinWait("Microsoft Internet Explorer", "", 1)

#another way to do this, you can get the popup info with autoit Spy tool
#autoit.ControlClick("Microsoft Internet Explorer", "", "[CLASS:Button; INSTANCE:1]")

puts(flag)

# If window found, send appropriate keystroke (e.g. {enter}, {Y}, {N}).

if (flag==1) then autoit.Send("{Enter}") end

sleep(0.5)
end
end

#main body of watir program:

ie = Watir::IE.new

ie.speed = :fast

puts "Step 5: Click one vendor detail link"

#create a new thread to call check for popup window method
popup = Thread.new {check_for_popup_window}

ie.goto test_vendorURI

sleep 1

#tear down the thread has been created
at_exit { Thread.kill(popup) }

No comments:

Post a Comment