By matias.astor

When it comes to functional testing, please have a look at Selenium, a testing framework that provides tools and asserts on a webpage running on a web browser.

The aim of this article is to show you how to set up a Selenium server, with or without Xserver, and run PHP unit tests on a web page.

Before starting you will need:

  • Linux running machine (with or without Xserver)
  • Java
  • Selenium server jar file (can be found here)

You will learn how to install the following:

  • Xvfb (check this) [just if we dont have a Xserver]
  • PHPUnit – PHP – Pear – Selenium TestCase extension (PHPUnit_Extensions_SeleniumTestCase)

I am going to use Fedora for this example, but bear in mind that the only thing that might change in other Linux ditros could be the package manager.

After downloading the Selenium server jar file, and installing Java, you will need to follow these steps:

Xvfb:

[box]➜ ~ sudo yum install xvfb[/box]

Now that you’ve got it, you will need to start it up like this:

[box]➜ ~ Xvfb :99 -screen 0 800x600x16[/box]

Then, you have to export an environment variable called Display and set the value :99.

[box]➜ ~ export DISPLAY=:99[/box]

At this point, you can start the Selenium server. You can found it here.
Please note that in the following example, I have renamed the Selenium server jar to selenium-server.jar.

[box]➜ ~ java -jar selenium-server.jar[/box]

Now that it’s launched, you might go ahead and develop some tests in PHPUnit.

Remember that you will need to have PHP, Pear, PHPUnit and PHPUnit Selenium’s extension to get this working.

[box]➜ ~ sudo yum install php php-pear[/box]

Now you have to update pear.

[box]➜  ~  sudo pear upgrade pear[/box]

After that, you need to set auto_discovery to 1, and install PHPUnit extension:

[box]

➜  ~  sudo pear config-set auto_discover 1

➜  ~  sudo pear install pear.phpunit.de/PHPUnit

[/box]

Next, you can install the Selenium PHPUnit extension.

[box]➜ ~ sudo pear install phpunit/PHPUnit_Selenium[/box]

Alright, we should be fine to start writing some tests, let’s check an example (taken from here, and modified so it works for the example);

Imagine you have a test that has failed, and you want to know where it has failed. Remember you are on a headless server, so you are not seeing what the explorer is actually doing. Selenium can show you where the test has failed, by taking a screenshot.

In the next example I will show you how to take a screenshot when a test fails.

You will see, it’s pretty simple.

Here are some more advantages of using Selenium:

  • Run functional tests on a website, going either forward or backwards.
  • Check through an API if the data introduced in a form is correct! (not the actual point of Selenium, but it could be useful sometimes…).
  • Work with almost any language you need and use different drivers. For example you can work on PHP (php-webdriver-bindings [using Selenium 2 – webdriver -], PHPUnit Extension [which I explained above, using Selenium 1], Facebook’s webdriver [Using Selenium 2 -webdriver-]), on Ruby (A LOT of integrations here!), and on Java, etc, etc.

That’s all for now, please let me know if you have any questions!