Running automated UI tests using Selenium IDE:
Creating the tests
-
Install the Java Runtime Environment
-
Install the selenium server (unzip selenium rc to your c:\ drive)
-
Install Firefox
-
Install the firefox selenium IDE plugin
-
Run the IDE and specify the base url
-
Create every test to open the base url by running the command “open” with value ”/”
-
Now complete the test by recording your mouse clicks etc.
-
Don’t forget to verify for things (i.e. text, titles, elements etc.)!
-
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).
-
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
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
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
-
Download SeleniumRC
-
Add the “ThoughtWorks.Selenium.Core.dll” to your sharedlibs folder
-
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.
-
Add references to Selenium Core and nunit.
-
Add a resources folder with the selenium server jar file in it
-
Add the following post build event to your project
copy "$(TargetDir)\resources\*.jar" c:\temp
-
Modify the Selenium instantiation to launch your required browser
-
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)
-
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. (:

