.net - 如何在 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
- 当不带任何参数运行时,它会打开 WebBrowser Windows 窗体控件,导航到 www.ceridian.com 并在ceridian
DocumentComplete
事件到达时打开一个模式对话框。关闭对话框结束应用程序。 - 当使用单个命令行参数运行时,比如X,它使用 InternetExplorer COM 对象打开 IE 浏览器,导航到http://www.X.com并在X
DocumentComplete
事件到达时打开一个模式对话框。关闭对话框结束应用程序。
现在,一切正常:
- www.ceridian.com的 WebBrowser 控件-
BrowserTest.exe
- www.live.com的 IE 窗口-
BrowserTest.exe live
- www.google.com的 IE 窗口-
BrowserTest.exe google
但是,运行BrowserTest.exe ceridian
永远不会打开模式对话框。所以,代码一定有问题,但是什么???
解决方案
推荐阅读
- bash - BASH:我的变量值在循环内未正确显示
- bash - 使用 unicode 字符处理 macos 问题
- php - PHP Geotarget重定向在wordpress站点中不起作用
- authentication - 带有 netcat 源的 Apache Flume 需要发送身份验证
- c# - 一个视图中的 MVC 枚举和模型 - 错误:“是一种类型,在给定的上下文中无效”
- reactjs - 单击当前组件以外的按钮时显示警告模式
- node.js - npm 脚本无法使用 node_modules.bin 目录中的可执行文件
- python - Selenium Python - 如何在动态网站中查找元素(获取 NoSuchElementException 异常)
- python - 如何更改数据框中的字符?
- c++ - 如果我已经在这个 C++ 片段中声明了大小并初始化了数组,那么这段代码是如何运行的?