How to wait for SHDocVw InternetExplorerClass.DocumentComplete event in a C# unit test?

0

I have a unit test code that needs to open a page in IE and do something after the document is complete. The page contains redirects and at the end loads Silverlight (we are stuck with it for another year).

Here is the code:

using System;
using System.Threading;
using System.Windows.Forms;
using Common;
using NUnit.Framework;
using SHDocVw;

namespace Web
{
    partial class ForEachWebServer
    {
        private class IEEvent
        {
            public object Url;

            public void OnDocumentComplete(object pDisp, ref object URL)
            {
                Url = URL;
            }
        }

        [Test, Category("non-ui"), Category("xap")]
        [SkipTestExecutionForServicesBinding]
        public void XAPDownload()
        {
            var ieEvent = new IEEvent();
            var ie = new InternetExplorerClass();
            ie.DocumentComplete += ieEvent.OnDocumentComplete;
            ie.Visible = true;
            ie.Navigate("ceridian.com");
            while (ieEvent.Url == null)
            {
                Application.DoEvents();
                Thread.Sleep(50);
            }
            Console.WriteLine($"Navigation complete: {ieEvent.Url}");
        }
    }
}

But ieEvent.Url remains null forever. Also if I try to access ie.Busy at some point while still waiting for the loop to end I get this:

System.Runtime.InteropServices.COMException: 'The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))'

What am I doing wrong?

EDIT 1

I have a fully functional project here - https://dev.azure.com/MarkKharitonov0271/_git/BrowserTest

  • When ran without any parameters it opens the WebBrowser Windows Forms control, navigates to www.ceridian.com and opens a modal dialog when the DocumentComplete event arrives for ceridian. Closing the dialog ends the application.
  • When ran with a single command line argument, say X it opens the IE browser using the InternetExplorer COM object, navigates to http://www.X.com and opens a modal dialog when the DocumentComplete event arrives for X. Closing the dialog ends the application.

Now, all works fine for:

  1. WebBrowser control for www.ceridian.com - BrowserTest.exe
  2. IE window for www.live.com - BrowserTest.exe live
  3. IE window for www.google.com - BrowserTest.exe google

But, running BrowserTest.exe ceridian never opens the modal dialog. So, something must be wrong with the code, but what ???

.net
windows
com-interop
shdocvw
shdocvw.internetexplorer
asked on Stack Overflow Mar 7, 2019 by mark • edited Apr 9, 2019 by mark

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0