Skip to content

Selenium Server as Windows Service via NSSM

by on May 22, 2013

In this blog post you will find a convenient batch-script to install (and update) Selenium WebDriver Server as Windows Service using NSSM – the Non-Sucking Service Manager, tested with Windows 7 64bit.

Actually searching through the internet you will find quite many postings on how to install Selenium Server as Windows Service. Most of them either refer to the Windows Server 2003 Resource Kit Tools and srvany.exe or to AlwaysUp from Core Technologies Consulting.

srvany.exe is quite obviously a hack – and from what I have read won’t work with 64bit systems. AlwaysUp seems to have a nice UI and feature like email alerts in case of problems. Nevertheless you have to purchase licenses to run AlwaysUp.

So we found a solution which uses a free tool: NSSM – the Non-Sucking Service Manager. And in order to have an easy update process for Selenium Server I wrote a Batch Script, which basically performs these steps:

  1. Stop any possibly still running Selenium Server Service (sc),
  2. remove the service (nssm),
  3. install the new Selenium Server as service (nssm),
  4. configure the service to interact with the Desktop (sc) and
  5. start the service (sc).

The interesting part are actually the version numbers you set at the top of the script. Having them it is a piece of cake to update to a new Selenium Server version – which is a good thing due to their frequent updates.

Below you will find the stripped version of the batch-file. The full version with some convenience checks can be downloaded as Gist from GitHub.

@echo off
set SERVICE_NAME=Selenium Server
set NSSM=C:\nssm-2.16\nssm-2.16\win64\nssm.exe
set JAVA=%ProgramFiles%\Java\jre7\bin\java.exe
set SERVER_BASENAME=selenium-server-standalone
set IESERVER_BASENAME=IEDriverServer_Win32
sc stop "%SERVICE_NAME%" >NUL 2>NUL
"%NSSM%" remove "%SERVICE_NAME%" confirm > NUL 2>NUL
"%NSSM%" install "%SERVICE_NAME%" "%JAVA%" -jar ""%SERVER_JAR%""""%IESERVER_EXEC%"" -timeout 120 -browserTimeout 120
if not errorlevel 1 (
   sc config "%SERVICE_NAME%" type= own type= interact
   sc start "%SERVICE_NAME%"

See Also

From → Dev, Testing

  1. This posting raised a discussion at Twitter (this and that) with Adam Goucher and Jim Evans (both project leads at Selenium). Main statement: Starting Selenium Server as Windows server is a (to quote Jim) spectacularly bad idea.

    They asked for the actual use case why we have chosen this approach. Doing so in Tweets is a bit restricted. First of all (I think this is what most people want to achieve) is that Selenium starts automatically after system restart. Adam suggested to use auto-login and auto-start to achieve this. This would help – if this would be the only use case.

    Some Painful History

    We started some time ago (started counting in years) with Selenium 1 and soon switched to Selenium WebDriver. During this history we especially observed:

    Unstable tests, which run fine when opened a session via remote desktop (we called it the Heisenbug referring to the Heisenberg/Uncertainty principle).

    It took us a long time to note that the actual problem was the last one who logged in via RDP. The screen resolution also influenced the screen resolution of the tests which started next – and screenshots on failures were always black afterwards. The root cause might have been that these machines were virtual machines thus not having any monitor attached.

    The solution we found: We wrote a batch-script which needed to be started by anyone who logged in via RDP to the GUI-test-machine. The main trick of that script was to shutdown Selenium, kill the session of the currently logged in user (tscon did the trick here as describe here) and immediately afterwards start Selenium Server again.

    This became good practice over years. But still we had unstable tests. We spent a lot of effort in a framework to cope with most of the problems – and as additional effort we ran a cleanup job regularly which killed all left over sessions which exist for some unknown reason). But Windows is not good in selecting tasks to kill – so that we ran into some problems there, too.

    Final conclusion (up to now – perhaps I need to revert) was to start Selenium as service. Having this it is a piece of cake to start and stop the server – and current observation is, that the tests even run more robust than ever before.

    So thanks to Adam’s and Jim’s comments we will at least carefully observe if this (although discouraged) approach is of any help for us.

  2. Amazing post, im going to try it.

  3. myo permalink

    I have one runnable jar file and want to schedule this task. What is the service name that would be used? Is it the service I created using batch file that invokes from Window Scheduler? Actually after doing these steps, I run the batch like yours, it didn’t invoke the service and didn’t start the selenium server. what am I missing?

    • The service name will be “Selenium Server”. The server should be started if the installation of the service ran without error (see the errorlevel check).

      For analysis I recommend: Use the linked GIST version (if you did not already do that) as it has some convenience checks to warn on possible issues before actually starting the service. If this does not provide more information, remove the “echo off” to see what will be actually executed.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s