Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 15, 2012 16:36
Show Gist options
  • Save davybrion/3728740 to your computer and use it in GitHub Desktop.
Save davybrion/3728740 to your computer and use it in GitHub Desktop.
code snippets for "Sharing An IE Instance Among Multiple Tests With WatiN And MS Test" post
public static class IEStaticInstanceHelper
{
// TODO: move this to a config file
public const string ROOT_URL = "http://localhost:13834/";
private static IE _ie;
private static int _previouslyKnownIeThreadHashCode;
private static string _ieHwnd;
public static void Initialize()
{
IE = new IE(ROOT_URL);
}
public static IE IE
{
get
{
if (GetCurrentThreadHashCode() != _previouslyKnownIeThreadHashCode)
{
_ie = Browser.AttachTo<IE>(Find.By("hwnd", _ieHwnd));
_previouslyKnownIeThreadHashCode = GetCurrentThreadHashCode();
}
return _ie;
}
private set
{
_ie = value;
_ieHwnd = _ie.hWnd.ToString();
_previouslyKnownIeThreadHashCode = GetCurrentThreadHashCode();
}
}
private static int GetCurrentThreadHashCode()
{
return Thread.CurrentThread.GetHashCode();
}
}
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext testContext)
{
IEStaticInstanceHelper.Initialize();
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
if (IEStaticInstanceHelper.IE != null)
{
IEStaticInstanceHelper.IE.Close();
IEStaticInstanceHelper.IE.Dispose();
}
}
_ie = Browser.AttachTo<IE>(Find.By("hwnd", _ieHwnd));
[AssemblyCleanup]
public static void AssemblyCleanup()
{
var thread = new Thread(() =>
{
if (IEStaticInstanceHelper.IE != null)
{
IEStaticInstanceHelper.IE.Close();
IEStaticInstanceHelper.IE.Dispose();
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment