2

I’m using SHDocVw.InternetExplorer APIs in my Vb.Net WinForms application to capture requests sent from IE in my application.

As given on this link

"The NewWindow3 event is only fired when a new instance of Internet Explorer is about to be created. Calling showModalDialog or showModelessDialog does not trigger an event because they are not new instances of Internet Explorer"

I am trying to automate user actions in Internet Explorer for automated testing. So I want to know that due to some user action a modal dialog is opened or some alert window is opened.

So my question is:

  • How do I know that a modal dialog window is opened in Internet Explorer?

  • Is there any other event raised by Internet Explorer in this case?

  • If it's not possible directly then is there some other way to handle this scenario?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
prem
  • 3,348
  • 1
  • 25
  • 57

3 Answers3

1

The only thing I was able to find is the WindowStateChanged event http://msdn.microsoft.com/en-us/library/aa768307(v=vs.85).aspx which mentions:

The WindowStateChanged event is raised when the state of a content window, such as the browser window or a tab, might have changed. The following actions raise this event.

  • The browser window is minimized or restored.
  • An active tab becomes inactive.
  • An inactive tab becomes active.
  • The browser window is enabled or disabled due to a modal dialog box.

Here is a VB.NET code example of how to use the WindowStateChanged event to check the enabled/disabled and visible flags:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Code to instantiate the IE COM object'
        Dim IE As New SHDocVw.InternetExplorer()
        AddHandler IE.WindowStateChanged, AddressOf OnWindowStateChanged
        'do stuff here'
        IE.Quit()
    End Sub

    Public Sub OnWindowStateChanged(ByVal dwWindowStateFlags As UInteger, ByVal dwValidFlagsMask As UInteger)
        Const OLECMDIDF_WINDOWSTATE_USERVISIBLE As UInteger = 1
        Const OLECMDIDF_WINDOWSTATE_ENABLED As UInteger = 2

        'Check if the window is disabled'
        If dwValidFlagsMask And OLECMDIDF_WINDOWSTATE_ENABLED = OLECMDIDF_WINDOWSTATE_ENABLED Then
            If dwWindowStateFlags And OLECMDIDF_WINDOWSTATE_ENABLED <> OLECMDIDF_WINDOWSTATE_ENABLED Then
                'The browser window has been disabled.... possibly by a modal popup'
            Else
                'The browser is enabled.'
            End If
        Else
            'The enabled/disabled flag has not been initialized, so we cannot check the state during this event handler execution'
        End If

        'Check if window is visible: '
        If dwValidFlagsMask And OLECMDIDF_WINDOWSTATE_USERVISIBLE = OLECMDIDF_WINDOWSTATE_USERVISIBLE Then
            If dwWindowStateFlags And OLECMDIDF_WINDOWSTATE_USERVISIBLE <> OLECMDIDF_WINDOWSTATE_USERVISIBLE Then
                'The browser window is NOT visible'
            Else
                'The browser window is visible'
            End If
        Else
            'The visible flag has not been initialized, so we cannot check the state during this event handler execution'
        End If

    End Sub
End Class
BateTech
  • 5,780
  • 3
  • 20
  • 31
  • Thanks for your answer, but the browser is getting disabled due to other reasons also like Alert, Organize Favorites window etc. – prem Aug 18 '14 at 09:50
  • You code pointed me a bit closer to what I want to achieve, So I am accepting this as answer. Thanks for your help. – prem Aug 20 '14 at 13:22
1

The event you're looking for probably doesn't exist, however there's a number of workarounds for detecting if a modal is open.

You can use jquery to add/ remove classes on the modal: Can I check if Bootstrap Modal Shown / Hidden?

You can check the console: check if jquery reveal modal is active, do something

I don't recommend the console approach, there's more to it like when the console's not open.

I can think of several ways to solve your problem without caring about the event at all however, couldn't the modal talk to the IE page and then use the API to talk to your app? You can use show() & hide() to change the state of the modal and when that state changes fire additional code to do what you need to.

Another option is to save values to a database and read them back in the app (doesn't require the API obviously).

If none of these sound good, perhaps you should tell us exactly what you're trying to accomplish from a functional standpoint and we'll be able to architect / give you a solution.

Community
  • 1
  • 1
RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
  • I am trying to automate user actions in internet explorer for automated testing. So I want to know that due to some user action a modal dialog is opened or some alert window is opened. – prem Aug 18 '14 at 09:53
  • 1
    Ah, never had the time or resources to implement API testing myself, but if all you're doing is testing to see if the modal opens or not, my approach will still work... simulate a click event on whatever opens the modal & then check for the new css class on the underlying page, if it's there, your modal's code executed meaning it opened! – RandomUs1r Aug 18 '14 at 14:54
0

There is no a such event, but you can use some other approaches:

  1. Webbrowser.Document.Window.Error is fired before modal window will be displayed
  2. You can implement INewWindowManager interface - I don't know exactly, but I think it can give some information to you

  3. You can implement IDocHostUIHandler and use ShowUI method - but it executed not only for displaying new window, you should use it carefully

  4. IOleCommandTarget - 'Exec' method with cmdId OLECMDID_SHOWSCRIPTERROR, OLECMDID_SHOWMESSAGE, OLECMDID_SHOWFIND, OLECMDID_SHOWPAGESETUP, OLECMDID_SHOWPRINT means that new window will be opened
Peter O.
  • 32,158
  • 14
  • 82
  • 96