Selenium Remote Control on Linux

This a tutorial about front-end unit testing using Selenium Remote Control and Firefox on a headless Linux server.

  • Selenium Remote Control
  • Firefox
  • Xvfb (X Windows Virtual Frame Buffer)
  • PHPUnit

Install Selenium

$ wget
$ unzip -d /opt/selenium-remote-control-1.0.3
$ ln -s /opt/selenium-remote-control-1.0.3 /opt/selenium

Install Firefox

$ wget
$ tar xjf firefox-3.6.3.tar.bz2
$ export PATH=$PATH:/path/to/firefox/dir

Install Xvfb

$ yum install Xvfb 

Install PHPUnit

$ pear channel-discover
$ pear install channel://

Ant's Build File

Copy / paste as build.xml

<project name="apw" default="build" basedir=".">

<target name="start-selenium">
<exec executable="Xvfb" spawn="true">
<arg line=" :0 -ac -noreset -screen 0 1024x768x24 -sp /etc/X11/fs/config"/>
<exec executable="java" spawn="true">
<env key="DISPLAY" value=":0"/>
<arg line="-jar /opt/selenium/selenium-server-1.0.3/selenium-server.jar -log /tmp/selenium.log"/>

<target name="stop-selenium">
<get taskname="selenium-shutdown"
dest="/tmp/result.txt" ignoreerrors="true" />
<echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />

PHPUnit Test Script

Copy / paste as apw.php


require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class APW extends PHPUnit_Extensions_SeleniumTestCase
protected $autoStop = FALSE;
protected $captureScreenshotOnFailure = TRUE;
protected $screenshotPath = '/tmp/screenshots';
protected $screenshotUrl = 'http://localhost/screenshots';

protected function setUp()

public function test_search()
$this->type("search_text", "brakes");
try {
$this->assertTrue($this->isTextPresent("You searched for \"Brakes\" (2 matches)"));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors, $e->toString());
//$this->drivers[0]->captureEntirePageScreenshot($this->screenshotPath . DIRECTORY_SEPARATOR . str_replace('/', '_', $this->getLocation()) . $this->testId . '.png');
$this->drivers[0]->captureEntirePageScreenshot($this->screenshotPath . DIRECTORY_SEPARATOR . rawurlencode($this->getLocation()) . '.png');


$ mkdir /tmp/screenshots

Start Selenium

$ ant start-selenium

Execute Test Script

$ phpunit apw.php


PHPUnit 3.4.12 by Sebastian Bergmann.


Time: 17 seconds, Memory: 7.75Mb

There was 1 failure:

1) APW::test_search
Failed asserting that <boolean:false> is true.

Tests: 1, Assertions: 1, Failures: 1.


$ ls -l /tmp/screenshots

Stop Selenium

$ ant stop-selenium


SecurityPolicy file error

Error message

FreeFontPath: FPE "unix/:7100" refcount is 2, should be 1; fixing.
error opening security policy file /usr/lib64/xserver/SecurityPolicy

You need to specify SecurityPolicy file location by using -sp argument

$ Xvfb :0 -ac -noreset -screen 0 1024x768x24 -sp /etc/X11/fs/config

Suppress exec output

Prevent Ant's <exec> output messages from displaying. Add spawn attribute and set value to true.

<exec executable="java" spawn="true">
<arg line="-jar /opt/selenium/selenium-server-1.0.3/selenium-server.jar -log /tmp/selenium.log"/>

Firefox error

Failed to start new browser session: java.lang.RuntimeException: Firefox 3 could not be found in the path!
Please add the directory containing ''firefox-bin' or 'firefox'' to your PATH environment

Make sure Firefox is accessible and you have properly configured PATH variable.

$ export PATH=$PATH:/path/to/firefox/dir