MSTest中的WatiN - ClassCleanup失败

时间:2010-03-18 09:49:56

标签: c# mstest watin

在帖子WatiN in Visual Studio 2008 - second test method fails中 IEStaticInstanceHelper(原始答案Reusing an IE instance in VS testsources)有一个很好的解决方案,但是当ClassCleanup在AttachToIE上触发失败时。结果IEXPLORAR仍在运行。

有什么问题?

当然可以像这样杀死这个过程:

// Id of IEXPLORAR
_ie.ProcessID

Process.GetProcessById(_processId).Kill();
this._ie = null;

但我真的不喜欢这种方式......

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

它失败了,因为MSTest在多线程公寓中进行类清理,甚至认为它在STA中运行单独的测试。 WaitN附加到IE的方式涉及查找非线程安全的COM对象,并且不暴露给MTA。

感谢您使用{@ 1}}而不是CloseMainWindow()

答案 1 :(得分:0)

您可以使用带有WatiN的AttachTo方法的通配符来获取现有的浏览器实例。这将允许您在后续测试中重新使用浏览器实例,或者如果您感兴趣的话,请关闭浏览器。例如:

// find first browser matching our wildcard
IE found = Browser.AttachTo<IE>(Find.ByTitle(new Regex(".*")));

// then close just that one
found.Close();

// or close all running IE instances at once
// found.ForceClose();

您可以使用具有相同“查找约束”的Exists方法确定是否要附加IE实例。例如:

Constraint browserWildcard = Find.ByTitle(new new Regex(".*"));
if(IE.Exists<IE>(browserWildcard))
{
  // ...
}