Is there a method to handling modal dialog but not providing the title?

39 views
Skip to first unread message

Wesley Chen

unread,
Aug 26, 2009, 2:42:57 AM8/26/09
to Watir General

Hi, guys,

I mean that sometimes,  we know there is a modal dialog but we won't to know the title.

For following, we must provide a title. But I don't want to provide it sometimes.

=============================

    autoit=WIN32OLE.new('AutoItX3.Control')
#    ret=autoit.WinWait(title,"",180)                              # wait the popups for 3 minutes.
    ret=autoit.WinWait(title,"",60)                              # wait the popups for 3 minutes.

...

=============================


Any suggestion would be quite appreciated.
Thanks.
Wesley Chen.

Namit

unread,
Aug 26, 2009, 3:08:49 AM8/26/09
to Watir General
Hi,
If you do not want to use title of the pop up then you can use the
hwnd command to close the pop up by just passing the caption of
button. Visit this URl for some sample examples.

http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups

Thanks
Namit

Wesley Chen

unread,
Aug 26, 2009, 3:37:39 AM8/26/09
to watir-...@googlegroups.com
Hi, Namit,
Your suggestion maybe good. I know some kinds of solution.
But if I use autoit, there is no title, how can I make it?
autoit=WIN32OLE.new('AutoItX3.Control')   
ret=autoit.WinWait(title,"",60)

Thanks.
Wesley Chen.

Tony

unread,
Aug 26, 2009, 3:43:08 AM8/26/09
to Watir General
Hi Wesley,

There is a different technique to handling popups without autoit -
This will act only on the current ie window which watir is using not
any other browser.
The below will handle alert, confirm , prompt, auth dialog box.

Usage:
require 'watir'
iewin = Watir::IE.new
iewin.goto("http://www.w3schools.com/js/tryit_view.asp?
filename=tryjs_alert")
iewin.button(:value, "Show alert box").click_no_wait
txt = iewin.clickprompt()
puts txt #prints the popup text

You would have to add the below code to C:\ruby\lib\ruby\gems\1.8\gems
\watir-1.6.2\lib\watir\ie-class.rb (
add into module Watir, class IE

http://pastie.org/595060

Thanks,
Tony

Wesley Chen

unread,
Aug 26, 2009, 4:19:44 AM8/26/09
to watir-...@googlegroups.com
Hi, Tony,
Thank you very much for the quick reply.
:), 1) I can't find the below code;
2)will iewin.clickprompt( ) click the button on the pop up?  If it does, if on the pop up, there are two buttons such as "OK/Cancel", how to identify them?


Thanks.
Wesley Chen.

Tony

unread,
Aug 26, 2009, 4:49:16 AM8/26/09
to Watir General
Hi Wesley,

iewin.clickprompt() will click the button in the popup and return the
popup text.

This is the definition - def clickprompt(but="OK",txt ="", waitTime =
10)
iewin.clickprompt("CANCEL") # will click cancel button.. actually if
you pass in anything other than OK, cancel will be clicked.
iewin.clickprompt("OK") or iewin.clickprompt() will click the ok
button.
iewin.clickprompt("OK", "input text") #can be used to pass in values
into the prompt box.

iewin.clickprompt("OK", "", 20) #can increase the wait time for the
popup to occur. time = 20*0.2
will return '' if there is no popup.

Code is present here - http://pastie.org/595060
Pasting it here also -
################################################
def clickprompt(but="OK",txt ="", waitTime = 10)
tim = 0
poptxt= ''
while tim < waitTime
sleep 0.2
pophwnd = Win32API.new("user32", "GetWindow", 'Li', 'L').Call
(@ie.hwnd.to_i, 6)
# the above returns any popup windows that are present for
the specific window
tim += 0.2
tim += waitTime if pophwnd != 0
end
return '' if pophwnd == 0
button = but.upcase
outval = ' ' * 30
Win32API.new("user32", "GetWindowText", 'Lpi', 'L').Call
(pophwnd,outval,30)
popwndtitle = outval.rstrip.chomp("\000") # window title stored
here
outval = nil
#poptype = 0
#alert and confirm have ie6 - Microsoft Internet Explorer
#ie7 - Windows Internet Explorer
#ie8 - Message from webpage
if popwndtitle.include?("Microsoft Internet Explorer") ||
popwndtitle.include?("Windows Internet Explorer") ||
popwndtitle.include?("Message from webpage")
#confirm and alerts have the above 3 window titles
# poptype =1 means this is a javascript alert tag
#poptype = 1
poptxt = handlepopup1(pophwnd,button)
elsif popwndtitle.include?("Explorer User Prompt")
#prompts have the above window title
#poptype = 2
poptxt = handlepopup2(pophwnd,button, txt)
elsif popwndtitle.include?("Connect to")
#authentication dialog
#also make sure the username and password text fields are
present - if present we got the auth dialog
cntrlhwnd = 0
cntrlhwnd = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(pophwnd, 1002)
#poptype = 3 if cntrlhwnd != 0 #verified the 2 textboxes are
present to enter the values
return '' if cntrlhwnd == 0
poptxt = handlepopup3(pophwnd,button, prompt)
end
return poptxt
end

