Created
September 15, 2012 16:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AssemblyInitialize] | |
public static void AssemblyInitialize(TestContext testContext) | |
{ | |
IEStaticInstanceHelper.Initialize(); | |
} | |
[AssemblyCleanup] | |
public static void AssemblyCleanup() | |
{ | |
if (IEStaticInstanceHelper.IE != null) | |
{ | |
IEStaticInstanceHelper.IE.Close(); | |
IEStaticInstanceHelper.IE.Dispose(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ie = Browser.AttachTo<IE>(Find.By("hwnd", _ieHwnd)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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