QTP: Interacting with IE dialog boxes dynamically

***Excerpted from an article I posted to the Mercury Support site:

IE sometimes pops up dialogs. What if we could write code that would handle these without knowing which boxes and in what order they will appear?

Clicking Internet Explorer Dialogs Dynamically

Recently, I was plagued with Internet Explorer Security Alert and Security Information dialog box problems. Seemed like everytime I changed environments (we are a web shop), where these appeared changed. Sometimes, I'd get both, or neither, or just one or the other. I could try and compensate for it, but ultimately led to frustration.

For example, I used the Browser("Browswer").Dialog.Exist function to detect the boxes, then close them. Only trouble was that if the box *didn't* exist, it took QTP 20-30 seconds to tell me. A test run that should only take 15 seconds was taking almost two minutes. While speed isn't something I normally worry about, I felt this was terribly inefficient. Also, what if I changed environments for a particular run? It was concievable that I'd get a dialog in the new environment that I hadn't compensated for, and the whole run would stop. What if this occurred overnight while running a big bank of tests?

I finally wrote some code to use two Win API calls to handle the situation. One, FindWindowEx, would locate the dialogs for me (to Windows OS, *everything* visible is a window with a "handle"). Once I located the one I wanted, I used PostMessage to send the click to the dialog, and off we went. These functions work so fast you hardly notice the boxes pop up. Once I got this point, I registered them to my browser object using the RegisterUserFunc function in QTP. Now, I simply call Browser("Browser).ClearDialogs anywhere I think I might see them. The code has become more self-regulating and intelligent as a result.

I stored these in an external VBS file for ease.

***End Excerpt

I'll include the code once I get it formatted for the blog.

EDIT: Rather than get blasted by the BlogNotSee, I'll include a link to my code once I get it ready and posted on my own site. No need incur his wrath for something like this.....If you know him, you know what I mean. :-)


 

EDIT: I've had many requests to see the actual code, and I keep forgetting to post it. Anyway, here it is:

Option Explicit

Public Const WM_COMMAND = &H111&

Extern.Declare micHwnd, "FindWindowEx", "user32.dll", "FindWindowExA", micHwnd, micHwnd, micString, micString
Extern.Declare micLong, "PostMessage", "user32.dll", "PostMessageA", micHwnd, micLong, micLong, micLong
Extern.Declare micLong, "SendMessage", "user32.dll", "SendMessageA", micHwnd, micLong, micLong, micLong


Public Sub ClearDialogs()
'************************************************************************
'*Function: ClearDialogs
'*Author: Theo Moore
'*Date: 02142006
'*Assumptions:
'*    - That the Object being passed here
'*    is an Browser object
'*Inputs:
'*    - Object: The object to used in downstream
'*   
'*Outputs:
'*    - None
'*     
'*Notes:
'*    This function is registered to Browser objects. It checks to see
'*    if any dialogs are present. Using descriptive prog, we can get the
'*    dialog without having to learn it. We can also add more logic
'*    to handle other dialogs since sometimes we get more than one.
'*************************************************************************
    Dim Hwnd
    Dim blnNoMoreDialogs
    Dim lResult

    blnNoMoreDialogs = False
   
    Wait 2 'We have to wait, as this call may execute faster then IE can pop the box

    Do Until blnNoMoreDialogs       
        Hwnd = extern.FindWindowEx(0,0,vbNullString, "Security Alert")
        If Hwnd = 0 Then
            Hwnd = extern.FindWindowEx(0,0,vbNullString, "Security Information")
        End If

        blnNoMoreDialogs = Not(CBool(Hwnd))

        If Hwnd > 0 Then
            lResult = extern.PostMessage(Hwnd, WM_COMMAND, 6, 0)
            'TM - 10192006
            'Added a one second wait here to let another window pop should
            'one need to be shown immediately after the first one
            Wait 1
        End If

        Hwnd = 0
    Loop
   
End Sub
RegisterUserFunc "Browser", "ClearDialogs", "ClearDialogs"

posted @ Tuesday, February 21, 2006 5:30 AM

Print

Comments on this entry:

# re: QTP: Interacting with IE dialog boxes dynamically

Left by Will at 3/9/2006 7:32 PM
Gravatar
Did you try Dialog.Exist(0) or (1)? That should speed it up quite a bit if it works for you.

# re: QTP: Interacting with IE dialog boxes dynamically

Left by Theo Moore at 3/9/2006 8:16 PM
Gravatar
Actually, I did. I ran into issues where QTP wouldn't see the right/wrong box depending on which development environment I was in. Ultimately, it was too unreliable to allow me to write code that would work in all environments. The Win API code, while a little more work, is much more reliable.

Thanks for the input!
Theo

# re: QTP: Interacting with IE dialog boxes dynamically

Left by Will at 3/10/2006 7:23 AM
Gravatar
Now that you say it that way. I think I may have run into this problem recently. I was using the 9.0 beta and assumed it was only a bug with 9.0, but I never tried running the same code on 8.2. Maybe that bug has been around longer than I thought.

# re: QTP: Interacting with IE dialog boxes dynamically

Left by Nick at 3/30/2006 4:41 AM
Gravatar
could you please post the code in the blog.

Thanks

# re: QTP: Interacting with IE dialog boxes dynamically

Left by Snap252 at 8/21/2006 12:04 AM
Gravatar
can u pleeeaase post the code, or tell us, where to find it?

THX

# re: QTP: Interacting with IE dialog boxes dynamically

Left by mahadevi at 8/28/2007 8:17 AM
Gravatar
for Infromation

# re: QTP: Interacting with IE dialog boxes dynamically

Left by Ross at 10/20/2007 11:53 PM
Gravatar
can u please post the code, or tell us, where to find it?

Thanks,
Ross

# re: QTP: Interacting with IE dialog boxes dynamically

Left by Mahesh Dasari at 2/22/2008 11:26 PM
Gravatar
Can you let me know how to get Dialog box text

Thanks in Advance,
Mahesh Dasari
Comments have been closed on this topic.
«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910