WatiN in Visual Studio 2008 - second test method fails

automation, testing, visual-studio, watin

Solution

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

Problem

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

Original source