Testing Websites With Selenium And PHP

16th July 2012 - 6 minutes read time

Selenium is an application that allows automated testing of websites through a browser and consists of a number of different components. It allows the creation of browser tests that perform certain actions, which can then be run again at a later date. Three components are required to allow Selenium to run tests through PHP. These are as follows:

  • Selenium IDE
  • Selenium Server (formerly Selenium RC Server)
  • PHPUnit with the PHPUnit_Selenium extension

The first step is to download the Selenium IDE. This part of Selenium allows you to record actions on a website and run certain tests. These might be things like making sure certain text appears on certain pages, or if a non-logged in use is able to access the administration pages. When you first go to the Selenium downloads page you'll be presented with a whole bunch of different things, which can be a bit bewildering for first time visitors. The Selenium IDE download link is at the top of the page.

When you download the Selenium IDE you'll get a file with a name like selenium-ide-1.8.1.xpi, which is a Firefox extension. You can install it by opening it with Firefox and allowing the extension to be installed. On my system (Kubuntu) I could either drag it from my file explorer to Firefox or right click and select "open with". After restarting Firefox you will see an option to open Selenium IDE in the tools menu.

Once Selenium IDE is installed you'll need to get the Selenium PHP Formatters so that you can export your Selenium tests into PHP. You can find these on the Selenium download page, or via a search in the Firefox extension library. You need to restart Firefox to complete the instillation of these components. You can now open Selenium IDE and start creating your test cases.

Once you have a Selenium test case you can export it into a PHPUnit test class by going to File > Export Test Case As and selecting PHP (PHP Unit) from this list of available formatters. You'll also notice an option for exporting the tests as PHP (Testing_Selenium), which is a different way of using Selenium in PHPUnit but doesn't use the PHPUnit_Selenium extension. When you select PHP (PHP Unit) from the list you'll create a file containing your Selenium test as a PHPUnit test.

Before you can run the tests through PHPUnit you'll need to install PHPUnit and the PHPUnit_Selenium extension. These can both be done through pear via the following commands.

  1. pear config-set auto_discover 1
  2. pear install pear.phpunit.de/PHPUnit
  3. pear install phpunit/PHPUnit_Selenium

You can now run the PHPUnit test file as you normally would, but you'll notice that when you do it skips all tests. What's going on here? Well in order to run the tests you need the Selenium Server to be running and without this in place PHPUnit will gracefully skip over the tests, rather than fail them. The Selenium Server is a Java file that you can download from the same Selenium downloads page that you got the Selenium IDE from. You can optionally put this Java file somewhere convenient like /usr/local/bin. To run the server use the command.

java -jar selenium-server-standalone-2.24.1.jar

When you run the PHPUnit file it will run through your Selenium tests by opening a browser (the default is Firefox) and running through each of the commands and tests one by one.

It's worth looking into the sort of file that is created via the Selenium IDE export. I created a Selenium test case that opened the homepage of #! code, went to the Blog page and made sure that it contained the text "Blog". This is what the exported file looked like.

  1. <?php
  3. class Example extends PHPUnit_Extensions_SeleniumTestCase
  4. {
  5. protected function setUp()
  6. {
  7. $this->setBrowser("*chrome");
  8. $this->setBrowserUrl("http://www.hashbangcode.com/");
  9. }
  11. public function testMyTestCase()
  12. {
  13. $this->open("/");
  14. $this->click("link=Blog");
  15. $this->waitForPageToLoad("30000");
  16. $this->verifyTextPresent("Blog");
  17. }
  18. }

You can see that the setUp() method sets up the browser and the URL to use. The good thing about this is that you can swap out the URL for your development site with a single code change. If you find yourself building Drupal or WordPress sites a lot then you can create a test harness for doing certain things and then just drop these tests into your projects when you are building them to make sure things are working. For example, you might want to test that an anonymous get an access denied page when they try to access the administration pages of the site.

  1. <phpunit>
  2. <php>
  3. <var name="url" value="http://www.hashbangcode.com"/>
  4. </php>
  5. </phpunit>

You can pass in global variables using the variables configuration settings of PHPUnit. If you create a file called phpunit.xml then PHPUnit will pick it up automatically when running the tests.

You can then reference this variable from your setUp() function like this.

  1. protected function setUp()
  2. {
  3. $this->setBrowser("*chrome");
  4. $url = $GLOBALS['url'];
  5. $this->setBrowserUrl($url);
  6. }


Hi Philip, I see that you've used Selenium IDE to create tests and then exported them to RC. However, there's more you can do with IDE now. There's SeLite, a framework for Selenium IDE that allows your test to access (read and write to) a test database (isolated from the database of the tested application). SeLite is ideal for Drupal, since all Drupal text content is in DB. Also, Drupal can work with SQLite DB, which is perfect for SeLite - so if (some of) your test deployments can use SQLite, your test data lifecycle would be very easy. See https://code.google.com/p/selite/wiki/ProjectHome and https://code.google.com/p/selite/wiki/DrupalTutorial.

Peter Kehl (Mon, 02/03/2014 - 23:24)

Great blog.you put Good stuff.All the topics were explained briefly.so quickly understand for me.I am waiting for your next fantastic blog.Thanks for sharing.Any coures related details learn...

rosebk (Tue, 12/26/2017 - 06:26)

Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

sabeer (Mon, 03/12/2018 - 10:05)

Add new comment

The content of this field is kept private and will not be shown publicly.