华廷在Visual Studio 2008 - 第二个测试方法失败 [英] WatiN in Visual Studio 2008 - second test method fails

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

问题描述

当试图在Visual Studio 2008中运行一个非常简单的华廷2.0(CT​​P3)的测试,我发现第一个始终执行罚款。第二个测试方法似乎打破IE的对象的东西产生了以下异常:


  

试验方法
  testProject.WatinTest.testTwo扔
  例外:
  System.Runtime.InteropServices.InvalidComObjectException:
  已被分离的COM对象
  从它的基础RCW不能
  二手..


一个样本code如下。由于方式初始化方法在VS2008干活浏览器变量已被定义为静态我相信这可能是问题的关键。不幸的是,除非在浏览器中的普通的方法把它打开装置,用于每个测试这是不理想的一个单独的窗口

我将是如何解决这个问题的任何想法非常感谢。
谷歌搜索和SO所以我希望,很好地回答了这个问题,将有助于社区搜索没有产生任何有用的结果。非常感谢,


 私有静态IE浏览器即    [ClassInitialize]
    公共静态无效testInit(的TestContext的TestContext)
    {
        IE =新的IE(http://news.bbc.co.uk);
    }    [测试方法]
    公共无效testOne()
    {
        Assert.IsTrue(ie.ContainsText(低图形));
    }    [测试方法]
    公共无效testTwo()
    {
        Assert.IsTrue(ie.ContainsText(低图形));
    }


解决方案

我以前听说过这个问题,并打算调查这一段时间。现在,华廷2.0 Beta 1的可我坐了下来,创造了一个辅助类来解决这个问题视觉工作室测试运行。继辅助类和重组后的测试类。我也博客这个解决方案给它更多的曝光

 公共类IEStaticInstanceHelper
{
    私人IE _ie;
    私人诠释_ieThread;
    私人字符串_ieHwnd;    公共IE IE
    {
        得到
        {
            变种currentThreadId = GetCurrentThreadId();
            如果(currentThreadId!= _ieThread)
            {
                _ie = IE.AttachToIE(Find.By(HWND,_ieHwnd));
                _ieThread = currentThreadId;
            }
            返回_ie;
        }
        组
        {
            _ie =价值;
            _ieHwnd = _ie.hWnd.ToString();
            _ieThread = GetCurrentThreadId();
        }
    }    私人诠释GetCurrentThreadId()
    {
        返回Thread.CurrentThread.GetHash code();
    }
}

和使用这个助手的测试类:

  [识别TestClass]
公共类的UnitTest
{
    私有静态IEStaticInstanceHelper ieStaticInstanceHelper;    [ClassInitialize]
    公共静态无效testInit(的TestContext的TestContext)
    {
        ieStaticInstanceHelper =新IEStaticInstanceHelper();
        ieStaticInstanceHelper.IE =新的IE(http://news.bbc.co.uk);
    }    公共IE IE
    {
        {返回ieStaticInstanceHelper.IE; }
        集合{ieStaticInstanceHelper.IE =价值; }
    }    [ClassCleanup]
    公共静态无效MyClassCleanup()
    {
        ieStaticInstanceHelper.IE.Close();
        ieStaticInstanceHelper = NULL;
    }    [测试方法]
    公共无效testOne()
    {
        Assert.IsTrue(IE.ContainsText(低图形));
    }    [测试方法]
    公共无效testTwo()
    {
        Assert.IsTrue(IE.ContainsText(低图形));
    }
}

HTH,
吉荣

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:

Test method testProject.WatinTest.testTwo threw exception: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used..

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

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

解决方案

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, Jeroen

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