I want to have our Selenium tests to run automatically the same way as our other NUnit tests does. This means that I want to have the Selenium Server started automatically before running the test cases.
- I started by downloading the Selenium RC and unzipped it to C:SeleniumSelenium-RC
- I created a .NET project where I referenced the ThoughtWorks DLLs in C:SeleniumSelenium-RCselenium-dotnet-client-driver-1.0.1
- I added a new class to where I copied the Selenium getting started C# class
- I added my own TestFixtureSetup and a TestFixtureTeardown to this class. TestFixtureSetup launches the Selenium Server and TestFixtureTeardown closes it
Here is the class for starting the Selenium Server and the example Selenium test.
using System;
using System.Diagnostics;
using System.Text;
using NUnit.Framework;
using Selenium;
namespace Test.UI.Web.Selenium
{
[TestFixture]
public class SeleniumGoogleTest
{
private ISelenium selenium;
private StringBuilder verificationErrors;
private Process seleniumServer;
private readonly ProcessStartInfo seleniumServerProcessStartInfo = new ProcessStartInfo("java", @"-jar C:SeleniumSelenium-RCselenium-server-1.0.1selenium-server.jar");
[TestFixtureSetUp]
public void TestFixtureSetup() {
seleniumServer = Process.Start(seleniumServerProcessStartInfo);
}
[TestFixtureTearDown]
public void TestFixtureTearDown() {
seleniumServer.CloseMainWindow();
}
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "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 OpenGoogleWebSite()
{
selenium.Open("http://www.google.com");
}
}
}
Enjoy!
Nice! I no longer work with ASP.Net but Selenium could be useful for my colleagues and ex-colleagues in Thailand. Thanks for the article.