首页 > 解决方案 > 如何在 C# 单元测试中等待 SHDocVw InternetExplorerClass.DocumentComplete 事件?

问题描述

我有一个单元测试代码,需要在 IE 中打开一个页面,并在文档完成后做一些事情。该页面包含重定向并在最后加载 Silverlight(我们又坚持了一年)。

这是代码:

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}");
        }
    }
}

但永远ieEvent.Url存在。另外,如果我在等待循环结束的同时null尝试在某个时候访问,我会得到以下信息:ie.Busy

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

我究竟做错了什么?

编辑 1

我在这里有一个功能齐全的项目 - https://dev.azure.com/MarkKharitonov0271/_git/BrowserTest

现在,一切正常:

  1. www.ceridian.com的 WebBrowser 控件-BrowserTest.exe
  2. www.live.com的 IE 窗口-BrowserTest.exe live
  3. www.google.com的 IE 窗口-BrowserTest.exe google

但是,运行BrowserTest.exe ceridian永远不会打开模式对话框。所以,代码一定有问题,但是什么???

标签: .netwindowscom-interopshdocvwshdocvw.internetexplorer

解决方案


推荐阅读