Prev Next

Chapter 18. PHPUnit and Selenium

Selenium RC

Selenium RC is a test tool that allows you to write automated user-interface tests for web applications in any programming language against any HTTP website using any mainstream browser. It uses Selenium Core, a library that performs automated browser tasks using JavaScript. Selenium tests run directly in a browser, just as real users do. These tests can be used for both acceptance testing (by performing higher-level tests on the integrated system instead of just testing each unit of the system independently) and browser compatibility testing (by testing the web application on different operating systems and browsers).

Let us take a look at how Selenium RC is installed:

  1. Download a distribution archive of Selenium RC.
  2. Unzip the distribution archive and copy server/selenium-server.jar to /usr/local/bin, for instance.
  3. Start the Selenium RC server by running java -jar /usr/local/bin/selenium-server.jar.

Now we can send commands to the Selenium RC server using its client/server protocol.

PHPUnit_Extensions_SeleniumTestCase

The PHPUnit_Extensions_SeleniumTestCase test case extension implements the client/server protocol to talk to Selenium RC as well as specialized assertion methods for web testing.

Example 18.1 shows how to test the contents of the <title> element of the http://www.example.com/ website.

Example 18.1: Usage example for PHPUnit_Extensions_SeleniumTestCase

<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class WebTest extends PHPUnit_Extensions_SeleniumTestCase
{
protected function setUp()
{
$this->setBrowser('*firefox /usr/lib/firefox/firefox-bin');
$this->setBrowserUrl('http://www.example.com/');
}

public function testTitle()
{
$this->open('http://www.example.com/');
$this->assertTitleEquals('Example Web Page');
}
}
?>

Unlike with the PHPUnit_Framework_TestCase class, test case classes that extend PHPUnit_Extensions_SeleniumTestCase have to provide a setUp() method. This method is used to configure the Selenium RC session. See Table 18.1 for the list of methods that are available for this.

Table 18.1. Selenium RC API: Setup

Method Meaning
void setBrowser(string $browser) Set the browser to be used by the Selenium RC server.
void setBrowserUrl(string $browserUrl) Set the base URL for the tests.
void setHost(string $host) Set the hostname for the connection to the Selenium RC server.
void setPort(int $port) Set the port for the connection to the Selenium RC server.
void setTimeout(int $timeout) Set the timeout for the connection to the Selenium RC server.
void setSleep(int $seconds) Set the number of seconds the Selenium RC client should sleep between sending action commands to the Selenium RC server.

You can also run each test using a set of browsers: Instead of using setBrowser() to set up one browser you declare a public static array named $browsers in your test case class. Each item in this array describes one browser configuration. Each of these browsers can be hosted by different Selenium RC servers:

Example 18.2: Setting up multiple browser configurations

<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class WebTest extends PHPUnit_Extensions_SeleniumTestCase
{
public static $browsers = array(
array(
'name' => 'Firefox on Linux',
'browser' => '*firefox /usr/lib/firefox/firefox-bin',
'host' => 'my.linux.box',
'port' => 4444,
'timeout' => 30000,
),
array(
'name' => 'Safari on MacOS X',
'browser' => '*safari',
'host' => 'my.macosx.box',
'port' => 4444,
'timeout' => 30000,
),
array(
'name' => 'Safari on Windows XP',
'browser' => '*custom C:\Program Files\Safari\Safari.exe -url',
'host' => 'my.windowsxp.box',
'port' => 4444,
'timeout' => 30000,
),
array(
'name' => 'Internet Explorer on Windows XP',
'browser' => '*iexplore',
'host' => 'my.windowsxp.box',
'port' => 4444,
'timeout' => 30000,
)
);

protected function setUp()
{
$this->setBrowserUrl('http://www.example.com/');
}

public function testTitle()
{
$this->open('http://www.example.com/');
$this->assertTitleEquals('Example Web Page');
}
}
?>

PHPUnit_Extensions_SeleniumTestCase can collect code coverage information for tests run through Selenium:

  1. Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
  2. In your webserver's php.ini configuration file, configure PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.
  3. In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use
    protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';
    
    to configure the URL for the phpunit_coverage.php script.

Table 18.2 lists the various assertion methods that PHPUnit_Extensions_SeleniumTestCase provides.

Table 18.2. Assertions

