WatiN and INamingContainer adventures


I decided to add a simple WatiN acceptance test today to validate our login page.  When a user enters invalid credentials, the page should show an error message.

To test this, I need to set the text of the username and password, and click the Login button.  Since I didn’t want just any textbox, but specifically the username and the password, I like to use the element ID’s to find them.

We’re using the ASP.NET Login control to provide the login interface, which has the textboxes, the button, and the error message in one package.  When I want to get the ID, this is what it winds up being:

ctl00_ctl00_ContentPlaceHolderMain_CenterContent_ctl00_ctlLogin_UserName

Well that’s no fun.  The INamingContainer interface puts out some crazy HTML element IDs to guarantee uniqueness.  Luckily I can use regular expressions to search for elements ending in “UserName”, and that works for the button, too.

However, the error message does not have an ID around it, so I have to dig into the login control manually to fish it out:

Table loginControl = browser.Table(new Regex("._ctlLogin$"));
TableCell errorCell =
    loginControl.TableBodies[0].TableRows[0].TableCells[0].
        Tables[0].TableBodies[0].TableRows[3].TableCells[0];

Since I don’t have access to the innards of the HTML in the Login control, this is what I have to resort to.

I’m really looking forward to MVC framework, where I can take back control over the HTML generated.  At least WatiN will be easier.

Some essential Visual Studio tools