当试图在Visual Studio 2008中运行一个非常简单的WatiN 2.0(CTP3)测试时,我发现第一个测试总是很好.第二个测试方法似乎破坏IE对象中的某些东西,产生以下异常:
测试方法testProject.WatinTest.testTwo抛出异常:System.Runtime.InteropServices.InvalidComObjectException:已经与其底层RCW分离的COM对象无法使用..
示例代码如下.由于初始化方法在VS2008中的工作方式,浏览器变量必须定义为static
我认为可能是问题的关键.不幸的是,除非在常用方法中打开浏览器,否则它意味着每个测试的单独窗口并不理想
我会非常感谢有关如何解决这个问题的任何想法.谷歌搜索和SO搜索没有产生任何有用的结果,所以我希望这个问题的一个好的答案将有助于社区.非常感谢,
private static IE ie [ClassInitialize] public static void testInit(TestContext testContext) { ie = new IE("http://news.bbc.co.uk"); } [TestMethod] public void testOne() { Assert.IsTrue(ie.ContainsText("Low graphics")); } [TestMethod] public void testTwo() { Assert.IsTrue(ie.ContainsText("Low graphics")); }
Jeroen van M.. 10
我之前听过这个问题,并打算对此进行一段时间的调查.既然WatiN 2.0 beta 1可用,我坐下来创建了一个帮助类来解决Visual Studio测试运行器的这个问题.遵循帮助程序类和修改后的测试类.我还在 博客上写了这个解决方案,让它更具曝光度.
public class IEStaticInstanceHelper { private IE _ie; private int _ieThread; private string _ieHwnd; public IE IE { get { var currentThreadId = GetCurrentThreadId(); if (currentThreadId != _ieThread) { _ie = IE.AttachToIE(Find.By("hwnd", _ieHwnd)); _ieThread = currentThreadId; } return _ie; } set { _ie = value; _ieHwnd = _ie.hWnd.ToString(); _ieThread = GetCurrentThreadId(); } } private int GetCurrentThreadId() { return Thread.CurrentThread.GetHashCode(); } }
使用这个帮助器的测试类:
[TestClass] public class UnitTest { private static IEStaticInstanceHelper ieStaticInstanceHelper; [ClassInitialize] public static void testInit(TestContext testContext) { ieStaticInstanceHelper = new IEStaticInstanceHelper(); ieStaticInstanceHelper.IE = new IE("http://news.bbc.co.uk"); } public IE IE { get { return ieStaticInstanceHelper.IE; } set { ieStaticInstanceHelper.IE = value; } } [ClassCleanup] public static void MyClassCleanup() { ieStaticInstanceHelper.IE.Close(); ieStaticInstanceHelper = null; } [TestMethod] public void testOne() { Assert.IsTrue(IE.ContainsText("Low graphics")); } [TestMethod] public void testTwo() { Assert.IsTrue(IE.ContainsText("Low graphics")); } }
HTH,Jeroen
我之前听过这个问题,并打算对此进行一段时间的调查.既然WatiN 2.0 beta 1可用,我坐下来创建了一个帮助类来解决Visual Studio测试运行器的这个问题.遵循帮助程序类和修改后的测试类.我还在 博客上写了这个解决方案,让它更具曝光度.
public class IEStaticInstanceHelper { private IE _ie; private int _ieThread; private string _ieHwnd; public IE IE { get { var currentThreadId = GetCurrentThreadId(); if (currentThreadId != _ieThread) { _ie = IE.AttachToIE(Find.By("hwnd", _ieHwnd)); _ieThread = currentThreadId; } return _ie; } set { _ie = value; _ieHwnd = _ie.hWnd.ToString(); _ieThread = GetCurrentThreadId(); } } private int GetCurrentThreadId() { return Thread.CurrentThread.GetHashCode(); } }
使用这个帮助器的测试类:
[TestClass] public class UnitTest { private static IEStaticInstanceHelper ieStaticInstanceHelper; [ClassInitialize] public static void testInit(TestContext testContext) { ieStaticInstanceHelper = new IEStaticInstanceHelper(); ieStaticInstanceHelper.IE = new IE("http://news.bbc.co.uk"); } public IE IE { get { return ieStaticInstanceHelper.IE; } set { ieStaticInstanceHelper.IE = value; } } [ClassCleanup] public static void MyClassCleanup() { ieStaticInstanceHelper.IE.Close(); ieStaticInstanceHelper = null; } [TestMethod] public void testOne() { Assert.IsTrue(IE.ContainsText("Low graphics")); } [TestMethod] public void testTwo() { Assert.IsTrue(IE.ContainsText("Low graphics")); } }
HTH,Jeroen
我正在寻找同样的事情,jvmenen的答案帮助了我.但是,由于给出了答案,因此对WatiN进行了一些更新,因此我不得不重写辅助类,以便与最新的WatiN版本(目前为2.0.20)保持一致.
WatiN不再包含该IE.AttachToIE
函数,所以我不得不修改它.此外,我还使用泛型制作了助手类,因此任何浏览器类型都可以使用,而不仅仅是IE(我相信现在WatiN支持IE和Firefox,而Chrome也会出现).
所以如果其他人也在寻找这个,这是我的版本IEStaticInstanceHelper
(现在称为StaticBrowserInstanceHelper
):
class StaticBrowserInstanceHelperwhere T : Browser { private Browser _browser; private int _browserThread; private string _browserHwnd; public Browser Browser { get { int currentThreadId = GetCurrentThreadId(); if (currentThreadId != _browserThread) { _browser = Browser.AttachTo (Find.By("hwnd", _browserHwnd)); _browserThread = currentThreadId; } return _browser; } set { _browser = value; _browserHwnd = _browser.hWnd.ToString(); _browserThread = GetCurrentThreadId(); } } private int GetCurrentThreadId() { return Thread.CurrentThread.GetHashCode(); } }
希望这有助于某人:)