views:

524

answers:

4

I am introducing Selenium tests into my build for the first time. I figured that to do this in NAnt, I would have to start the WebDev server first:

<exec program="path/to/WebDev.WebServer.exe"
   commandline="/port:51150 /path:path/to/website"
   failonerror="true"
   resultproperty="selenium.webdev.server.running"
   spawn="true">
</exec>

Then start the Selenium server:

<exec program="path/to/java.exe"
   commandline="-jar path/to/selenium-server.jar"
   failonerror="false"
   spawn="true">
</exec>

Then run my tests. This works. What i can't figure out is how do I kill the WebDev and Selenium servers when my tests have finished?

A: 

We usually leave the Selenium server running all the time on our build servers, it's more practical that way.

Igor Brejc
I also like to run tests locally - and don't want WebDev server and Selenium server hanging around afterwards...
James Allen
+1  A: 

James, I managed to solve Selenium starting/stopping problem by applying the test assembly initialization and cleanup mechanism (see the rest of the discussion on my blog):

[AssemblyFixture]
public class SeleniumTestingSetup : IDisposable
{
    [FixtureSetUp]
    public void Setup()
    {
        seleniumServerProcess = new Process();
        seleniumServerProcess.StartInfo.FileName = "java";
        seleniumServerProcess.StartInfo.Arguments =
            "-jar ../../../lib/Selenium/selenium-server/selenium-server.jar -port 6371";
        seleniumServerProcess.Start();
    }

    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or
    /// resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    /// <summary>
    /// Disposes the object.
    /// </summary>
    /// <param name="disposing">If <code>false</code>, cleans up native resources. 
    /// If <code>true</code> cleans up both managed and native resources</param>
    protected virtual void Dispose(bool disposing)
    {
        if (false == disposed)
        {
            if (disposing)
                DisposeOfSeleniumServer();

            disposed = true;
        }
    }

    private void DisposeOfSeleniumServer()
    {
        if (seleniumServerProcess != null)
        {
            try
            {
                seleniumServerProcess.Kill();
                bool result = seleniumServerProcess.WaitForExit(10000);
            }
            finally
            {
                seleniumServerProcess.Dispose();
                seleniumServerProcess = null;
            }
        }
    }

    private bool disposed;
    private Process seleniumServerProcess;
}
Igor Brejc
Nice solution. Maybe add the code from your blog to your answer?
James Allen
Yes, I'll do it - but first I have to solve one glitch: the server doesn't get stopped after running these tests in CruiseControl.NET. So a new task for me for tomorrow :)
Igor Brejc
A: 

Failing that, there's always the trusty old pskill. It's a big hammer approach, but it works a treat to kill off webdevwebserver :-)

I know very very little about selenium, so apologies in advance if pskill is no good for stopping it

Dan F
I think it wouldn't really be a problem to use pskill, as long as you know which process to kill. There can be several Selenium servers running on your build server (and all of them run as "java"-named processes).
Igor Brejc
+1  A: 

Here is what i do locally, but should work remotely too with a simple http get request:

http://localhost:4444/selenium-server/driver/?cmd=shutDown

or for post 1.0.1 versions of Selenium:

replace "shutDown" with "shutDownSeleniumServer"

ruby fu novice
Perfect, exactly what I'm after. Is this in the official docs? I couldn't find it
James Allen
that one wasn't well documented. i found it somewhere in the openqa wiki pages.
ruby fu novice