6

So, I have some code set up via Gallio, to start the selenium server before each test session, and shut it down when the session finishes (all tests have run). It starts the server fine, but I can't seem to get it to stop the server. I'm pretty sure I've done something wrong, but I can't see what. Can you help me?

I'm using:
* Selenium Server 2.0b4
* Selenium WebDriver 2.0b4
* Visual Studio 2010 (C#)
* ReSharper
* Gallio

Here is my code:

[AssemblyFixture]
public class SeleniumSetup : IDisposable
{
    private Process seleniumServer;
    private Boolean disposed;

    [FixtureSetUp]
    public void Setup()
    {
        const string javaFileLocation = @"C:\opt\java\bin\java.exe";
        const string jarFileLocation = @"C:\my\autotests\Selenium\selenium-server.jar";
        var seleniumServer = new Process
            {
                StartInfo =
                    {
                        FileName = javaFileLocation,
                        Arguments = "-jar " + jarFileLocation + " -port 4444 -timeout 90 -browserSideLog -ensureCleanSession -trustAllSSLCertificates -log selenium-server.log",
                        WorkingDirectory =
                            jarFileLocation.Substring(0, jarFileLocation.LastIndexOf("\\")),
                        UseShellExecute = true
                        //WindowStyle = ProcessWindowStyle.Hidden
                    }
            };
        seleniumServer.Start();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (false == disposed)
        {
            if (disposing)
                DisposeOfSeleniumServer();

            disposed = true;
        }
    }

    [FixtureTearDown]
    private void DisposeOfSeleniumServer()
    {
        if (seleniumServer != null)
        {
            try
            {
                seleniumServer.Kill();
                seleniumServer.WaitForExit(10000);
            }
            finally
            {
                seleniumServer.Dispose();
                seleniumServer = null;
            }
        }
    }
}
3
  • Since I'm still relatively new to Selenium, this may not be a great question, but, is there a reason why you're running Selenium Server with Selenium2? I didn't think that it was required with the new API. Or is this running in the background without me noticing? Commented Jun 14, 2011 at 4:17
  • 1
    @Lyndon You still need to run the selenium server to get the Remote Web Driver to work. We tend to use this to allow our test to be run on a another machine rather than the build server. You also need it in .net to run the headless browser (HtmlUnit).
    – stuartf
    Commented Jun 14, 2011 at 8:00
  • I love learning something new when I wasn't looking for it. Thanks! Commented Jun 14, 2011 at 12:39

1 Answer 1

4

The following was the best way to close the server in my experience.

    /// <summary>
    /// Close the server.
    /// </summary>
    public static void CloseServer()
    {
        string address = ConfigurationManager.AppSettings["RemoteWebDriverAddress"];
        string port = ConfigurationManager.AppSettings["RemoteWebDriverPort"];

        var request = (HttpWebRequest)WebRequest.Create(String.Format("http://{0}:{1}/selenium-server/driver/?cmd=shutDownSeleniumServer", address, port));
        request.GetResponse();
    }

You can send a get request to the server which will close the server, I'm not sure of the top of my head why I was having issues with the other methods.

2
  • Thanks for the suggestion, Stuart. I'll give it a shot this morning. If it works, You get a vote! :) Commented Jun 14, 2011 at 12:47
  • 1
    Ok, this worked perfectly. A couple things I had to make sure I was doing right: 1> "shutDownSeleniumServer" starts with a LOWER-CASE "s". 2> I had to remove the conditional in DisposeOfSeleniumServer(), that was checking to make sure seleniumServer was NOT null. I wonder if there's an attribute I could be looking at instead of just the object itself. Anyway, thanks for the help! Commented Jun 14, 2011 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.