Using Selenium Server 2 and PHPUnit to automate browser acceptance testing

I’m writing this so I don’t have to go through it all again! This is how I set up my Debian Squeeze development server to handle automated browser acceptance testing with PHPUnit and Selenium.

As a quick side note, I run my development linux server in a VMWare virtual machine with all the virtual host directories pointing to local shared folders on my Mac. Also all of the following is executed as root, this is how I run my development environment as it makes life simple and does not need high levels of security.

Setting it all up

1) First thing is to install java so we can run Selenium 2

apt-get install openjdk-6-jdk

2) Next download Seleniun Server and place it somewhere you want to run it from (I placed it in my shared vmware virtual hosts root directory so I can run it from my server).

3) Install Firefox. This next step was the one that took me longest to get fully working. We need to install a web browser on the linux server so Selenium has a browser to launch instances of for our testing. Selenium comes with a WebDriver for Firefox and this is it’s default. I tried getting Chrome setup but never got it fully working.

So change to your home directory then download and install Firefox, this downloads Firefox 18 – the current release for Linux.

# change to home and make a tmp directory
cd ~/
mkdir tmp

# download firefox 

Now install it.

# this installs it to the /opt directory
tar -jxvf firefox-18.0.tar.bz2 -C /opt

4) Tell Selenium where Firefox is. Selenium will look for the firefox binary in the server $PATH variable so we need to add it there. I did this by creating a .bash_profile file for root and adding the /opt directory to the $PATH variable.

nano ~/.bash_profile

# then add this line and save the file

For this to take effect you will need to log out and log in again – or restart linux, which is what I did.

5) Set up the DISPLAY. As this is a server there is no desktop environment and no display. This created all sorts of problems for me when I was trying to get firefox to run. If you run the Firefox binary now:


you will get an error about no display. So to fix this we need to install a virtual display.

apt-get install xvfb

Here is more about Xvfb. That’s it for now but we will need to know more about where Xvfb is and how to use it when we come to actually run the testing.

The PHP bit

I’m assuming PHPUnit is already setup for this, so now we need to interface with the Selenium WebDriver with PHP so we can create instances and do some testing with PHP. Luckily a clever chap at Facebook has written a neat little library that does exactly that. Download the php-webdriver library and put it in your project (I like mine in the tests directory in a separate directory called lib). Now we can write a small test like this simple one:

require_once(__DIR__ . '/lib/php-webdriver-master/__init__.php');

class acceptanceTestCase extends PHPUnit_Framework_TestCase {

     * @var WebDriverSession
    protected $_session;

    public function setUp() {
        $web_driver = new WebDriver();
        $this->_session = $web_driver->session();

    public function tearDown() {


    public function test_homepage_goes_to_blog() {

        $this->_session->element('id', 'blogLink')->click();



Make sure you are including the correct version of the php-webdriver library you downloaded.

I’m not going to go into the details of the webdriver API but this simply checks that when the link with an id of “blogLink” is clicked it takes the user to the /blog/ page.

If you try and run this test now it will fail. We need to start the Selenium server.

Starting Selenium

First we need to find out where Xvfb was installed, then  we will create a “display” and then start the Selenium server. Once this is done we can run the tests.

# get the path to Xvfb
whereis Xvfb

# probably this: /usr/bin/Xvfb
# now create a screen
/usr/bin/Xvfb :1 -screen 0 1024x768x24 &

# export it
export DISPLAY=:1

This step will stop Firefox from error-ing.

Now finally we can start Selenium and then start writing some tests. So change to the directory you downloaded the Selenium server to and run this:

java -jar selenium-server-standalone-2.28.0.jar

Be sure that the version and file name match the one you downloaded. You should see some output like this:

INFO: Launching a standalone server
10:42:35.056 INFO - Java: Sun Microsystems Inc. 14.0-b16
10:42:35.057 INFO - OS: Linux 2.6.32-5-686 i386
10:42:35.068 INFO - v2.28.0, with Core v2.28.0. Built from revision 18309
10:42:35.183 INFO - RemoteWebDriver instances should connect to:
10:42:35.184 INFO - Version Jetty/5.1.x
10:42:35.185 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
10:42:35.185 INFO - Started HttpContext[/selenium-server,/selenium-server]
10:42:35.186 INFO - Started HttpContext[/,/]
10:42:35.200 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@ab95e6
10:42:35.200 INFO - Started HttpContext[/wd,/wd]
10:42:35.203 INFO - Started SocketListener on
10:42:35.203 INFO - Started org.openqa.jetty.jetty.Server@1cafa9e

If you now run the PHUnit test you created you should see some output and hopefully your test succeeds! If you get exceptions relating to Not Found Elements on the page then don’t forget that you need to have the correct entries in the Debian servers host file for the local domains you are testing. For example:

nano /etc/hosts

# contents of hosts file host.local


I’m really excited to get this up and running. There is loads of scope here for automated acceptance testing. Selenium Server 2 is pretty awesome and can also take screen shots of the pages. Currently I’m using this to generate screenshots when tests fail but you could use it for all sorts including manually checking certain tests. The API is good and you can interact with the webpage in every way any normal user can, and that even includes javascript!

0 Responses to Find and replace html links with PHP

  1. Dave Harris says:

    Nice walk through, really handy, will take for a test drive soon.

    One thought – I’ve been avoiding using the openjdk since I had a whole day of head scratching trying to get some automated browser screen capture scripts working. The solution was to use the Sun/Oracle version of Java, this was a couple of years ago (2010) and was one of the reasons I used Ubuntu rather than Debian for a server as at the time as it was only apt-get able on Ubuntu. Pretty sure there’s a squeeze release now though. If some bits don’t work it could very likely be that there’s still some incompatibility with openjdk.

    • Kevin says:

      Yeh I’m going to be re-visting this soon and setting up a Vagrant / Puppet config to use with provisioning VM’s. I’m on wheezy now and although when I did this it was on Squeeze I didn’t have any problems with the java open jdk but I think you are probably right about the oracle version, I remember reading something similar somewhere.

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>