Automated Testing and Sitecore - Part 7
WatiN is a browser driver that instantiates a new browsing window and provides access directly to the DOM to allow manipulation and testing. WatiN only currently supports IE. If you need cross browser testing you will have to use one of the other browser drivers such as Selenium.
Because WatiN needs to interact with applications on your desktop (IE) I would not suggest running your WatiN tests in the custom test runner. In fact, I haven’t yet been able to get WatiN to run in the custom test runner due to the threading apartment of the ASP.NET requests.
The first thing we need to do is to create some test content programmatically using the techniques which have been covered in previous parts of the series. The gist of this technique is to have a layout which does item creation and cleanup and we call an item with this layout across HTTP, so our windows app can control the lifetime of the test items.
Now that we have some content to test against we can create a WatiN.IE object to make the request and verify the results.
const string DOMAIN = "http://localhost";
We can find elements in the DOM in a few ways using WatiN. Firstly as shown above, you can access a kind of element, such as a paragraph above, by index. Generally this isn’t adequate and you’ll want to be finding by ID. So in that case we can use the method on the IE object rather than the property.
Keep in mind the ID we’re searching for will be the clientID in ASP.NET controls which may be of the format “ctlxx_name”. Make sure you get the right ID.
We can also make use of LINQ for querying elements by other parameters. In the below code snippet I’ll locate the first div with a class attribute of “breadcrumb”.
var match = from WatiN.Core.Div d in ie.Divs where
I can also use a WatiN constraint to find an element by an attribute.
So now that we know how to find elements in the DOM, we can start testing the dynamic behaviors of our presentation components. First we’ll find a textbox, put a value in it, then click the submit button of the form and validate the text in the span.
const string DOMAIN = "http://snd.localhost";
Footnote for Vista users: make sure the site you’re testing is in your trusted sites list so WatiN can interact with the IE window properly. Check out http://codebetter.com/blogs/james.kovacs/archive/2008/06/18/running-watin-tests-on-vista.aspx for details.