Creating Selenium Unit Test Suites with NUnit

When looking for a way to test our websites by using web macros, directly interacting with the rendered presentation of our sites, I ran across quite a few test suites. Both Waitr and Selenium stood out as the more stable, extensible and easy to incorporate test suites out there. Selenium, however, seemed to offer easy implementation on a .Net platform. Once properly setup, Selenium unit tests are as easy to create as opening Firefox, browsing the site you want to test and record your unit test macro as you navigate from one page to another.

Selenium comes in with a few different installation components and programs:

  • Selenium IDE – Firefox plugin used to create the unit tests.
  • Selenium Core – Core libraries for .Net/C#.
  • Selenium Remote Control -Allows for managing Selenium unit tests with multiple broswers simultaneously (includes the Selenium Core libraries).
  • Selenium Grid -Load balance your tests across multiple servers.

Installing Selenium IDE

Download and install the Selenium IDE. This will add a “Selenium IDE” option under tools in your Firefox browser.

Installing Selenium Remote Control

Download the Selenium RC installation files and extract to a location on your test server. For this example, I extracted to c:\Selenium-Remote-Control\.

Starting the Selenium Server is as easy as running “java -jar selenium-server.jar” from the directory of your Selenium Server installation. Since this is a java-based application, you may be required to download the latest version of java prior to successfully starting the Selenium Server.

Starting selenium server

Starting the Selenium Server using Java v1.6.017

If your Selenium Server output resembles something like above, then you’re in business.

Installing NUnit

Installing Selenium Server is not enough. You must also install NUnit to drive the C# generated unit tests that target the Selenium server.

Download the NUnit installation files and install the software to your test server.

Creating Your First Selenium Unit Test

1. Open Firefox

2. Go to tools and select Selenium IDE

Starting Selenium IDE

Starting the Selenium IDE

3. Once the IDE opens, it defaults to recording every action you take on a given website. Select the output that the IDE will use to generate your unit test. For this example, we’re using C#.

Choose C#

Choose C# to output your test

4. While still within Firefox, click through the website for which you want to create a unit test. Every action you take will be recorded by the IDE, with your unit tests generated immediately.

5. When done generating your test, click on the record/stop button to end the IDE. You’ll see your complete unit test within the Source tab of the Selenium IDE window.

Selenium IDE Record Actions

Record your actions in C#

Example Unit Test Generated by the Selenium IDE

Clicking through w3school’s website generated the following code:

Selenium Test Code

Snapshot of Generated Code

Steps to generate the code above:

Clicked on Next Chapter

Clicked on "Next Chapter"

Clicked on Try it Yourself

Clicked on Try it Yourself

Edited the HTML

Edited the HTML

Running The Example Test

1. Take the C# code generated by the Selenium IDE and add it as a class to a new Visual Studio project.

2. Add references to your project for the following dll’s

  • C:\Selenium-Remote-Control\selenium-dotnet-client-driver-1.0.1\nmock.dll
  • C:\Selenium-Remote-Control\selenium-dotnet-client-driver-1.0.1\nunit.core.dll
  • C:\Selenium-Remote-Control\selenium-dotnet-client-driver-1.0.1\nunit.framework.dll
  • C:\Selenium-Remote-Control\selenium-dotnet-client-driver-1.0.1\ThoughtWorks.Selenium.Core.dll
  • C:\Selenium-Remote-Control\selenium-dotnet-client-driver-1.0.1\ThoughtWorks.Selenium.IntegrationTests.dll
  • C:\Selenium-Remote-Control\selenium-dotnet-client-driver-1.0.1\ThoughtWorks.Slenium.UnitTests.dll
Selenium Project References

Selenium Project References

3. Build the solution.

4. Open NUnit. Select File > Open Project… and select the your project’s compiled assembly.

5. Within NUnit, highlight your unit test class name and click on Run.

NUnit Run

Sit back and watch the show. NUnit will kick off the unit test, which runs the compiled code generated by the Selenium IDE. You’ll see the Selenium Server output the commands in the command prompt window. Selenium Remote Control Server then opens, which triggers the opening of a browser window to run the recorded Selenium unit test. NUnit will then report back the results of the test.

Selenium in Action

Selenium in Action


About Dave
Certified Sitecore Developer.

4 Responses to Creating Selenium Unit Test Suites with NUnit

  1. Pingback: Integrating Selenium Tests into CruiseControl.Net via NUnit « Everything Web

  2. Arun Krishnankutty says:

    Hi Dave,

    Got Selenium working on my local box. This is sooooooooo cool.


  3. Pranjal says:

    Hi Dave,

    Thanks for providing such a valuable information.

    Pranjal R Nigam

  4. Zohaib says:

    Excellent article. Very detailed and good screen shots. Only error I ran into was, I used VS2010 .Net 4.0 project. By default a console app is set to .Net 4.0 client profile, change it to .Net 4.0 from project properties > Application > Target framework and good to go.

    Great work!

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: