| Prev | Next |
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:
Download a nightly snapshot of Selenium RC.
The current release version (0.9.0) does not support Mozilla Firefox 2, for instance.
server/selenium-server.jar to /usr/local/bin, for instance.java -jar /usr/local/bin/selenium-server.jar.Now we can send commands to the Selenium RC server using its client/server protocol.
The PHPUnit_Extensions_SeleniumTestCase test case
extension implements the protocol to talk to Selenium RC as well as
specialized assertion methods for web testing.
Example 17.1 shows how to test the
contents of the <title> element
of the http://www.example.com/ website.
Example 17.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');
$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 17.1 for the list of
methods that are available for this.
Table 17.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. |
Table 17.2 lists the various assertion
methods that PHPUnit_Extensions_SeleniumTestCase provides.
Table 17.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 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. |
Please refer to the documentation of Selenium commands for a reference of the commands available and how they are used.
| Prev | Next |
Copyright © 2005-2010 Sebastian Bergmann.