Deerwalk Blog

Selenium with PHPUnit

Posted by Jeevan Timilsina on September 02, 2011



is a portable software testing framework for web applications. Selenium provides a record/playback tool for authoring tests without learning a test scripting language. Selenium provides a test domain specific language (DSL) to write tests in a number of popular programming languages including C#, Java, Groovy, Perl, PHP, Python and Ruby. Test playback is possible in most modern web browsers. Selenium deploys on Windows, Linux, and Macintosh platforms.

Selenium IDE

is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run.

Selenium IDE is not only a recording tool: it is a complete IDE. You can choose to use its recording capability, or you may edit your scripts by hand. With auto complete support and the ability to move commands around quickly, Selenium IDE is the ideal environment for creating Selenium tests no matter what style of tests you prefer.

Selenium Remote Control

(RC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser.

Selenium RC comes in two parts.

1. A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them.

2. Client libraries for your favourite computer language.

The RC server also bundles Selenium Core and automatically loads it into the browser.

Below is a simplified architectural representation...


is a regression testing framework used by the developer who implements unit tests in PHP.

Downloading and Installing

1.) Selenium IDE

For use with Firefox, download the IDE from the Selenium downloads page ( Then install the Selenium-IDE add-on on your Firefox browser. After the installation completes you can run the IDE to create test cases. To run the Selenium-IDE, simply select it from the Firefox Tools menu. It opens with an empty script-editing window and a menu for loading, or creating new test cases.

2.) Selenium RC

- Make sure that you have Java Runtime installed on your machine.

- Download Selenium RC from downloads page (

- After extracting the files from the archive, copy the 'selenium-server.jar' file to any location of your choice.

- Start the Selenium RC server from the command-line by issuing the following command: java -jar selenium-server.jar

This will start the server on port 4444.

- Now the server is ready to accept test commands from your PHP script. Make sure you keep this server running till you finish testing.

3.) PHPUnit

-An easy way to install PHPUnit is to use the PEAR installer. First of all install the latest version of PEAR by supplying following command :

pear install PEAR-1.9.2

-The PEAR channel ( is used to distribute PHPUnit so make sure that it is registered with your local PEAR environment:

pear channel-discover

-After the channel is registered install PHPUnit:

pear install phpunit/PHPUnit

-Finally upgrade the PHPUnit to its latest version.

pear upgrade phpunit/PHPUnit

Running a simple test using Selenium IDE

-Start Selenium IDE in Firefox: Tools->Selenium IDE

-Click on the red record button on the right.

- Browse to

- Browse the page and all the links and buttons you clicked will be recorded.

- Stop the recording by clicking on the record button.

If you click on the ‘Source’ tab of the IDE you can see the test.html generated by selenium.

The ‘table’ tab shows the commands recorded by Selenium.

- Open a new tab in Firefox and click on the Selenium IDE’s play button to run the recorded test.

- The IDE should play your recorded test. The IDE after the test run is shown below. In the ‘Log section’ you can see the various events run by the test. In the table tab you can see that all the rows are green, which means that the test ran successfully.

The interesting part follows – running the same test from PHP. From the file menu, select ‘Export Test Case As…’ and as we are using PHP, select ‘PHP – PHPUnit’. Save the file by the name ‘Example.php’.

This is what we get in Example.php

class Example extends PHPUnit_Extensions_SeleniumTestCase


protected function setUp()





public function testMyTestCase()



$this->click("link=Services »");


$this->click("link=Careers »");




$this->click("link=Case Studies");





Actual testing

Now that PHPUnit is installed and the Selenium RC server is up and running, it’s time to run our test we saved before in our ‘Example.php’ file. Type the following on the command-line:

phpunit Example

This will start the test. The PHPUnit Selenium driver will execute each test command from your file and send it to the Selenium server, which does the job of launching the appropriate browser, opening web pages, and performing various specified actions; and closing the browser after the test completes.

Now to test the same test case in another browser you simply need to set Browser to the one you want the test to run.

Supported browsers include:



















“Selenese” Selenium Commands



is what tells Selenium what to do. Selenium commands come in three 'flavors':








are commands that generally manipulate the state of the application. They do things like "click this link" and "select that option". If an Action fails, or has an error, the execution of the current test is stopped.

Many Actions can be called with the "AndWait" suffix, e.g. "clickAndWait". This suffix tells Selenium that the action will cause the browser to make a call to the server, and that Selenium should wait for a new page to load.


examine the state of the application and store the results in variables, e.g. "storeTitle". They are also used to automatically generate Assertions.


are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include "make sure the page title is X" and "verify that this checkbox is checked".

All Selenium Assertions can be used in 3 modes: "assert", "verify", and "waitFor". For example, you can "assertText", "verifyText" and "waitForText". When an "assert" fails, the test is aborted. When a "verify" fails, the test will continue execution, logging the failure. This allows a single "assert" to ensure that the application is on the correct page, followed by a bunch of "verify" assertions to test form field values, labels, etc. "waitFor" commands wait for some condition to become true (which can be useful for testing Ajax applications). They will succeed immediately if the condition is already true. However, they will fail and halt the test if the condition does not become true within the current timeout setting.

Commonly Used Selenese


opens a page using a URL.


performs a click operation, and optionally waits for a new page to load.


verifies an expected page title.


verifies expected text is somewhere on the page.


verifies an expected UI element, as defined by its HTML tag, is present on the page.


verifies expected text and it’s corresponding HTML tag are present on the page.


verifies a table’s expected contents.


pauses execution until an expected new page loads. Called automatically when clickAndWait is used.


pauses execution until an expected UI element, as defined by its HTML tag, is present on the page.


Subscribe to Blog Updates

Posts by Topic

see all