本文介绍了Visual Studio 2008 中的 WatiN - 第二个测试方法失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 Visual Studio 2008 中运行一个非常简单的 WatiN 2.0 (CTP3) 测试时,我发现第一个总是运行良好.第二种测试方法似乎破坏了 IE 对象中的某些内容,产生了以下异常:

When trying to run a very simple WatiN 2.0 (CTP3) test in Visual Studio 2008 I found that the first one always executes fine. The second test method seem to break something in the IE object producing the following exception:

测试方法testProject.WatinTest.testTwo 扔了例外:System.Runtime.InteropServices.InvalidComObjectException:已分离的 COM 对象从其底层 RCW 不能用过..

示例代码如下.由于初始化方法在 VS2008 中的工作方式,浏览器变量必须定义为 static 我相信这可能是问题的关键.不幸的是,除非浏览器以常用方法打开,否则每个测试都有一个单独的窗口,这并不理想

A sample code is below. Due to the way the initialization method is workin in VS2008 the browser variable has to be defined as static which I believe could be a key to the problem. Unfortunately unless the browser is opened in the common method it means a separate window for every test which is not ideal

如果您有任何关于如何解决该问题的想法,我将不胜感激.谷歌搜索和 SO 搜索没有产生任何有用的结果,所以我希望这个问题的一个好的答案对社区有所帮助.非常感谢,

I would be very grateful for any ideas on how to fix that.Google search and SO search did not produce any useful results so I hope that a good answer to this question will help the community. Many thanks,

    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 测试运行器解决这个问题.继辅助类和改进的测试类之后.我还博客关于这个解决方案给它更多曝光.

I have heard this problem before and meant to investigate this for a while. Now that WatiN 2.0 beta 1 is available I sat down and created a helper class to solve this problem with Visual Studios test runner. Following the helper class and the revamped test class. I also blogged about this solution to give it even more exposure.

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

以及使用这个助手的测试类:

And the test class using this helper:

[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,杰伦

这篇关于Visual Studio 2008 中的 WatiN - 第二个测试方法失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:44
查看更多