donderdag 26 maart 2009

Custom elements and controls in WatiN 2.0 beta

Update 3/31/2009: You can download the example code for this blog post here.

Still working on the beta release of WatiN 2.0 (will ship in the next few days), but I felt the need to blog about a new feature in the 2.0 beta. In previous posts I showed you how you could extend WatiN with your own controls:

In the upcoming beta WatiN will offer you these new features:

  1. Add support for native html elements not provided by WatiN.
  2. Add support for your (custom) web controls.

Which make the previous posts obsolete.

Add your own native html element

WatiN doesn’t provide support for all html elements defined by the W3C standards. For instance there is no out-of-the-box support for li tagged elements. But starting with WatiN 2.0 beta 1 you can “add” support for linked lists yourself.

First lets show you how you could make use of your own ListItem element.

ListItem list = browser.ElementOfType<ListItem >(Find.ById(“theElementId”));

(And yes, this syntax also works with the out-of-the-box elements like TextField etc..)

Now we know how we could use a none native ListItem element in our (test) code, lets create this ListItem class.

  1. First create a new class called ListItem and make it inherit WatiN.Core.Element.
  2. Add the ElementTagAttribute to your class to tell WatiN which tagName(s) your element class can handle. Incase of ListItem: [ElementTag(“li”)]
  3. Add whatever properties and methods are needed to make this element work for you.
  4. The last thing you need to do is register ListItem with WatiN’s ElementFactory before you can use it in your (test) code:

ElementFactory.RegisterElementType(typeof(ListItem ));

That’s it, you’ve added a new (almost native) element to WatiN. You could make it look even more native by extending the IElementContainer  interface with some extension methods which do wrap the calls to ElementOfType<ListItem >(…) and make it look like:

ListItem list = browser.ListItem (Find.ById(“theElementId”));

Add support for your (custom) web controls

I’ll tell you about that one in my next post… back to releasing the beta :-)

Technorati Tags:

3 reacties:

Jeff Brown zei

Looking forward to reading about this one. ;-)

Bernie zei

Can you please give an example of extending the IElementContainer so that I could call via
Item List list = browser.ListItem (Find.ById (theElementId "));

Cheers

Lowell zei

this thread is referred to as the newer, more updated way to add elements, but it's not obvious enough for me.
the original thread about handling the TH element suggested doing this in WatiN 1.0:
TableCell.ElementTags.Add(new ElementTag("th"));

This thread has an example of adding a new element, the LI for ListItem, but does not mention the TH issue and how to make the TableCell object also handle the TH tag,in addition to it's already implemented TD tag.
can you post an example of how to enhance the TableCell class to use the TH element in WatiN 2.0?