How to run automated UI tests using Selenium

Running automated UI tests using Selenium IDE:

Creating the tests

  1. Install the selenium server (unzip selenium rc to your c:\ drive)
  2. Install Firefox
  3. Run the IDE and specify the base url
  4. Create every test to open the base url by running the command “open” with value ”/”
  5. Now complete the test by recording your mouse clicks etc.
  6. Don’t forget to verify for things (i.e. text, titles, elements etc.)!
  7. Make sure each test is small and self contained (i.e. login in to start and do a click and wait on the logout to finish).
  8. Save your tests cases and suites with a html suffix and avoid whitespace

See the documentation here for more details: http://solobonite.wikidot.com/seleniumrunner-tutorial

Running Locally

There’s a command line interface like so:
java -jar selenium-server.jar -htmlSuite “*firefox” “http://localhost:8080” ”/path/to/folder/TestSuccessfulSuite.html” ”/path/to/folder/result.html”

Running on Hudson

We’ve added a Selenium plugin to Hudson. This basically creates the command in the previous step. 

Running automated UI tests using Selenium RC

It’s a better idea to store your tests under source control as C# code in a project rather than in a solution folder as html files. They can also be integrated into StoryQ syntax to be made more readble and modifiable. Use http://thetestingblog.com/2009/09/10/selenium-rc-in-c-using-nunit-an-end-to-end-example/ for reference.

Creating the tests

Create the tests as above using the Selenium IDE. Select “Options →Format → C# – Selenium RC” from the Selenium IDE window.

This will magically turn your html output into code.

Setup the C# Test Project

  1. Download SeleniumRC
  2. Add the “ThoughtWorks.Selenium.Core.dll” to your sharedlibs folder
  3. Create a new class project (.NET 3.5) called “ui.tests” in your solution and paste the code generated in the previous step into a class file in there.
  4. Add references to Selenium Core and nunit.
  5. Add a resources folder with the selenium server jar file in it
  6. Add the following post build event to your project

    copy "$(TargetDir)\resources\*.jar" c:\temp
  7. Modify the Selenium instantiation to launch your required browser
  8. Modify the url to the site you want to test (might be a good idea to have this as a config setting so you can reuse on web dev and live)
  9. Compile your project
  LAUNCHER NAME          DESCRIPTION                    CROSS-DOMAIN
  *iexplore, *iehta  =>  Internet Explorer in HTA mode  YES
  *iexploreproxy     =>  Internet Explorer normal       NO
  *firefox, *chrome  =>  Firefox in Chrome mode         YES
  *firefoxproxy      =>  Firefox normal                 NO

Running the Selenium RC Unit Tests

In order to run the Selenium RC test, you first have to have the Selenium RC server running. The Selenium RC server folder should be in the files you unzipped. Normally, I have a batch file on my desktop created to run the Selenium Server, but for our purposes, we’ll just run this from the “Start -> Run” commandline, using this command: java -jar C:\selenium-remote-control-1.0.1\selenium-server-1.0.1\selenium-server.jar

To automate this, I added the following method to my [SetUp] method:

        private void RunSeleniumServer()
        {
            if (!File.Exists(_seleniumServerJarFilePath))
                throw new SeleniumServerFileNotFoundException(
                    string.Format("Unable to find the selenium server file at location [{0}]",
                                  _seleniumServerJarFilePath));

            _proc = new Process();
            try
            {
                _proc.StartInfo.FileName = "java";
                _proc.StartInfo.Arguments = "-jar " + _seleniumServerJarFilePath;
                _proc.StartInfo.CreateNoWindow = false;
                _proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                _proc.StartInfo.UseShellExecute = false;
                _proc.StartInfo.WorkingDirectory = _tempDir;
                _proc.Start();
            }
            catch (Exception)
            {
                if (!_proc.Start())
                    throw new Exception("Failed to start selenium server");
            }
        }

and this to my [TearDown]:

        private void KillSeleniumServer()
        {
            _proc.Kill();

Running a Selenium Server for all Unit Tests in a namespace

Instead of instantiating a Selenium server in the [SetUp] and [TearDown] of each individual test, if several of your unit / acceptance tests are going to make use of Selenium then it’s better to create an instant for the lifetime of the test class.

Here’s a TestFixtureSetup class which is defined in the global namespace which NUnit runs before all and after all tests to start and stop the Selenium Server.

using System;
using System.Diagnostics;
using System.IO;
using NUnit.Framework;

// do not add a namespace here!

[SetUpFixture]
public class TestFixtureSetup
{
    private Process _proc;

    [SetUp]
    public void RunBeforeAnyTests()
    {
        RunSeleniumServer();
    }

    [TearDown]
    public void RunAfterAllTestsHaveBeenRun()
    {
        KillSeleniumServer();
    }

    private void RunSeleniumServer()
        {
            if (!File.Exists(_seleniumServerJarFilePath))
                throw new SeleniumServerFileNotFoundException(
                    string.Format("Unable to find the selenium server file at location [{0}]",
                                  _seleniumServerJarFilePath));

            _proc = new Process();
            try
            {
                _proc.StartInfo.FileName = "java";
                _proc.StartInfo.Arguments = "-jar " + _seleniumServerJarFilePath;
                _proc.StartInfo.CreateNoWindow = false;
                _proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                _proc.StartInfo.UseShellExecute = false;
                _proc.StartInfo.WorkingDirectory = _tempDir;
                _proc.Start();
            }
            catch (Exception)
            {
                if (!_proc.Start())
                    throw new Exception("Failed to start selenium server");
            }
        }

    private void KillSeleniumServer()
    {
        _proc.Kill();
    }
}

Running on Hudson

You just add this as a normal NUnit test. (:


What did you think?

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

WordPress.com Logo

You are commenting using your WordPress.com 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