Over the last couple of months we’ve talked about the need to get some UI testing in place for some of the projects that I work on daily. I considered Selenium and spent a little time looking at Watir. As I started getting into Watir I realized that understanding and implementing it would take some time on my part and I set it aside for another day.
Early last week I was asked about UI testing again and coupled with my overhearing a mention of how much someone liked using WatiN at the Dayton .NET Developer’s Group I decided to give it a look. Friday I downloaded and began playing with it and I was surprised how easy it was to implement. For me, the overhead in using WatiN is much, much less than Watir. It probably took me less than 30 minutes to download it, check out some examples and get some tests running.
To set up a WatiN test you:
add a reference to the NUnit.Framework dll (or unit testing tool of your choice),
add a reference to the WatiN.Core dll
write some tests. Example:
[Test]
public void TestLogin() {
using (IE ie = new IE(“http://localhost/somesite/”)) {
//Fill in login info and submit the login form
ie.TextField("txtUsername").Value = "myusername";
ie.TextField("txtPassword").Value = "mypassword";
ie.Button("btnLogin").Click();
//Should be redirected back to the home page, check for expected text
Assert.IsTrue(ie.ContainsText("Expected text"), "Unsucessful login");
}
}
That’s all there is to it. No installing Ruby, no installing Watir, no wiring up a bunch of other stuff to get Watir to run in NUnit or as part of an automated build.
When necessary, to help with the WatiN scripting I have used WatirRecorder++. WatirRecorder++ outputs script to be used by Ruby, but it wasn’t too tough for me to translate to C# code - the WatiN developers have done a pretty good job of sticking to the Watir API. There is a port of WatirRecorder++ to WatiNRecorder, but it doesn’t appear to be active and I have read requests for the source that appear to be unanswered.
There are some threading issues that need to addressed with NUnit, other testing frameworks and other programs using WatiN. I was able to work around each of those easily enough when running the tests in Visual Studio, with Resharper’s Test Runner, or via the NUnit console or command line.
However, the one thing I haven’t been able to address is getting the WatiN NUnit tests running under Cruise Control.NET. The above mentioned fixes for NUnit did not seem to help when running under Cruise Control. If I run the test manually via the command line then they works fine. If I run the tests in Cruise Control via NAnt I get the ThreadStateException. I have read a couple of comments on the WatiN Sourceforge site and in some forum postings where some claim to have gotten it working in CC.NET, but I have not seen any examples, descriptions, or other details yet. Hopefully this issue will get corrected soon otherwise I’ll have to look elsewhere for UI testing. It will not help me all that much to have good UI tests in place that will not run in my CI process.