Monday, November 29, 2010

Automatically launching Selenium server

Selenium is an amazing tool.  However, one of the most annoying things about it is having to deal with the selenium server.  For those who don't know, the selenium-server is a .jar that you run on the machine you will be testing on.  It has to be running for tests to pass.  What this means is that you either have to start/stop it each time you want to perform a test, or you have to leave it up and running on your machine constantly.  This can be an issue when you are dealing with a remote machine, you don't want to have to remote desktop into it, launch selenium-server.jar, and then run your tests.

So my first blog will be about how to add a SetUpFixture to your tests to automatically launch and close selenium-server.

I created a new class to contain the SetUpFixture.  For those who don't know, the SetUpFixture tag identifies a class to be run before/after all Fixtures.  I created a string called Common.seleniumDirectory which contains the location of my tests.  I did it this way to allow it to be more dynamic.  I also am having it launch a specific firefox profile, however you can modify the string to be whatever you want.

namespace SeleniumTests
{
    [SetUpFixture]
    public class Common_Setup_Fixture
    {
        // Runs before all tests
        public static Process seleniumServer;
        public static readonly ProcessStartInfo seleniumServerProcessStartInfo = new ProcessStartInfo("java", "-jar " + Common.seleniumDirectory  + "RemoteControl\\selenium-server-1.0.3\\selenium-server.jar -firefoxProfileTemplate \"" + Common.seleniumDirectory + "FirefoxProfile\"");
      
        [SetUp]
        public void SetUp()
        {
            seleniumServer = Process.Start(seleniumServerProcessStartInfo);
            Common.verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TearDown()
        {
            try
            {
                seleniumServer.CloseMainWindow();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
          
        }
    }

No comments:

Post a Comment