One of the best things any developer can do is to automate regular tasks as much as possible. On the quality control side, this includes running regular functional tests to check to make sure that everything still works exactly as it’s supposed to. Selenium is a project which lets developers write tests for browsers to “drive themselves”. This basically means you develop a script which selenium can then read and interpret into mouse clicks. Scripts can be written by hand, but it is usually easiest to start by having the Selenium IDE watch you click around and then use the script it generates as a starting point. The best part about this project is Seleneium-RC which lets you take the scripts and run them directly from .NET, Java, Ruby, PHP, and others. Having this capability also lets us integrate these scripts into unit testing, which we can run regularly with tools like CruiseControl.
For example, if I launch Firefox, and open up the Selenium IDE, and then open google and search for “Billy Bob Thorton”, then click pages 2,3,4, Next, and Back, Selenium IDE will look like this:

We can then export this script to code. C#, for Example:
using System; using System.Text; using System.Text.RegularExpressions; using System.Threading; using NUnit.Framework; using Selenium;namespace SeleniumTests { [TestFixture] public class google { private ISelenium selenium; private StringBuilder verificationErrors; [SetUp] public void SetupTest() { selenium = new DefaultSelenium(“localhost”, 4444, “*firefox”, “http://localhost:4444″); selenium.Start(); verificationErrors = new StringBuilder(); } [TearDown] public void TeardownTest() { try { selenium.Stop(); } catch (Exception) { // Ignore errors if unable to close the browser } Assert.AreEqual(“”, verificationErrors.ToString()); } [Test] public void TheGoogleTest() { selenium.Open(“/”); selenium.Type(“q”, “billy bob thorton”); selenium.Click(“btnG”); selenium.WaitForPageToLoad(“30000″); selenium.Click(“link=2″); selenium.WaitForPageToLoad(“30000″); selenium.Click(“link=3″); selenium.Click(“link=4″); selenium.WaitForPageToLoad(“30000″); selenium.Click(“link=Next”); selenium.WaitForPageToLoad(“30000″); selenium.Click(“link=Previous”); selenium.WaitForPageToLoad(“30000″); } } }
This code should be compiled to a Windows Library type, and run within an NUnit Test runner. There’s a few things you need to consider before straight up running this code:
- Ensure that your selenium server is running on your localhost, or change the selenium constructor line to point to the correct network path.
- Include the NUnit.Framework and NUnit.Core dll’s in your resource directory, and make sure they get copied to the output directory.
For the NUnit test runner, I would reccommend downloading testdriven.net- it allows you to easily right click within the method and then run the tests without having to worry about compiling, launching the NUnit test runner, and then opening up the DLL. The end result of this is a firefox browser is opened, and Selenium reproduces exactly what you did when you were controlling firefox.
More on integrating this test with CruiseControl soon…