def handlepopup1(pophwnd, button)
# handles the alerts and confirm dialogs
#Yes there is a popupwindow... hence get the controlhandle for the
text control - 65535
cntrlhwnd = Win32API.new("user32", "GetDlgItem", 'Li', 'L').Call
(pophwnd, 65535)
#now get the text from the popup
outval = ' ' * 900
Win32API.new("user32", "GetWindowText", 'Lpi', 'L').Call
(cntrlhwnd,outval, 900)
poptext = outval.rstrip.chomp("\000")
outval = nil

#confirm ok-1 and cancel-2, alert ok-2
cntrlhwndOK = 0
cntrlhwndCANCEL = 0
cntrlhwndOK = Win32API.new("user32", "GetDlgItem", 'Li', 'L').Call
(pophwnd, 1)
if cntrlhwndOK == 0 # only 1 button alert
cntrlhwndOK = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(pophwnd, 2)
clickWin32Button(cntrlhwndOK) # done clicking javascript ok
button
return poptext
else # this is a confirm with 2 buttons
cntrlhwndCANCEL = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(pophwnd, 2)
end
button.include?("OK") ? clickWin32Button(cntrlhwndOK) :
clickWin32Button(cntrlhwndCANCEL)
#clickWin32Button(cntrlhwndCANCEL)
return poptext
end
private :handlepopup1

def handlepopup2(pophwnd, button, prompt)
#handles prompt boxes which takes a value as input
cntrlhwndOK = Win32API.new("user32", "GetDlgItem", 'Li', 'L').Call
(pophwnd, 1)
cntrlhwndCANCEL = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(pophwnd, 2)

# get handle to the text control from the prompt box
cntrlpromptText = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(pophwnd, 8132)
#now get the text from the popup
outval = ' ' * 200
Win32API.new("user32", "GetWindowText", 'Lpi', 'L').Call
(cntrlpromptText,outval, 200)
poptext = outval.rstrip.chomp("\000")
outval = nil

cntrltextarea = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(pophwnd, 8133)
if prompt.size != 0
sendmessage = Win32API.new("user32", "SendMessage", 'LLpp', 'L')
sendmessage.Call(cntrltextarea, 0x000C, '', prompt) # calling
sendmessage with WM_SETTEXT
end
button.include?("OK") ? clickWin32Button(cntrlhwndOK) :
clickWin32Button(cntrlhwndCANCEL)
return poptext
end
private :handlepopup2

def handlepopup3(pophwnd, button, prompt)
# handles the auth dialog box , 3 tries then the 401 page is shown
cntrlhwnd = Win32API.new("user32", "GetDlgItem", 'Li', 'L').Call
(pophwnd, 1002)
cntrlusername = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(cntrlhwnd, 1003)
cntrlpassword = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(cntrlhwnd, 1005)
if prompt.size == 2
sendmessage = Win32API.new("user32", "SendMessage", 'LLpp', 'L')
sendmessage.Call(cntrlusername, 0x000C, '', prompt[0]) # calling
sendmessage with WM_SETTEXT
sendmessage.Call(cntrlpassword, 0x000C, '', prompt[1]) # calling
sendmessage with WM_SETTEXT
end
cntrlhwndOK = Win32API.new("user32", "GetDlgItem", 'Li', 'L').Call
(pophwnd, 1)
cntrlhwndCANCEL = Win32API.new("user32", "GetDlgItem", 'Li',
'L').Call(pophwnd, 2)
button.include?("OK") ? clickWin32Button(cntrlhwndOK) :
clickWin32Button(cntrlhwndCANCEL)

end
private :handlepopup3

def clickWin32Button(cntrlhwnd)
Win32API.new("user32", "SendMessage",'LLLL','L').Call(cntrlhwnd,
0x0006, 1,0)
Win32API.new("user32", "SendMessage",'LLLL','L').Call(cntrlhwnd,
0x00F5, 0,0)
end
private :clickWin32Button

###################################

Thanks,
Tony

Wesley Chen

unread,
Aug 27, 2009, 2:54:51 AM8/27/09
to watir-...@googlegroups.com
Hi, Tony,
Thank you so much, I tried your code, it works perfect! It is amazing!


Thanks.
Wesley Chen.
Reply all
Reply to author
Forward
0 new messages