Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Configuring Headless Server for Selenium Testing

Previously, we discussed getting started with writing and running Selenium tests in PHP. Now we will discuss how to run those tests on a remote VPS which will become our "build server". Selenium really needs to run in a desktop environment and drive an actual browser. This way you will capture occurances of elements not being within view etc. If you've deployed a remote ubuntu VPS, it will most likely be running minimal version of Ubuntu server with no desktop environment. These instructions can help you with getting it to run your selenium tests. Hooking it up so that it automatically runs these tests whenever an event happens is still up to you.

Desktop environments are heavy. Make sure your VPS has more than 1GB of RAM. The more the better.

Setup Script

I run this script on the server to install everything I need. This gives us a desktop on the server than we can connect to and see through VNC.

#!/bin/bash

# Update the server
sudo apt update && sudo apt dist-upgrade -y

sudo apt-get install lxde-core -y

# Install vnc server which will install xserver and allow us to vnc in.
apt-get install xorg tightvncserver -y

# Install firefox which we will be using for the selenium tests
sudo apt-get install firefox -y

# Install java which the selenium server requires
sudo apt-get install openjdk-8-jre -y

# Install php for our tests which are written in PHP
sudo apt-get install php7.0-cli -y

# Install the firefox driver which the tests will use to drive the browser
wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar --extract --gzip --file gecko*
sudo mv -i geckodriver /usr/bin/.

Now whenever the build server needs to run tests, it runs the script below.

#!/bin/bash
if ! [ -n "$BASH_VERSION" ];then
    echo "this is not bash, calling self with bash....";
    SCRIPT=$(readlink -f "$0")
    /bin/bash $SCRIPT
    exit;
fi

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

# Run your own commands here
# for building and deploying your codebase,
# preferably through docker

# Start server in 1280 x 1024 resolution.
tightvncserver :1 -geometry 1280x1024

# Run the selenium server in the background
java -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false &

# Sleep for a sec so that the selenium server has time to initialize
sleep 3

# Run the selenium tests tests
/usr/bin/php myPhpTestsEntryPoint.php

# Stop the selenium and vnc servers
killall java
tightvncserver -kill :1

References