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:
- Add support for native html elements not provided by WatiN.
- 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.
- First create a new class called ListItem and make it inherit WatiN.Core.Element.
- Add the ElementTagAttribute to your class to tell WatiN which tagName(s) your element class can handle. Incase of ListItem: [ElementTag(“li”)]
- Add whatever properties and methods are needed to make this element work for you.
- 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 :-)