Assertion Meaning
void assertAlertPresent() Reports an error if no alert is present.
void assertNoAlertPresent() Reports an error if an alert is present.
void assertChecked(string $locator) Reports an error if the element identified by $locator is not checked.
void assertNotChecked(string $locator) Reports an error if the element identified by $locator is checked.
void assertConfirmationPresent() Reports an error if no confirmation is present.
void assertNoConfirmationPresent() Reports an error if a confirmation is present.
void assertEditable(string $locator) Reports an error if the element identified by $locator is not editable.
void assertNotEditable(string $locator) Reports an error if the element identified by $locator is editable.
void assertElementValueEquals(string $locator, string $text) Reports an error if the value of the element identified by $locator is not equal to the given $text.
void assertElementValueNotEquals(string $locator, string $text) Reports an error if the value of the element identified by $locator is equal to the given $text.
void assertElementContainsText(string $locator, string $text) Reports an error if the element identified by $locator does not contain the given $text.
void assertElementNotContainsText(string $locator, string $text) Reports an error if the element identified by $locator contains the given $text.
void assertElementPresent(string $locator) Reports an error if the element identified by $locator is not present.
void assertElementNotPresent(string $locator) Reports an error if the element identified by $locator is present.
void assertLocationEquals(string $location) Reports an error if the current location is not equal to the given $location.
void assertLocationNotEquals(string $location) Reports an error if the current location is equal to the given $location.
void assertPromptPresent() Reports an error if no prompt is present.
void assertNoPromptPresent() Reports an error if a prompt is present.
void assertSelectHasOption(string $selectLocator, string $option) Reports an error if the given option is not available.
void assertSelectNotHasOption(string $selectLocator, string $option) Reports an error if the given option is available.
void assertSelected($selectLocator, $option) Reports an error if the given label is not selected.
void assertNotSelected($selectLocator, $option) Reports an error if the given label is selected.
void assertIsSelected(string $selectLocator, string $value) Reports an error if the given value is not selected.
void assertIsNotSelected(string $selectLocator, string $value) Reports an error if the given value is selected.
void assertSomethingSelected(string $selectLocator) Reports an error if the option identified by $selectLocator is not selected.
void assertNothingSelected(string $selectLocator) Reports an error if the option identified by $selectLocator is selected.
void assertTextPresent(string $pattern) Reports an error if the given $pattern is not present.
void assertTextNotPresent(string $pattern) Reports an error if the given $pattern is present.
void assertTitleEquals(string $title) Reports an error if the current title is not equal to the given $title.
void assertTitleNotEquals(string $title) Reports an error if the current title is equal to the given $title.
void assertVisible(string $locator) Reports an error if the element identified by $locator is not visible.
void assertNotVisible(string $locator) Reports an error if the element identified by $locator is visible.

Table 18.3 shows the two template methods of PHPUnit_Extensions_SeleniumTestCase:

Table 18.3. Template Methods

Method Meaning
void defaultAssertions() Override to perform assertions that are shared by all tests of a test case. This method is called after each command that is sent to the Selenium RC server.
void sharedAssertions() Override to perform assertions that are shared by all tests of a test case. This method is called before the execution of a test ends.

Please refer to the documentation of Selenium commands for a reference of the commands available and how they are used.

Using the runSelenese($filename) method, you can also run a Selenium test from its Selenese/HTML specification. Furthermore, using the static attribute $seleneseDirectory, you can automatically create test objects from a directory that contains Selenese/HTML files. The specified directory is recursively searched for .htm files that are expected to contain Selenese/HTML. Example 18.3 shows an example.

Example 18.3: Use a directory of Selenese/HTML files as tests

<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class SeleneseTests extends PHPUnit_Extensions_SeleniumTestCase
{
public static $seleneseDirectory = '/path/to/files';
}
?>

Prev Next
1. Automating Tests
2. PHPUnit's Goals
3. Installing PHPUnit
4. Writing Tests for PHPUnit
Data Providers
Testing Exceptions
Testing PHP Errors
5. The Command-Line Test Runner
6. Fixtures
More setUp() than tearDown()
Variations
Sharing Fixture
7. Organizing Test Suites
Suite-Level Setup
8. TestCase Extensions
Testing Output
Testing Performance
9. Database Testing
Datasets
Flat XML Data Set
XML Data Set
Operations
Database Testing Best Practices
10. Incomplete and Skipped Tests
Incomplete Tests
Skipping Tests
11. Mock Objects
Self-Shunting
Stubs
12. Testing Practices
During Development
During Debugging
13. Test-First Programming
BankAccount Example
14. Code Coverage Analysis
Specifying Covered Methods
Ignoring Code Blocks
Including and Excluding Files
15. Other Uses for Tests
Agile Documentation
Cross-Team Tests
16. Logging
XML Format
Code Coverage (XML)
JavaScript Object Notation (JSON)
Test Anything Protocol (TAP)
GraphViz Markup
Test Database
17. Skeleton Generator
Annotations
18. PHPUnit and Selenium
Selenium RC
PHPUnit_Extensions_SeleniumTestCase
19. Continuous Integration
CruiseControl
phpUnderControl
Apache Maven
20. PHPUnit's Implementation
21. PHPUnit API
Overview
PHPUnit_Framework_Assert
PHPUnit_Framework_Test
PHPUnit_Framework_TestCase
PHPUnit_Framework_TestSuite
PHPUnit_Framework_TestResult
Package Structure
22. Extending PHPUnit
Subclass PHPUnit_Framework_TestCase
Assert Classes
Subclass PHPUnit_Extensions_TestDecorator
Implement PHPUnit_Framework_Test
Subclass PHPUnit_Framework_TestResult
Implement PHPUnit_Framework_TestListener
New Test Runner
A. Assertions
B. The XML Configuration File
Test Suite
Groups
Including and Excluding Files for Code Coverage
Logging
PMD Rules
Setting PHP INI settings and Global Variables
C. PHPUnit for PHP 4
D. Index
E. Bibliography
F. Copyright