First Steps with Selenium and NUnit

After installing Selenium and NUnit, I started my first experiments with the testing framework. Starting from the results of the Selenium IDE, which generates C# source of recorded test cases, I quickly created a test case project in VS2005.

In NUnit, each test case is a class with the [TestFixture] attribute, whose public methods are tagged with [SetUp], [TearDown] and [Test].

Helper Class

Since I intended to write several tests, the first step was to encapsulate Selenium initialization and login steps into a helper class:

public class Helper
  public static ISelenium StartSelenium()
    ISelenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://web.server");
    return selenium;

  public static string Login(ISelenium selenium)
    selenium.Type("username", "User");
    selenium.Type("password", "Password123");
    selenium.FireEvent("username", "keyup");
    return selenium.GetLocation();

A test class simply calls the StartSelenium() method from the [SetUp] method, and the [Test] method uses Login() to log in to the web application.

Javascript Events

The selenium.Type() method only sets an input controls text, but does not simulate key or mouse events. If you have a button which is enabled through Javascript events, you need to explicitly fire an event.


NUnit GUI displays all calls to System.Diagnostics.Trace.WriteLine() in its Trace tab.

Integration with log4net

This blog has basic instructions of how to integrate log4net (in VB.Net though).

To have log4net functionality for every test case, I created a class with the [SetUpFixture] attribute, with its [Setup] method creating the logger. This provides logging functionality for every test in the namespace.

One Response to First Steps with Selenium and NUnit

  1. […] start up the web application test, we have to login first, and we should be able to detect .Net server […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: