Running Selenium RC Automated Tests Automatically Without a Windows Service

We have a suite of regression tests that we wanted to run automatically after our MSbuild build is complete. I’ve implemented these tests as C# code exported from Selenium IDE and preprocessed by some .NET code that I wrote. The tests are then compiled using MSBuild and executed.  The problem I ran into that wasn’t clearly documented anywhere was how to run a Selenium RC test as part of a build process – which requires the Java based Selenium RC server running on port 4444. Since we run our test as part of a scheduled task, how do we maintain the Java Selenium server running across logins of the scheduled task?

My first instinct was to run the Selenium server as a Windows service and I found a forum post that provided a .NET service to do this. The problem is that when the service runs in the LocalSystem’s account context, IE was problematic for enabling the proxy that Selenium RC uses during the invocation of tests.  Trying to modify the service to run as a legitimate user also didn’t work – since the service was running in a different Windows Station and IE did not run correctly in this mode either. 

It turned out the simple solution was to just invoke the Selenium Server from the build batch file and then terminate the server when I was finished with the test. There is a built in command in the command line interpreter named "taskkill" that does the job for me. Here is the .bat file code that I used – and note that the first line kills any java apps that are running so be careful.  I needed this line to abort a server in progress if the script or tests failed and left a java session hanging. My build machine doesn’t have any other java things going on so this worked for me. I’m using nunit to invoke my C# tests contained in "jalisngtests.dll". I’m also using the "sleep" command from the Windows 2003 Resource Kit.

taskkill /f /im java.exe
sleep 5
start "Selenium Server" java ^
-jar C:\selenium-remote-control-1.0-beta-1\^
selenium-server-1.0-beta-1\selenium-server.jar
copy .\bin\debug\*.dll
sleep 20
nunit-console jalisngtests.dll
taskkill /f /fi "WINDOWTITLE eq Selenium Server"

192.168.18.125
13.59.197.213

6 thoughts on “Running Selenium RC Automated Tests Automatically Without a Windows Service”

  1. If you double click the selenium-server.jar file in windows, it will launch a windowless process for selenium server. I use selenium to automate a process right now that runs at regular intervals throughout the day,a nd i need to ensure that the selenium server is running whenever a certain class in my app performs work. Here’s how I’ve solved the problem:

    1. Create a .bat file with “selenium-server.jar” as the only command
    2. Constructor of the (C#/.NET2.0) class that needs Selenium executes the .bat file using System.Diagnostics.Process class

    After doing this a new java.exe process appears in windows task scheduler. If I run the selenium-server.jar several times, only a single process ever appears in the task list, and if i run from the command prompt using java -jar selenium-server.jar it seems to prevent itself from running if another instance of the server is already running. So I’m assuming that the 2-step method above is only launching a single instance of the server. My app has been running fine like this for almost a year, and the selenium-server.jar and starter .bat file get compiled and deployed with the app as part of the build.

  2. More detail about the method i described…

    This way i don’t have to worry about killing the right/wrong task, or what other java apps may be running on the same machine. It’s fine with me if i leave that server instance running after my app is done, since that same instance will be around if I run the app again.

  3. Although I haven’t used Cruise Control, I don’t think there would be any problem using the batch file to start up the Selenium server.

  4. Hey,

    I have used following below code in a notepad and saved it as batch file.
    selenium-server-standalone-2.7.0.jar

    When i executed the batch file a cmd prompt is just flashing and leaving. Please suggest what else can be done to start the selenium server using the batch file?

  5. Its been a while since I’ve last run the bat file, but you need to check that the java server is up and running. Check the task manager and verify that Java.exe is actually running by commenting out the taskkill at the end of the bat file and verifying that your selenium test can connect to the java server on the default port after the batch file is run. If it can’t connect then you need to check your installation of the Java run time to make sure it is in your path by typing “java” at the command prompt and that you Java responding with its help info.

Leave a Reply