将 WatiN 附加到 IE 时出现问题
-
26-09-2019 - |
题
我正在尝试使用 WatiN 进行 UI 测试,我可以让测试正常工作,但之后无法关闭 IE。
我正在尝试使用 WatiN 的示例在类清理代码中关闭 IE IEStaticInstanceHelper 技术.
问题似乎出在 IE 线程上,该线程超时:
_instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));
(_ieHwnd 是 IE 首次启动时存储的 IE 句柄。)
这给出了错误:
类清洁方法class1.myclasscleanup失败。错误信息:WatiN.Core.Exceptions.BrowserNotFoundException:找不到IE窗口匹配的约束:属性“ hwnd”等于'1576084'。搜索在“ 30”秒后过期。堆栈跟踪:在watin.core.native.internetexplorer.attachtoiehelper.find上
我确信我一定错过了一些明显的东西,有人对此有任何想法吗?谢谢
为了完整起见,静态助手如下所示:
public class StaticBrowser
{
private IE _instance;
private int _ieThread;
private string _ieHwnd;
public IE Instance
{
get
{
var currentThreadId = GetCurrentThreadId();
if (currentThreadId != _ieThread)
{
_instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));
_ieThread = currentThreadId;
}
return _instance;
}
set
{
_instance = value;
_ieHwnd = _instance.hWnd.ToString();
_ieThread = GetCurrentThreadId();
}
}
private int GetCurrentThreadId()
{
return Thread.CurrentThread.GetHashCode();
}
}
清理代码如下所示:
private static StaticBrowser _staticBrowser;
[ClassCleanup]
public static void MyClassCleanup()
{
_staticBrowser.Instance.Close();
_staticBrowser = null;
}
解决方案 3
此倾倒MSTEST并使用MbUnit的代替固定自己。我还发现,我并不需要可以使用任何IEStaticInstanceHelper东西,它只是工作。
其他提示
问题是当 MSTEST 执行该方法时 [ClassCleanup]
属性,它将在不属于该属性的线程上运行 斯塔.
如果您运行以下代码,它应该可以工作:
[ClassCleanup]
public static void MyClassCleanup()
{
var thread = new Thread(() =>
{
_staticBrowser.Instance.Close();
_staticBrowser = null;
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
WatiN 网站简要提到 WatiN 无法与不在 STA 中的线程一起工作 这里 但并不明显的是 [TestMethod]
在 STA 中运行,而方法如下 [ClassCleanup]
和 [AssemblyCleanupAttribute]
不要。
通过当IE对象被破坏默认情况下,它们自动关闭浏览器。
您清理代码可能会尝试找到一个浏览器已经接近,这为什么你有一个错误。