Watin is a web application testing framework handy for writing high-level smoke screen tests against your .NET web application.

The website has a decent getting started section but I ran into one gotcha (really just a missing dll) and wanted to show you how to get it up and running quickly and easily using NUnit on a simple MVC.NET application.

Step 1: Download Watin

You can download Watin from the main website or just click here and download the lastest Watin 2.x release candidate.

Step 2: Create an empty web application.

For this example I created an empty ‘ASP.NET MVC2 Web Application’ and then added a ‘Class Library’ project to hold my Watin unit tests.

Step 3: Add references.

To run the Watin tests you need two dlls:

  • Watin.Core.dll
  • Interop.SHDocVw.dll

Add these (as well as nunit.framework.dll) to your unit test class library.

Step 4: Create config file to set thread state

Because Internet Explorer is not thread safe, we need to configure NUnit to run in single thread state mode (more details on why here).

Add a ‘Application Configuration File’ (i.e. App.config) to your unit test dll

and paste the following into your configuration file:

<?xml version="1.0" encoding="utf-8" ?>
   <sectionGroup name="NUnit">
    <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
    <!-- Valid values are STA,MTA. Others ignored. -->
    <add key="ApartmentState" value="STA" />

Step 5: Write your test(s)

The Watin Getting Started section has a simple example to get you going.

using NUnit.Framework;
using WatiN.Core;

namespace tests
    public class WatinTest
        public void HelloGoogle()
            // Open a new Internet Explorer window and
            // goto the google website.
            IE ie = new IE("http://www.google.com");

            // Find the search text field and type Watin in it.

            // Click the Google search button.
            ie.Button(Find.ByValue("Google Search")).Click();

            // Uncomment the following line if you want to close
            // Internet Explorer and the console window immediately.

When you run this test, your default browser should pop-up, enter ‘Watin’ in the search box, and then click the search button.

To run a test against your MVC.NET application, you need to start your webserver. Hit F5 to run your application, note the URL it starts up on (i.e. http://localhost:1110), and then edit the test below so it looks something like this:

        public void HelloMVC()
            IE ie = new IE("http://localhost:1110");
            Assert.IsTrue(ie.Text.Contains("My MVC Application"));

Congratulations! You’ve just written your first Watin test.

Next week we will dive deeper and look more closely at some specific Watin controls and how they work.