2010-11-19 145 views
2

我正在尝试WatiN为我们的UI测试,我可以得到测试工作,但我不能让IE关闭后。问题附加到IE浏览器

我试图关闭IE中的类清理代码,使用WatiN的示例IEStaticInstanceHelper technique

这个问题似乎被附接至所述IE线程,超时:(_ieHwnd是手柄到IE当IE第一次启动存储)

_instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd)); 

这给出了错误:

Class Cleanup method Class1.MyClassCleanup failed. Error Message: WatiN.Core.Exceptions.BrowserNotFoundException: Could not find an IE window matching constraint: Attribute 'hwnd' equals '1576084'. Search expired after '30' seconds.. Stack Trace: at WatiN.Core.Native.InternetExplorer.AttachToIeHelper.Find(Constraint findBy, Int32 timeout, Boolean waitForComplete)

我相信我一定会错过一些明显的东西,有没有人对此有任何想法? 感谢

为了完整起见,静态辅助看起来是这样的:

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

回答

0

通过转储mstest和使用mbunit来解决这个问题。我也发现,我不需要使用任何IEStaticInstanceHelper的东西,它只是工作。

+0

我遇到了同样的问题,但我既没有使用MSTest也没有使用MbUnit。 您的环境中是否有其他可能解决此问题的更改? – 2010-12-01 01:23:06

+0

我认为这取决于测试执行线程的方式,它描述了各种设置; http://watin.sourceforge.net/apartmentstateinfo.html 我的方式是通过FixtureSetUp导航到页面执行操作等,然后关闭FixtureTearDown中的浏览器。 WatiN在其他配置上表现不佳。 – 2011-01-07 14:07:31

0

默认情况下,当IE对象被破坏,它们自动关闭的浏览器。

您的CleanUp代码可能会尝试查找已关闭的浏览器,这就是为什么您有错误。

+0

感谢您的回答,但不幸的是我的问题是浏览器保持打开 – 2010-11-20 15:53:59

1

问题是,当MSTEST使用[ClassCleanup]属性执行方法时,它将在不属于STA的一部分的线程上运行。

如果您运行下面的代码它应该工作:

[ClassCleanup] 
public static void MyClassCleanup() 
{ 
    var thread = new Thread(() => 
    { 
     _staticBrowser.Instance.Close(); 
     _staticBrowser = null; 
    }); 

    thread.SetApartmentState(ApartmentState.STA); 
    thread.Start(); 
    thread.Join(); 
} 

的华廷网站简要地提到,华廷不会不会在STA here使用线程,但它并不明显,[TestMethod]的运行在STA中,而像[ClassCleanup][AssemblyCleanupAttribute]这样的方法则没有。