Using Splinter in headless mode on Linux Servers

How to run your Splinter automated test cases on a Linux server that does not have a UI? Most of my customers use cloud services from AWS, Google Cloud and Azure. They want to run automation from servers deployed on these cloud environments as part of their CI (Continuous Integration) pipeline. To achieve this we use Splinter in headless mode, in short, the Web driver will run the test in the background without opening the browser window.

Starting Splinter in Headless mode

Below is a simple Python example that open the browser in a headless mode and perform a test.

from splinter import Browser

url = "https://google.com"

browser = Browser('chrome', headless=True)
browser.visit(url)
assert browser.title == "Google"
browser.quit()

The above code will open Chrome browser in the background, load the web page and test the assertion, in our case, compare the title of the page. The same works for Firefox as well

The above code was tested on Ubuntu 18.04 server.

Installing Chrome and Chromedriver in Ubuntu 18.04 server

Below are the instructions to install chrome and chrome driver on Ubuntu 18.04

$ sudo apt update
$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
$ sudo dpkg -i google-chrome-stable_current_amd64.deb

If your install fail with errors, then run the below commands:

$ sudo apt install -f
$ sudo apt update --fix-missing
$ sudo apt --fix-broken install
$ google-chrome --version
Google Chrome 75.0.3770.142

We have to download the same version of Google chrome driver as well.

$ wget https://chromedriver.storage.googleapis.com/75.0.3770.140/chromedriver_linux64.zip
$ sudo apt install unzip
$ unzip chromedriver_linux64.zip

Make sure the extracted chromedriver is on the PATH. Now run the Python code.

Installing Firefox and Gecko driver on Ubuntu 18.04 server

Below are the commands to install Firefox and related Gecko driver on Ubuntu server.

$ sudo apt install firefox
$ wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz
$ tar -xvf geckodriver-v0.24.0-linux64.tar.gz

Make sure geckodriver is on the PATH.

Conclusion

Definitely running Splinter in headless mode will take your automated test cases on to the cloud platforms, which will help in integrating your testing process into your DevOps. Sometimes you need to have more control on the test case execution, for example, recording the complete test execution. We need some more advanced tools like Virtual display, specifically Pyvirtuadisplay, which is a wrapper around Xvfb (X11 with frame buffer) to simulate UI. We will discuss this in future articles. Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *