Our target is: Watin-enabled browser testing embedded in a .net winform.
Currently, we are using a .net WebBrowser control to embed browser behavior in a winform. We
I have just upgraded to newest version of WatiN (head revision - 1166 - in trunk: https://watin.svn.sourceforge.net/svnroot/watin/trunk/src/). Since there was a change in original DialogWatcher class it is now possible to use existing DialogWatcher with fewer code.
Create class:
public class WebBrowserIE : IE
{
private IntPtr hwnd;
public WebBrowserIE(WebBrowser webBrowserControl)
: base(webBrowserControl.ActiveXInstance, false)
{
hwnd = webBrowserControl.FindForm().Handle;
StartDialogWatcher();
}
public override IntPtr hWnd
{
get
{
return hwnd;
}
}
protected override void Dispose(bool disposing)
{
hwnd = IntPtr.Zero;
base.Dispose(disposing);
}
}
Use it instead of original IE class and see disappearing javascript alert dialog:
var ie = new WebBrowserIE(webBrowser1);
var thread = new Thread(() =>
{
ie.GoTo("http://www.plus2net.com/javascript_tutorial/window-alert.php");
ie.Button(Find.ByValue("Click here to display alert message")).Click();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Warning: Create WebBrowserIE instance outside the thread. Otherwise you will have to modify this class to avoid cross-thread operation when reading Handle property of Form.