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.
pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
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.
class Example extends PHPUnit_Extensions_SeleniumTestCase
protected function setUp()
public function testMyTestCase()
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.
<var name="url" value="http://www.hashbangcode.com"/>
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.
protected function setUp()
$url = $GLOBALS['url'];
Submitted by Peter Kehl on Mon, 02/03/2014 - 23:24Permalink
Submitted by rosebk on Tue, 12/26/2017 - 06:26Permalink
Submitted by sabeer on Mon, 03/12/2018 - 10:05Permalink
Add new comment