Equivalent of "click_no_wait" using win32ole and threads?


#1

Hi guys,

I have a watir script to download few files. In the script I am
currently using “click_no_wait” to download which is working fine.
But if I remove the download file from the backend and then run my
script. The script will hang as because “click_no_wait” expects a
download popup to show. I want my script to be full proof.

So I want something equivalent to “click_no_wait”. I have tried using
threads and win32ole library to achieve the same. With the code bellow I
have almost succeeded. This code can click and download the file. The
problem with the code bellow is that both the threads “t” and “s” still
runs after its job is done (does not join with the main thread). I am
not able to figure out why?.


#require

#download_file()
#download the file into the given path
def download_file(filepath)
ai = WIN32OLE.new(“AutoItX3.Control”)
ret = ai.WinWaitActive($wintitle[‘502’], “”, $sleeptime)
ai.WinWait($wintitle[‘502’], “”, $sleeptime)
ai.ControlFocus($wintitle[‘502’], “”, “&Save”)
sleep 1
ai.ControlClick($wintitle[‘502’], “”, “&Save”, “left”)
ai.WinWait($wintitle[‘503’], “”, $sleeptime)
sleep 1
ai.ControlSend($wintitle[‘503’], “”, “Edit1”, filepath)
sleep 1
ai.ControlClick($wintitle[‘503’], “”, “&Save”, “left”)
sleep 1
# wait for the window to manually close after download. Time out,
# as that might be the case when browser download window
automatically
# closes the window with option “Close the dialog box when download
completes”
# or something similar.
ret = ai.WinWaitActive($wintitle[‘504’], “Close”, $maxsleeptime)
sleep 1
if ret == 1
ai.ControlClick($wintitle[‘504’], “”, “Close”)
end
sleep 1
end #end of download_file()

ie = Watir::Browser.new
ie.goto “http://myportal/downloads
ie.wait
#click on download guide
t = Thread.new(){
ie.link(:text, /Download guide/).click
}
s = Thread.new(){
sleep 30
download_file(“c:\downloads\guide.doc”)
}
t.join
s.join

Please suggest, what is that I am doing wrong. Also let me know if there
is other alternatives.

Thanks in Advance,
Mandeep