代码之家  ›  专栏  ›  技术社区  ›  mark

如何等待SHDocVw IInternetExplorerClass.DocumentComplete文件单元测试中的事件?

  •  0
  • mark  · 技术社区  · 6 年前

    我有一个单元测试代码,需要在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

    • 当在没有任何参数的情况下运行时,它会打开WebBrowser Windows窗体控件,并导航到www.ceridian.com当 DocumentComplete 活动到达目的地 . 关闭对话框结束应用程序。
    • 当使用单个命令行参数运行时,例如 它使用InternetExplorer COM对象打开IE浏览器,导航到 http://www.X.com 文档完成

    现在,所有的工作都很好:

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

    但是,跑步 BrowserTest.exe ceridian

    0 回复  |  直到 6 年前