views:

62

answers:

2

I've scoured the internet (including answers from this site) but nothing appears to work for me. Does anyone know the correct approach to attach WatiN to a WebBrowser instance (Windows Presentation Forms version)?

Most of the answers I've read tell me about 'ActiveXInstance', which is not visible in the WPF version of WebBrowser. I've tried exposing the IWebBrowser 2 interface from the WebBrowser documentation (http://msdn.microsoft.com/en-us/library/cc491073%28v=VS.90%29.aspx) but after much reworking of the code structure to get it to compile, a simple WatiN goto(URL statement simply ends up timing out.

As a last resort I've tried

WatiN.Core.Settings.AutoStartDialogWatcher = false;
var browser = new IE(wbrowser);

On the loadcompleted event (as the browser instance will result in null if I place it in the MainWindow() constructor method), but that brings up an Argument Exception with the message "iwebBrowser2 needs to implement shdocvw.IWebBrowser2". Unfortunately I've no idea how to resolve this.

A: 

In the WatiN_IE_ExtensionMethods.cs API I wrote for O2, I was able to get it to work quite nicely with the normal WinForms webbrowser (inside another WinForm control or an WPF control).

Since you can use WinForm controls inside WPF (and that is exactly what .NET 3.5 is doing with the WPF WebBrowser since it is not a native WPF control), here is an example that: does exactly that Using WatiN to inside WPF.

This video shows an O2 script that uses this technique: http://www.youtube.com/watch?v=YsVX5-nGHWI

Note that I wrote a bunch of Extension methods to make WatiN easier to consume:

var ie = winFormsPanel.add_IE();
ie.open("http://www.google.com");       
ie.link("Videos").flash().click();
ie.field("q").value("OWASP O2 Platform").flash();
ie.button("Search Videos").flash().click();
ie.link("O2 Platform - XSS PoC builder.avi").scrollIntoView().flash().click();
Dinis Cruz
A: 

It's likely that you'll want to attach by window handle:

var ie = IE.AttachToIE(Find.By("hwnd", containerHwnd);
Ian P