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 ???

mark
  • 59,016
  • 79
  • 296
  • 580
  • Do you have a reproducing project? – Simon Mourier Mar 08 '19 at 08:05
  • I mean, the code works fine when you target a standard url that does contain a document, for example https://www.google.com, so the problem is probably the fact there is no document in your case, just a window – Simon Mourier Mar 08 '19 at 08:28
  • Try `ceridian.com`. It does not work. – mark Mar 08 '19 at 19:39
  • It works fine for me, but have you noticed OnDocumentComplete is called many times (so the code crashes because the task is now completed again). Speaking about your first code, I see you've changed it now ... – Simon Mourier Mar 09 '19 at 08:14
  • And the new code works fine for me. Note however it's not always the same url I get, it can be www.ceridian.com or some analytics stuff like `https://vars.hotjar.com/box-d831eecf6f5411af024c3acd759add17.html` so if your code waits for a specific url you'll have to modify it – Simon Mourier Mar 09 '19 at 08:34
  • Hmm, this is very curious. The code definitely does not work for us. – mark Mar 09 '19 at 13:14
  • I am running as part of an NUnit test from the Test Explorer in VS 2017. Maybe I need to try it as a simple exe. – mark Mar 09 '19 at 13:16
  • I'm doing that too. Here are my .cs and .csproj concatenated https://pastebin.com/raw/vzBGHk2m – Simon Mourier Mar 09 '19 at 13:57
  • I will test them as soon as I can. – mark Mar 19 '19 at 04:35
  • How exactly have you generated interop.ShDocVw.dll? – mark Mar 19 '19 at 19:13
  • I just used Visual Studio hint (Quick Action) on the InternetExplorer class name. – Simon Mourier Mar 19 '19 at 22:16
  • The example definitely does not work for us. What did work is to open a Windows.Forms form with the WebBrowser control in it. That one does send the events, but not the native browser. – mark Mar 22 '19 at 14:52
  • Not sure what you mean by WebBrowser vs native browser (WebBrowser is just a pre-built wrapper over IE, so also using ShDocVw, etc.). Do you have a small reproducing project? – Simon Mourier Mar 22 '19 at 16:36
  • I gave a link to the source code of a small fully functional example that shows off the problem. – mark Apr 09 '19 at 17:26
  • I don't reproduce the problem. BrowserTest.exe cedirian does the same as others. I've taken a screenshot so you believe me :-) https://imgur.com/a/Pi8dfm3 I guess, there's something special with your local installation. – Simon Mourier Apr 09 '19 at 17:39
  • I am at loss here. – mark Apr 09 '19 at 17:44

0 Answers0