Executing Selenium tests on a non-logged in Windows machine

One of the obstacles when doing GUI based test automation is that you need a user desktop context to run the tests. Very few organizations appreciate having to set up GPOs for machines to remove screensaver screenlocks.
Very few testers like having to regularly log in to a machine after each reboot or having scripts fail due to machine restarts leaving the machines without anyone logged in to them.

Luckily there are a few alternative ways to manage this. Here I'll tell about one very robust approach.

General concept description

To enable something to run without anyone logged in to a Windows machine we have to run code as Windows Services.
Windows Services has an option to allow them to interact with a desktop - essentially making them run in a desktop environment.
Then the Windows Service could be set to start with the Windows TaskScheduler, or like in this case - through Selenium Grid.

Pros

  • Works un-attended even when server/client is restarted
  • No-one has to be logged in to the server/client
  • You may choose user for execution
  • Approach works with most types of GUI automation
  • Robust

Cons

  • Desktop screenshots will be blank
  • Browser screenshot for some Selenium drivers will be blank
  • For one step in installation you'll need administrative rights

How-to

Procedure to get Selenium Grid up and running at new server:

1). Prepare for creation of Windows Services

Downloaded Windows Resource Kit 2003 from https://www.microsoft.com/en-us/download/details.aspx?id=17657 to client machine. This package include files that are very useful for creating Windows Services.
If you want to install the resource kit you may do this, but we only need a few of the files in this. So just use a program like 7-zip to open the installation package and extract the MSI file from the Windows Resource Kit 2003 installation file, and then 7-zip again to extract all the files from the msi file into a local folder (in our case C:\SeleniumGridService\WRK).

We'll only use two of the files in the Windows Resource Kit:

2). Prepare for running the SeleniumStandaloneServer.jar file

If no JRE is installed: create a Java JRE folder called C:\SeleniumGridService\JRE and made sure it contained a valid JRE for the Selenium Standalone Server jar file.

Download the latest version of the Selenium Standalone Server jar file to the C:\SeleniumGridService folder and remove the version part of the file name for ease of use.
If not removed we have to use RegEdit.exe to alter the Windows Services for each update of Selenium Standalone Server.

Download relevant Selenium drivers (e.g. chromedriver.exe, and IEDriverServer.exe) and copy them to the C:\SeleniumGridService folder.

3). Prepare creation of a Windows Service for the Selenium Grid Hub component

Selenium Grid is used by running Selenium scripts towards a RemoteWebDriver at a component called Selenium Grid Hub. This hub then identifies a suitable node from the filter parameters given (browser, OS, and so forth) and executes the test on this node.

In preparation to enter registry parameters for the service for Selenium Hub to be run a file called C:\SeleniumGridService\SeleniumHub.reg with the following content was created (note the double backslashes needed for UNC path definitions):


        Windows Registry Editor Version 5.00
        [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SeleniumHub\Parameters]
        "Application"="C:\\SeleniumGridService\\JRE\\bin\\java.exe"
        "AppDirectory"="C:\\SeleniumGridService\\"
        "AppParameters"="-Xrs -jar C:\\SeleniumGridService\\selenium-server-standalone.jar -role hub"
                

When executed this file will configure the Windows Service called SeleniumHub to use the specified java.exe file to run the Selenium-server-standalone.jar file as a hub.

4). Prepare creation of Windows Service for running a Selenium Node

Each web browser you would like to support on your Selenium Grid solution should have a separate Windows Service running for ease of maintenance.

In preparation to enter registry parameters for the service for Selenium Node to be run a file called C:\SeleniumGridService\SeleniumNode.reg with the following content was created (note the double backslashes needed for UNC path definitions):


        Windows Registry Editor Version 5.00
        [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SeleniumNode\Parameters]
        "Application"="C:\\SeleniumGridService\\JRE\\bin\\java.exe"
        "AppDirectory"="C:\\SeleniumGridService"
        "AppParameters"="-Xrs -jar selenium-server-standalone.jar -log SeleniumNode.log -role node -hub http://127.0.0.1:4444/grid/register"
                

When executed this file will configure the Windows Service called SeleniumNode to use the specified java.exe file to run the Selenium-server-standalone.jar file as a node and register it to the hub at the same machine (127.0.0.1 is loopback IP-address for same machine, and 4444 is the standard registration port of the Selenium Grid hub).

5). Create a script to install the services

Created a script to install the service. Script was called C:\SeleniumGridService\SeleniumServerInstall.bat with the following content:


        @echo off
        C:\SeleniumGridService\WRK\instsrv.exe SeleniumHub "C:\SeleniumGridService\WRK\srvany.exe"
        C:\SeleniumGridService\WRK\instsrv.exe SeleniumNode "C:\SeleniumGridService\WRK\srvany.exe"
        REGEDIT.EXE /S C:\SeleniumGridService\SeleniumHub.reg
        REGEDIT.EXE /S C:\SeleniumGridService\SeleniumNode.reg
        echo "Install Complete"
        pause
                

When executed this script will create two Windows Services. One will be called SeleniumHub and one will be called SeleniumNode. These will be configured by the reg files respectively.
At the end of registration (will take a few miliseconds only) the text "Install Complete" will be visible until a key is touched-

6). Install and configure the Windows Services

Run the C:\SeleniumGridService\SeleniumServerInstall.bat as an administrator to create the two services called SeleniumHub and SeleniumNode.

Open the Services control of the Windows Control Panel and made sure boh the services are running and that the SeleniumNode service had the checkbox for option for "Allow service to interact with desktop" checked.

7). Cleanup

Now you may safely remove the C:\SeleniumGridService\WRK folder since the services are already created.

You may also remove the C:\SeleniumGridService\SeleniumServerInstall.bat file.