vrijdag 15 januari 2010

Browser.AttachTo<T> and IAttachTo

In WatiN RC1 the functionality to attach to a browser instance has had a complete redo. This had two reasons:

  1. Being able to attach to a browser in a browser agnostic way.
  2. Being able to return your custom browser type.

Download the example code here

From the old way to a browser agnostic way

If you don’t intend to run your tests with different browsers, you still need to make a small change in your code when upgrading to WatiN RC1.


needs to be changed into


which is the same as


and the same as


As you can see in the last example, you can pass in a type which should inherit WatiN.Core.Browser. The following example shows you how to utilize this method in situations where you get a browser instance injected into your test code. In the example the link that is clicked on will open up a new browser instance and we want to verify the text in this newly opened browser instance.


Thanks to this new AttachTo(type, findBy) method there is no knowledge at all of the used browser type inside my test code.

Returning a custom Browser type from AttachTo()

Many of us who use WatiN will probably use their own custom super duper version of WatiN.Core.IE. Providing new or different behavior then the default IE implementation does. See here for example  (and yes this part of the post was also a little bit sparked by their need to write their own AttachTo methods).

Here is my great MyIE class which overrides the ToString method and returns the Title and Url of the shown page (it’ll surf just as an example).


And now I want to be able write


When I run this piece of code, WatiN will throw this exception:

No AttachToHelper registered for type WatiN.Examples.Tests.MyIE.

That does sound like Ican register an AttachToHelper for a browser Type, doesn’t it. The Browser class provides a method RegisterAttachToHelper doing just that.

So we need to create our own AttachToMyIEHelper class. Since MyIE is inheriting IE, AttachToMyIEHelper will inherit AttachToIEHelper. And then it is just a matter of overriding the CreateBrowserInstance method, return a new instance of MyIE and we are done. Here is the implementation:


And this code shows you how to register the new AttachToMyIEHelper and use it in your tests.


Enjoy testing with WatiN.

Technorati Tags:

5 reacties:

Anoniem zei

I tried adopting this text into your StaticInstanceHelper pattern. Unfortunately during the ClassCleanup method the AttachTo call times out and never finds the existing IE instance. Any ideas why this might be?

Jeroen zei

Hi forrest,

It indeed times out. I'll have a look.

Thanks for reporting,

Nathan zei

I get something similar when trying to attach to an existing IE instance. Basically i am trying to check if they have IE open already, and if they do use it, if not open new instance, but i always get a timout exception no matter what value i change the Settings.AttachToBrowserTimeOut to.

Wyen zei

Ur topic is cool in attaching the IE.
How can i attach a wizard?(it is enhanced form of dialog and contain several controls like text boxes,button, etc) hosted in IE8. WATIN does not grab it using HTMLDialog functionality

Wyen zei

i need ur help

I am in a situation that when i click on a node ,a Wizard appear. That wizard contain several controls(checkboxes,buttons and TextFields) and it has many screens(when next button is pressed , the next screen appear) but i am unable to handle it. The wizard contains update panel and all the controls appear in that panel. When user click next button other control appear which also exists in that update panel. I have apply the following techniques but failed

1. I have used ie.HtmlDialog but exception appear
2. I have grab the internal iframe (which is the main frame) but its HTMLDocument contains nothing. Where as i contain HTML when i look into it using IE8 Developer Tool
3. I have tried to use mshtml.HTMLDocuemntClass but failed
4. Wizard has url but unable to grab it by url

Unable to grab it by using ie.Form

Actually WatiN does not realise that the page has actually refreshed and will therefore not be able to find some of the elements it is looking for.

Please HELP !!! I am stuck here. Need ur help urgently