Thursday, 14 August 2008

Setting up and using WatiN

I am going to be doing a 10 minute nugget about WatiN at NxtGenUG Southampton on Thursday 21st August, so as I was preparing for it, I thought I would put together a little post about the relevant details.

So to get started with WatiN you need some basic tools:

  • WatiN Framework - this can be downloaded from here, at the time of writing the current RTM version is 1.2.1 but version 2.0 is available as a CTP, I shall be focusing on version 1.2.1 for my nugget. Simply download the MSI/zip of your choice, I have chosen the WatiN-
  • WatiN Test Recorder - this is really useful for getting you started with writing WatiN tests, it can be downloaded from here. This is currently at version 1.0.1b and as far as I know there is only a single download.
  • IE Developer Toolbar - WatiN 1.2.1 only supports IE, but 2.0 has support for FireFox. This is an essential tool for any kind of web development let alone for use when developing WatiN tests, it lets you inspect the html elements of a web page by simply pointing and clicking at the element you want to inspect. This can be downloaded from here.

These are the basic tools you will need but to make some real use of WatiN you will want to combine it with your favourite unit test framework, I've chosen MS Test simply because its the test tool of choice at my current place of work but WatiN will work with other tools such as NUnit etc.

Writing Your First WatiN Test

Once you've installed the above tools its time to start writing your first WatiN test, because my demonstration is going to be in a hall with no access to the Internet I need to use a locally hosted web site so I thought I would download a Northwind ASP .NET Sample web site with Ajax, now this is certainly not the prettiest web site or even the most functional but hey, it serves the purpose :-). Once you've downloaded the zip file, simply extract it to a location of your choice and setup an IIS application, remembering to change the connection string in the web.config to point to your version of the Northwind database.

Using VS2008 create a new test project called Northwind.Watin.Tests, delete theimage AuthoringTests.txt and rename the UnitTest1.cs file to something like NorthwindTests.cs.

Next we need to add a reference to our installed WatiN.Core assembly, the installer places the file in C:\Program Files\WatiN\1.2.1-net-2.0\bin folder but you can move it elsewhere if you desire.

We shall create a test method called GoToNorthwind that will simply perform the job of creating a Internet Explorer instance, entering the url of our Northwind application in the address bar and navigating to our Northwind web site. Ensure that you have added the

using WatiN.Core;

at the top of the page and the create a test method that looks something like this:

public void GoToNorthwind()
using(var ie = new IE())

So quite simply this test creates a new instance of IE, which is essentially an automation wrapper around Internet explorer, enters the url specified and in the address bar and then asserts that the IE instance contains the text "Northwind", obviously we could have done a bit more of test here but you get the picture.

Using WatiN Test Recorder to Create a Test

This time we'll use the WatiN Test Recorder to create the same test, launch an instance of the recorder, it should look something like the following, next in the "Test Name" text box type the name of the test "GoToNorthwind" and click the Record button, this will start recording the test.


Type the url into the address bar http://localhost/Northwind/default.aspx and watch the good things happen, you will notice that the main pane of the application navigates to the desired url whilst the text box under the test name shows the WatiN test script required to achieve this functionality.


Simple eh? In my experience I have found that this tool is really good for the development of tests during the early stages of developing tests but as you get more used to the syntax of key words of WatiN your dependency on this tool becomes less and less, I would thoroughly recommend exploring this tool further to get a better understanding of the kinds of things that WatiN can do, however you will soon start to realise that the tests you are creating using this tool are not very reusable or maintainable.

At this point you need to start looking at a way to make your tests reusable and easily maintainable, I'll be giving you one way to do this in my next post in this series.

1 comment:

Anonymous said...

this is very nice blog...
this is very helpful and attractive biog.
visit for help help