How to Configure Selenium RC for Use In C# NUnit Tests


When I set about integrating Selenium into my test suites, I found all the information I needed to do that with but had to hunt and peck through my google searches to find it.  So, as a point of reference, I figured I’d put what I needed to do all in one place:

Two main activities:

  1. Set up Selenium RC server in Windows
    • Download latest Java SE from http://java.sun.com/ and install
    • Create a folder named Selenium under your jdk or jre bin file (example: C:Program FilesJavajre1.6.0_05bin).
    • Download latest version of Selenium RC from http://seleniumhq.org/download/ and extract into your newly created folder
    • From the Command prompt run the following commands:
      • cd [your jdk/jre bin directory] (example: C:Program FilesJavajre1.6.0_05bin).
      • java -jar .Seleniumselenium-server.jar -interactive
      • If you see the following messages your Selenium server is alive and kickin’:
        Entering interactive mode… type Selenium commands here (e.g: cmd=open&1=http:/
        /www.yahoo.com)
  2. Place a reference to the ThoughtWorks.Selenium.Core.dll into your .NET test assembly
    • This

      can be found under the Selenium install directory (example: C:Program

      FilesJavajre1.6.0_05binSeleniumselenium-remote-control-1.0-beta-2selenium-dotnet-client-driver-1.0-beta-2ThoughtWorks.Selenium.Core.dll)

Git ‘Er Done With Some Tests

Now, you’re up and ready to start writing NUnit tests using Selenium in C#.  By the way, you can record your tests using the Selenium IDE and export the tests to a number of languages, including C#:

SeleniumExport

Example Test Suite as Exported using the above:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;

namespace SeleniumTests
{
    [TestFixture]
    public class NewTest
    {
        private ISelenium selenium;
        private StringBuilder verificationErrors;
        [SetUp]
        public void SetupTest()
        {
            selenium = new DefaultSelenium(“localhost”, 4444, “*chrome”, “http://sparkystestserver:48/”);
            selenium.Start();
        }
        [TearDown]
        public void TeardownTest()
        {
            try
            {
                selenium.Stop();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
           Assert.AreEqual(“”, verificationErrors.ToString());

        }
        [Test]
        public void TheNewTest()
        {
            selenium.Open(“/Home”);
            selenium.Type(“loginname, sparky);
            selenium.Type(“password”, “mooseButt);
            selenium.Click(“ctl00_MainContent_loginButton”);
            Assert.AreEqual(“burp”, selenium.GetValue(“burpField”));
        }
    }
}

Hope that helps!

 

[Originally posted on 2/3/2009 at http://agilecruz.blogspot.com]

Qualities that Undergird Agile/XP Development