watin attachto Embedded browser
I have a c# windows form project using watin.
I would love to attach to the web-browser control on a form, is this possible? Is there any sample code on how开发者_Go百科 to attach to the embedded web-browser control.
//Looks like this only works for actual IExplorer instances.
window = WatiN.Core.Browser.AttachTo<WatiN.Core.IE>(WatiN.Core.Find.ByTitle("Google"));
Perhaps i'm missing something here, but can you not use a WebBrowser
control and attach it to the WinForms application?
For me it gave the error.
WatiN.Core.Exceptions.TimeoutException was unhandled Message="Timeout while Internet Explorer busy" Source="WatiN.Core"
It is possible to make an embedded webbrowser control to work with WatiN's Dialog-handling mechanism if you are able to get the window handle of the webbrowser control, which should be fully possible if you are writing the code of the form that contains it (1)
The reason you need this window handle is exactly because WatiN fails to retrieve it by means of IWebBrowser2.HWND property - this is to be expected. (2)
As a side-note, I have experienced problematic and erratic behavior by IWebBrowser2.HWND property even when dealing with Microsoft's IE - so one more reason to follow the method outlined below to workaround such HWND issues.
Once you get your hands on the window handle of the webbrowser control you can simply make a proxy class that wraps around the IWebBrowser2 interface of the embedded web-browser (with the exception of HWND) so as to workaround the problem. Putting it all together you can now write:
Settings.AutoStartDialogWatcher = true;
new IE(new ProxyForIWebBrowser2InterfaceToWorkaroundHWNDBugs((IWebBrowser2) webBrowser1.ActiveXInstance, webBrowser1.Handle), true);
Hope this helps. Oh, and here's the proxy class you'll need:
using System;
using SHDocVw;
namespace WatiN.Core.Native.InternetExplorer
{
public class ProxyForIWebBrowser2InterfaceToWorkaroundHWNDBugs : IWebBrowser2
{
#region Implementation of IWebBrowser
private readonly int _ExplicitHwndForIWebBrowser2;
private readonly IWebBrowser2 _EmbeddedWebBrowserAsIWebBrowser2;
public ProxyForIWebBrowser2InterfaceToWorkaroundHWNDBugs(IWebBrowser2 embeddedWebBrowserAsIWebBrowser2, IntPtr explicitHwnd)
{
_EmbeddedWebBrowserAsIWebBrowser2 = embeddedWebBrowserAsIWebBrowser2;
_ExplicitHwndForIWebBrowser2 = (int)explicitHwnd;
}
void IWebBrowser.GoBack()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoBack();
}
void IWebBrowser2.GoForward()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoForward();
}
void IWebBrowser2.GoHome()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoHome();
}
void IWebBrowser2.GoSearch()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoSearch();
}
void IWebBrowser2.Navigate(string URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers)
{
_EmbeddedWebBrowserAsIWebBrowser2.Navigate(URL, ref Flags, ref TargetFrameName, ref PostData, ref Headers);
}
void IWebBrowser2.Refresh()
{
_EmbeddedWebBrowserAsIWebBrowser2.Refresh();
}
void IWebBrowser2.Refresh2(ref object Level)
{
_EmbeddedWebBrowserAsIWebBrowser2.Refresh2(ref Level);
}
void IWebBrowser2.Stop()
{
_EmbeddedWebBrowserAsIWebBrowser2.Stop();
}
object IWebBrowser2.Application
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Application; }
}
object IWebBrowser2.Parent
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Parent; }
}
object IWebBrowser2.Container
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Container; }
}
object IWebBrowser2.Document
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Document; }
}
bool IWebBrowser2.TopLevelContainer
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.TopLevelContainer; }
}
string IWebBrowser2.Type
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Type; }
}
int IWebBrowser2.Left
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Left; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Left = value; }
}
int IWebBrowser2.Top
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Top; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Top = value; }
}
int IWebBrowser2.Width
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Width; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Width = value; }
}
int IWebBrowser2.Height
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Height; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Height = value; }
}
void IWebBrowser2.Quit()
{
_EmbeddedWebBrowserAsIWebBrowser2.Quit();
}
void IWebBrowser2.ClientToWindow(ref int pcx, ref int pcy)
{
_EmbeddedWebBrowserAsIWebBrowser2.ClientToWindow(ref pcx, ref pcy);
}
void IWebBrowser2.PutProperty(string Property, object vtValue)
{
_EmbeddedWebBrowserAsIWebBrowser2.PutProperty(Property, vtValue);
}
object IWebBrowser2.GetProperty(string Property)
{
return _EmbeddedWebBrowserAsIWebBrowser2.GetProperty(Property);
}
public void Navigate2(ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers)
{
_EmbeddedWebBrowserAsIWebBrowser2.Navigate2(ref URL, ref Flags, ref TargetFrameName, ref PostData, ref Headers);
}
public OLECMDF QueryStatusWB(OLECMDID cmdID)
{
return _EmbeddedWebBrowserAsIWebBrowser2.QueryStatusWB(cmdID);
}
public void ExecWB(OLECMDID cmdID, OLECMDEXECOPT cmdexecopt, ref object pvaIn, ref object pvaOut)
{
_EmbeddedWebBrowserAsIWebBrowser2.ExecWB(cmdID, cmdexecopt, ref pvaIn, ref pvaOut);
}
public void ShowBrowserBar(ref object pvaClsid, ref object pvarShow, ref object pvarSize)
{
_EmbeddedWebBrowserAsIWebBrowser2.ShowBrowserBar(ref pvaClsid, ref pvarShow, ref pvarSize);
}
void IWebBrowser2.GoBack()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoBack();
}
void IWebBrowserApp.GoForward()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoForward();
}
void IWebBrowserApp.GoHome()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoHome();
}
void IWebBrowserApp.GoSearch()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoSearch();
}
void IWebBrowserApp.Navigate(string URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers)
{
_EmbeddedWebBrowserAsIWebBrowser2.Navigate(URL, ref Flags, ref TargetFrameName, ref PostData, ref Headers);
}
void IWebBrowserApp.Refresh()
{
_EmbeddedWebBrowserAsIWebBrowser2.Refresh();
}
void IWebBrowserApp.Refresh2(ref object Level)
{
_EmbeddedWebBrowserAsIWebBrowser2.Refresh2(ref Level);
}
void IWebBrowserApp.Stop()
{
_EmbeddedWebBrowserAsIWebBrowser2.Stop();
}
object IWebBrowserApp.Application
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Application; }
}
object IWebBrowserApp.Parent
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Parent; }
}
object IWebBrowserApp.Container
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Container; }
}
object IWebBrowserApp.Document
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Document; }
}
bool IWebBrowserApp.TopLevelContainer
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.TopLevelContainer; }
}
string IWebBrowserApp.Type
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Type; }
}
int IWebBrowserApp.Left
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Left; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Left = value; }
}
int IWebBrowserApp.Top
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Top; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Top = value; }
}
int IWebBrowserApp.Width
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Width; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Width = value; }
}
int IWebBrowserApp.Height
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Height; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Height = value; }
}
void IWebBrowserApp.Quit()
{
_EmbeddedWebBrowserAsIWebBrowser2.Quit();
}
void IWebBrowserApp.ClientToWindow(ref int pcx, ref int pcy)
{
_EmbeddedWebBrowserAsIWebBrowser2.ClientToWindow(ref pcx, ref pcy);
}
void IWebBrowserApp.PutProperty(string Property, object vtValue)
{
_EmbeddedWebBrowserAsIWebBrowser2.PutProperty(Property, vtValue);
}
object IWebBrowserApp.GetProperty(string Property)
{
return _EmbeddedWebBrowserAsIWebBrowser2.GetProperty(Property);
}
void IWebBrowserApp.GoBack()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoBack();
}
void IWebBrowser.GoForward()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoForward();
}
void IWebBrowser.GoHome()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoHome();
}
void IWebBrowser.GoSearch()
{
_EmbeddedWebBrowserAsIWebBrowser2.GoSearch();
}
void IWebBrowser.Navigate(string URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers)
{
_EmbeddedWebBrowserAsIWebBrowser2.Navigate(URL, ref Flags, ref TargetFrameName, ref PostData, ref Headers);
}
void IWebBrowser.Refresh()
{
_EmbeddedWebBrowserAsIWebBrowser2.Refresh();
}
void IWebBrowser.Refresh2(ref object Level)
{
_EmbeddedWebBrowserAsIWebBrowser2.Refresh2(ref Level);
}
void IWebBrowser.Stop()
{
_EmbeddedWebBrowserAsIWebBrowser2.Stop();
}
object IWebBrowser.Application
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Application; }
}
object IWebBrowser.Parent
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Parent; }
}
object IWebBrowser.Container
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Container; }
}
object IWebBrowser.Document
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Document; }
}
bool IWebBrowser.TopLevelContainer
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.TopLevelContainer; }
}
string IWebBrowser.Type
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Type; }
}
int IWebBrowser.Left
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Left; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Left = value; }
}
int IWebBrowser.Top
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Top; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Top = value; }
}
int IWebBrowser.Width
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Width; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Width = value; }
}
int IWebBrowser.Height
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Height; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Height = value; }
}
string IWebBrowser.LocationName
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.LocationName; }
}
string IWebBrowser2.LocationURL
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.LocationURL; }
}
bool IWebBrowser2.Busy
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Busy; }
}
string IWebBrowser2.Name
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Name; }
}
int IWebBrowser2.HWND
{
get { return _ExplicitHwndForIWebBrowser2; }
}
string IWebBrowser2.FullName
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.FullName; }
}
string IWebBrowser2.Path
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Path; }
}
bool IWebBrowser2.Visible
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Visible; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Visible = value; }
}
bool IWebBrowser2.StatusBar
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.StatusBar; }
set { _EmbeddedWebBrowserAsIWebBrowser2.StatusBar = value; }
}
string IWebBrowser2.StatusText
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.StatusText; }
set { _EmbeddedWebBrowserAsIWebBrowser2.StatusText = value; }
}
int IWebBrowser2.ToolBar
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.ToolBar; }
set { _EmbeddedWebBrowserAsIWebBrowser2.ToolBar = value; }
}
bool IWebBrowser2.MenuBar
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.MenuBar; }
set { _EmbeddedWebBrowserAsIWebBrowser2.MenuBar = value; }
}
bool IWebBrowser2.FullScreen
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.FullScreen; }
set { _EmbeddedWebBrowserAsIWebBrowser2.FullScreen = value; }
}
public tagREADYSTATE ReadyState
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.ReadyState; }
}
public bool Offline
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Offline; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Offline = value; }
}
public bool Silent
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Silent; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Silent = value; }
}
public bool RegisterAsBrowser
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.RegisterAsBrowser; }
set { _EmbeddedWebBrowserAsIWebBrowser2.RegisterAsBrowser = value; }
}
public bool RegisterAsDropTarget
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.RegisterAsDropTarget; }
set { _EmbeddedWebBrowserAsIWebBrowser2.RegisterAsDropTarget = value; }
}
public bool TheaterMode
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.TheaterMode; }
set { _EmbeddedWebBrowserAsIWebBrowser2.TheaterMode = value; }
}
public bool AddressBar
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.AddressBar; }
set { _EmbeddedWebBrowserAsIWebBrowser2.AddressBar = value; }
}
public bool Resizable
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Resizable; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Resizable = value; }
}
string IWebBrowser2.LocationName
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.LocationName; }
}
string IWebBrowserApp.LocationURL
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.LocationURL; }
}
bool IWebBrowserApp.Busy
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Busy; }
}
string IWebBrowserApp.Name
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Name; }
}
int IWebBrowserApp.HWND
{
get { return _ExplicitHwndForIWebBrowser2; }
}
string IWebBrowserApp.FullName
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.FullName; }
}
string IWebBrowserApp.Path
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Path; }
}
bool IWebBrowserApp.Visible
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Visible; }
set { _EmbeddedWebBrowserAsIWebBrowser2.Visible = value; }
}
bool IWebBrowserApp.StatusBar
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.StatusBar; }
set { _EmbeddedWebBrowserAsIWebBrowser2.StatusBar = value; }
}
string IWebBrowserApp.StatusText
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.StatusText; }
set { _EmbeddedWebBrowserAsIWebBrowser2.StatusText = value; }
}
int IWebBrowserApp.ToolBar
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.ToolBar; }
set { _EmbeddedWebBrowserAsIWebBrowser2.ToolBar = value; }
}
bool IWebBrowserApp.MenuBar
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.MenuBar; }
set { _EmbeddedWebBrowserAsIWebBrowser2.MenuBar = value; }
}
bool IWebBrowserApp.FullScreen
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.FullScreen; }
set { _EmbeddedWebBrowserAsIWebBrowser2.FullScreen = value; }
}
string IWebBrowserApp.LocationName
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.LocationName; }
}
string IWebBrowser.LocationURL
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.LocationURL; }
}
bool IWebBrowser.Busy
{
get { return _EmbeddedWebBrowserAsIWebBrowser2.Busy; }
}
#endregion
}
}
1: Even if you don't you have source-code access to such a form because, say, webbrowser control is in a 3rd party app that you have no access to, you can write a small utility function to scan all windows till you stumble upon the 3rd-party app that contains the webbrowser control you are after. You can have a look in WatiN's source code, inside ShellWindows2.CollectInternetExplorerInstances() to get an idea on how you can implement such a scan-function.
2: http://support.microsoft.com/kb/244310
Yes, you can use a WebBrowser control, but there are tricks.
The key point is that because your application is providing the dialogs (not IE), you must set the Settings object property AutoStartDialogWatcher to false. Otherwise, you will fail on attachment because the dialog watcher window handle is invalid.
I would suggest using something like: IE newbrowser = new WatiN.Core.IE(embeddedbrowser.IWebBrowser2);
...of course the IWebBrowser2 property name will change depending on the library you use.
This is code I used to attach to the Embeded Web Browser Control:
WatiN.Core.IE window = null;
WatiN.Core.Settings.AutoStartDialogWatcher = false;
window = new WatiN.Core.IE(webBrowser1.ActiveXInstance);
精彩评论