Question

I am experimenting with WatiN for our UI testing, I can get tests to work, but I can't get IE to close afterwards.

I'm trying to close IE in my class clean up code, using WatiN's example IEStaticInstanceHelper technique.

The problem seems to be attaching to the IE thread, which times out:

_instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));

(_ieHwnd is the handle to IE stored when IE is first launched.)

This gives the error:

Class Cleanup method Class1.MyClassCleanup failed. Error Message: WatiN.Core.Exceptions.BrowserNotFoundException: Could not find an IE window matching constraint: Attribute 'hwnd' equals '1576084'. Search expired after '30' seconds.. Stack Trace: at WatiN.Core.Native.InternetExplorer.AttachToIeHelper.Find(Constraint findBy, Int32 timeout, Boolean waitForComplete)

I'm sure I must be missing something obvious, has anyone got any ideas about this one? Thanks

For completeness, the static helper looks like this:

public class StaticBrowser
{
    private IE _instance;
    private int _ieThread;
    private string _ieHwnd;

    public IE Instance
    {
        get
        {
            var currentThreadId = GetCurrentThreadId();
            if (currentThreadId != _ieThread)
            {
                _instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));
                _ieThread = currentThreadId;
            }
            return _instance;
        }
        set
        {
            _instance = value;
            _ieHwnd = _instance.hWnd.ToString();
            _ieThread = GetCurrentThreadId();
        }
    }

private int GetCurrentThreadId()
{
    return Thread.CurrentThread.GetHashCode();
}
    }

And the clean up code looks like this:

private static StaticBrowser _staticBrowser;

[ClassCleanup]
public static void MyClassCleanup()
{
    _staticBrowser.Instance.Close();
    _staticBrowser = null;
}
Was it helpful?

Solution 3

Fixed this myself by dumping mstest and using mbunit instead. I also found that I didn't need to use any of the IEStaticInstanceHelper stuff either, it just worked.

OTHER TIPS

The problem is that when MSTEST executes the method with the [ClassCleanup] attribute, it will be run on a thread that isn't part of the STA.

If you run the following code it should work:

[ClassCleanup]
public static void MyClassCleanup()
{
    var thread = new Thread(() =>
    {
        _staticBrowser.Instance.Close();
        _staticBrowser = null;
     });

    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

The WatiN website briefly mentions that WatiN won't work with threads not in the STA here but it isn't obvious that [TestMethod]'s run in the STA while methods like [ClassCleanup] and [AssemblyCleanupAttribute] do not.

By default when IE object are destroyed, they autoclose the browser.

Your CleanUp code may try to find a browser already close, which why you have an error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top