Headless Selenium Testing on Ubuntu Server: Running Firefox GUI Tests Without X11


2 views

Running Selenium tests that require browser interaction on a headless Ubuntu server presents unique challenges. Traditional Selenium RC tests launch actual browser instances, which typically require a display server (X11) and graphical environment. Here's how to achieve this without installing a full desktop environment.

The most efficient approach is using Xvfb (X Virtual Frame Buffer), a display server that performs all graphical operations in memory:

sudo apt-get update
sudo apt-get install -y xvfb firefox

After installing prerequisites, you'll need to set up your test execution environment. Create a shell script to manage the virtual display:

#!/bin/bash
# Start Xvfb
Xvfb :99 -screen 0 1024x768x16 &> /dev/null &
export DISPLAY=:99

# Run your Selenium test
java -jar selenium-server-standalone.jar -htmlSuite "*firefox" "http://your-test-site.com" "/path/to/test-suite.html" "/path/to/results.html"

# Clean up
killall Xvfb

For Python users, here's how to integrate Xvfb with Selenium WebDriver:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

try:
    driver = webdriver.Firefox()
    driver.get("http://www.example.com")
    # Your test actions here
finally:
    driver.quit()
    display.stop()

For newer Firefox versions (v56+), you can run in pure headless mode without Xvfb:

from selenium import webdriver

options = webdriver.FirefoxOptions()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
driver.get("http://www.example.com")

When running tests on CI servers, consider these optimizations:

  • Disable images: profile.set_preference('permissions.default.image', 2)
  • Use a RAM disk for temporary files
  • Limit browser window size to reduce rendering overhead

If you encounter problems, check these common solutions:

# Ensure proper DISPLAY environment variable
echo $DISPLAY

# Verify Xvfb is running
ps aux | grep Xvfb

# Check Firefox version compatibility
firefox --version


Running Selenium tests that require browser GUIs on a headless Ubuntu server presents unique challenges. The traditional approach of installing full desktop environments like Gnome or KDE defeats the purpose of using lightweight server installations. Here's how to implement a proper headless testing solution.

Xvfb (X Virtual Frame Buffer) provides a perfect solution by creating a virtual display where GUI applications can run without physical hardware:

sudo apt-get install xvfb firefox
Xvfb :99 -ac -screen 0 1024x768x24 &
export DISPLAY=:99

Here's how to launch Selenium Server with the virtual display:

java -jar selenium-server-standalone.jar -firefoxProfileTemplate /path/to/profile

Create a custom Firefox profile for stability:

firefox -CreateProfile seleniumProfile

While Xvfb works well, you might consider more modern alternatives:

sudo apt-get install phantomjs

Here's a full working example using Python and pyvirtualdisplay:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

try:
    driver = webdriver.Firefox()
    driver.get("http://www.google.com")
    print(driver.title)
finally:
    driver.quit()
    display.stop()

For even cleaner isolation, consider Docker containers with pre-configured environments:

docker run -d -p 4444:4444 selenium/standalone-firefox

When running headless tests:

  • Allocate sufficient memory (at least 1GB for Firefox)
  • Disable images for faster execution: profile.set_preference('permissions.default.image', 2)
  • Use explicit waits instead of fixed sleeps

Common problems and solutions:

# If getting "no display specified" error:
export DISPLAY=:99

# For Firefox crashes:
sudo apt-get install libdbus-glib-1-2