当尝试在 Visual Studio 2008 中运行一个非常简单的 WatiN 2.0 (CTP3) 测试时,我发现第一个测试总是执行得很好。第二个测试方法似乎破坏了 IE 对象中的某些内容,产生以下异常:
测试方法
testProject.WatinTest.testTwo 抛出
例外:
System.Runtime.InteropServices.InvalidComObjectException:
已分离的COM对象
从其底层 RCW 不能
用过的..
示例代码如下。由于初始化方法在 VS2008 中的工作方式,浏览器变量必须定义为static
我认为这可能是问题的关键。不幸的是,除非以通用方法打开浏览器,否则这意味着每个测试都有一个单独的窗口,这并不理想
如果有任何关于如何解决这个问题的想法,我将非常感激。
Google 搜索和 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"));
}
我以前听说过这个问题,并打算对此进行一段时间的调查。现在 WatiN 2.0 beta 1 已经可用,我坐下来创建了一个帮助器类来使用 Visual Studios 测试运行器解决这个问题。继助手类和修改后的测试类之后。我也blogged关于此解决方案,以使其获得更多曝光。
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"));
}
}
哈特哈,
杰罗恩
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)