Start and stop Selenium Server with NUnit

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.

  1. I started by downloading the Selenium RC and unzipped it to C:SeleniumSelenium-RC
  2. I created a .NET project where I referenced the ThoughtWorks DLLs in C:SeleniumSelenium-RCselenium-dotnet-client-driver-1.0.1
  3. I added a new class to where I copied the Selenium getting started C# class
  4. 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!

Publicerat av

Ola

Mångsysslande doer & klätterentusiast. Jag driver varumärket Sten & bloggen Klätterbilder. Jag konsultar inom IT & Marknadsföring samt undervisar inom webb på Folkuniversitetet. Jag älskar att fota. /// Doer & climbing enthusiast. I run the climbing brand Sten & the blog Klätterbilder. Consulting with IT & marketing & teaching at Folkuniversitetet. I love to take pictures.

En reaktion på ”Start and stop Selenium Server with NUnit”

  1. 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.

Kommentarer inaktiverade.