When using WatiN it is easy to automate simple actions on standard elements in the browser. But when you try to automate (complex) controls of control vendors like Telerik, Infragistics or even your own custom controls, things get to start ugly quickly. Wouldn't it be nice if we could automate these controls in the same way we do automate a TextField ?
In this blog post and the next one, I while show you how.
The html code to automate
Lets start small and simple. The following HTML snippet is created by a (fictitious) custom date time control. For one of our (again fictitious) tests we need to set the values of the date and time input elements.
<table id="datetime2">
<tr>
<td>
<input type="hidden" id="original_date" value="01-01-2007" />
<input type="text" id="date" value="01-01-2007" />
</td>
<td>
<input type="hidden" id="original_time" value="10:00:00" />
<input type="text" id="time" value="10:00:00" />
</td>
</tr>
</table>
As you can see the html for this date time control contains two input fields, one for the date and one for the time. There are also two hidden input elements containing the original values of the input fields.
The simple way
To automate setting the date and time the following code will do the job nicely.
IE ie = new IE("http://www.examples.watin.net/datetime.html");
ie.Table("datetime2").TextField(new Regex("^date$")).TypeText("01-11-2008");
ie.Table("datetime2").TextField(new Regex("^time$")).TypeText("12:00:00");
So what's wrong with this code you might wonder. It basically comes down to being code and logic that isn't reusable. To name two issues:
- To get the correct TextField for the date or time, you need to remember (or find out the hard way) the correct regular expression. Otherwise you might end up setting the value of the hidden input element which will give you a nice exception (and then you remember... and I can tell).
- The formatting of the date and time needs to be in this specific format for the control to work.
We could refactor this into a method, but still. WatiN offers you all kinds of objects that wrap html elements so it feels more natural for this date time control to be wrapped as well.
Basic wrapping
So lets create a wrapper. I will use Table as the base for our new element class since Table seems to be the container element for the control.
public class DateTimeElement : Table
{
public DateTimeElement(Table table) : base(table) { }
public void TypeDateTime(DateTime value)
{
TextField(new Regex("^date$")).TypeText(value.ToString("dd-MM-yyyy"));
TextField(new Regex("^time$")).TypeText(value.ToString("hh:mm:ss"));
}
}
We can use this new DateTimeElement class in our tests like this:
DateTimeElement element = new DateTimeElement(ie.Table("datetime2"));
element.TypeDateTime(new DateTime(2008, 11, 1, 12, 00, 00));
In my opinion the readability and intention of this code is way better then our first approach. And it also encapsulate the knowledge of the regular expression and the specific formatting of the date and time.
So are we happy now. Not completely. I think the syntax of creating the new DateTimeElement can be improved cause it feels weird to pass in a table element instance into the constructor. Another, not visible, drawback of passing in an element is that it doesn't use WatiN's deferred execution to find the table element on the page. In more complicated wrappers this might lead to unexpected behavior. For now I will spare you the details.
Stay tuned
In part II I will show you how you can get deferred find execution by providing a small helper class and making some changes to the DateTimeElement constructor.
To top things off I will use extension methods (C# 3.0 only) to give you this end result in you test code:
ie.DateTimeElement("datetime2").TypeDateTime(new DateTime(2008, 11, 1, 12, 00, 00));
Stay tuned....
0 reacties:
Een reactie posten