Source for file web_tester.php
Documentation is available at web_tester.php
* Base include file for SimpleTest.
* @version $Id: web_tester.php 1723 2008-04-08 00:34:10Z lastcraft $
* include other SimpleTest class files
require_once(dirname(__FILE__
) .
'/test_case.php');
require_once(dirname(__FILE__
) .
'/browser.php');
require_once(dirname(__FILE__
) .
'/page.php');
require_once(dirname(__FILE__
) .
'/expectation.php');
* Test for an HTML widget value match.
* Sets the field value to compare against.
* @param mixed $value Test value to match. Can be an
* expectation for say pattern matching.
* @param string $message Optiona message override. Can use %s as
* a placeholder for the original message.
* Tests the expectation. True if it matches
* a string value or an array value in any order.
* @param mixed $compare Comparison value. False for
* @return boolean True if correct.
function test($compare) {
if ($this->_value ===
false) {
return ($compare ===
false);
if ($this->_isSingle($this->_value)) {
return $this->_testSingle($compare);
return $this->_testMultiple($compare);
* Tests for valid field comparisons with a single option.
* @param mixed $value Value to type check.
* @return boolean True if integer, string or float.
function _isSingle($value) {
* String comparison for simple field with a single option.
* @param mixed $compare String to test against.
* @returns boolean True if matching.
function _testSingle($compare) {
if (! $this->_isSingle($compare)) {
return ($this->_value ==
$compare);
* List comparison for multivalue field.
* @param mixed $compare List in any order to test against.
* @returns boolean True if matching.
function _testMultiple($compare) {
$compare =
array($compare);
return ($this->_value ===
$compare);
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
return "Field expectation [" .
$dumper->describeValue($this->_value) .
"]";
return "Field expectation [" .
$dumper->describeValue($this->_value) .
$dumper->describeValue($compare) .
"] " .
$dumper->describeDifference($this->_value, $compare);
* Test for a specific HTTP header within a header block.
* Sets the field and value to compare against.
* @param string $header Case insenstive trimmed header name.
* @param mixed $value Optional value to compare. If not
* given then any value will match. If
* an expectation object then that will
* @param string $message Optiona message override. Can use %s as
* a placeholder for the original message.
* Accessor for aggregated object.
* @return mixed Expectation set in constructor.
* Removes whitespace at ends and case variations.
* @param string $header Name of header.
* @param string Trimmed and lowecased header
function _normaliseHeader($header) {
* Tests the expectation. True if it matches
* a string value or an array value in any order.
* @param mixed $compare Raw header block to search.
* @return boolean True if header present.
function test($compare) {
* Searches the incoming result. Will extract the matching
* @param mixed $compare Raw header block to search.
* @return string Matching header line.
$lines =
split("\r\n", $compare);
foreach ($lines as $line) {
if ($this->_testHeaderLine($line)) {
* Compares a single header line against the expectation.
* @param string $line A single line to compare.
* @return boolean True if matched.
function _testHeaderLine($line) {
list
($header, $value) =
$parsed;
* Tests the value part of the header.
* @param string $value Value to test.
* @param mixed $expected Value to test against.
* @return boolean True if matched.
if ($expected ===
false) {
return $expected->test(trim($value));
return (trim($value) ==
trim($expected));
* Returns a human readable test message.
* @param mixed $compare Raw header block to search.
* @return string Description of success
return "Searching for header [$message] found [$line]";
return "Failed to find header [$message]";
* Test for a specific HTTP header within a header block that
* Sets the field and value to compare against.
* @param string $unwanted Case insenstive trimmed header name.
* @param string $message Optiona message override. Can use %s as
* a placeholder for the original message.
* Tests that the unwanted header is not found.
* @param mixed $compare Raw header block to search.
* @return boolean True if header present.
function test($compare) {
* Returns a human readable test message.
* @param mixed $compare Raw header block to search.
* @return string Description of success
return "Found unwanted header [$expectation] with [$line]";
return "Did not find unwanted header [$expectation]";
* Test for a text substring.
* Sets the value to compare against.
* @param string $substring Text to search for.
* @param string $message Customised message on failure.
* Accessor for the substring.
* @return string Text to match.
* Tests the expectation. True if the text contains the
* @param string $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
* Returns a human readable test message.
* @param mixed $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
$dumper->describeValue($compare) .
"]";
* Describes a pattern match including the string
* found and it's position.
* @param string $substring Text to search for.
* @param string $subject Subject to search.
$position =
strpos($subject, $substring);
return "Text [$substring] detected at character [$position] in [" .
$dumper->describeValue($subject) .
"] in region [" .
$dumper->clipString($subject, 100, $position) .
"]";
* Fail if a substring is detected within the
* Sets the reject pattern
* @param string $substring Text to search for.
* @param string $message Customised message on failure.
* Tests the expectation. False if the substring appears
* @param string $compare Comparison value.
* @return boolean True if correct.
function test($compare) {
return ! parent::test($compare);
* Returns a human readable test message.
* @param string $compare Comparison value.
* @return string Description of success
if ($this->test($compare)) {
$dumper->describeValue($compare) .
"]";
* Test case for testing of web pages. Allows
* fetching of pages, parsing of HTML and
* Creates an empty test case. Should be subclassed
* with test methods for a functional test case.
* @param string $label Name of test case. Will use
* the class name if none specified.
* Announces the start of the test.
* @param string $method Test method just started.
* Announces the end of the test. Includes private clean up.
* @param string $method Test method just finished.
function after($method) {
* Gets a current browser reference for setting
* special expectations or for detailed
* examination of page fetches.
* @return SimpleBrowser Current test browser object.
* Gets a current browser reference for setting
* special expectations or for detailed
* examination of page fetches.
* @param SimpleBrowser $browser New test browser object.
* Clears the current browser reference to help the
* Creates a new default web browser object.
* Will be cleared at the end of the test method.
* @return TestBrowser New browser.
* Gets the last response error.
* @return string Last low level HTTP error.
return $this->_browser->getTransportError();
* Accessor for the currently selected URL.
* @return string Current location or false if
* Dumps the current request for debugging.
* Dumps the current HTTP headers for debugging.
* Dumps the current HTML source for debugging.
* Dumps the visible text only for debugging.
* Simulates the closing and reopening of the browser.
* Temporary cookies will be discarded and timed
* cookies will be expired if later than the
* @param string/integer $date Time when session restarted.
* If ommitted then all persistent
* cookies are kept. Time is either
* Cookie format string or timestamp.
* Moves cookie expiry times back into the past.
* Useful for testing timeouts and expiries.
* @param integer $interval Amount to age in seconds.
* Disables frames support. Frames will not be fetched
* and the frameset page will be used instead.
* Switches off cookie sending and recieving.
* Skips errors for the next request only. You might
* want to confirm that a page is unreachable for
* Issues a fail if there is a transport error anywhere
* in the current frameset. Only one such error is
* @param string/boolean $result HTML or failure.
* @return string/boolean $result Passes through result.
function _failOnError($result) {
if ($error =
$this->_browser->getTransportError()) {
* Adds a header to every fetch.
* @param string $header Header line to add to every
* Sets the maximum number of redirects before
* the web page is loaded regardless.
* @param integer $max Maximum hops.
'Can only set maximum redirects in a test method, setUp() or tearDown()');
$this->_browser->setMaximumRedirects($max);
* Sets the socket timeout for opening a connection and
* receiving at least one byte of information.
* @param integer $timeout Maximum time in seconds.
$this->_browser->setConnectionTimeout($timeout);
* Sets proxy to use on all requests for when
* testing from behind a firewall. Set URL
* @param string $proxy Proxy URL.
* @param string $username Proxy username for authentication.
* @param string $password Proxy password for authentication.
function useProxy($proxy, $username =
false, $password =
false) {
$this->_browser->useProxy($proxy, $username, $password);
* Fetches a page into the page buffer. If
* there is no base for the URL then the
* current base URL is used. After the fetch
* the base URL reflects the new location.
* @param string $url URL to fetch.
* @param hash $parameters Optional additional GET data.
* @return boolean/string Raw page on success.
function get($url, $parameters =
false) {
return $this->_failOnError($this->_browser->get($url, $parameters));
* Fetches a page by POST into the page buffer.
* If there is no base for the URL then the
* current base URL is used. After the fetch
* the base URL reflects the new location.
* @param string $url URL to fetch.
* @param hash $parameters Optional additional GET data.
* @return boolean/string Raw page on success.
function post($url, $parameters =
false) {
return $this->_failOnError($this->_browser->post($url, $parameters));
* Does a HTTP HEAD fetch, fetching only the page
* headers. The current base URL is unchanged by this.
* @param string $url URL to fetch.
* @param hash $parameters Optional additional GET data.
* @return boolean True on success.
function head($url, $parameters =
false) {
return $this->_failOnError($this->_browser->head($url, $parameters));
* Equivalent to hitting the retry button on the
* browser. Will attempt to repeat the page fetch.
* @return boolean True if fetch succeeded.
return $this->_failOnError($this->_browser->retry());
* Equivalent to hitting the back button on the
* @return boolean True if history entry and
return $this->_failOnError($this->_browser->back());
* Equivalent to hitting the forward button on the
* @return boolean True if history entry and
return $this->_failOnError($this->_browser->forward());
* Retries a request after setting the authentication
* @param string $username Username for realm.
* @param string $password Password for realm.
* @return boolean/string HTML on successful fetch. Note
* that authentication may still have
return $this->_failOnError(
$this->_browser->authenticate($username, $password));
* Gets the cookie value for the current browser context.
* @param string $name Name of cookie.
* @return string Value of cookie or false if unset.
return $this->_browser->getCurrentCookieValue($name);
* Sets a cookie in the current browser.
* @param string $name Name of cookie.
* @param string $value Cookie value.
* @param string $host Host upon which the cookie is valid.
* @param string $path Cookie path if not host wide.
* @param string $expiry Expiry date.
function setCookie($name, $value, $host =
false, $path =
'/', $expiry =
false) {
$this->_browser->setCookie($name, $value, $host, $path, $expiry);
* Accessor for current frame focus. Will be
* false if no frame has focus.
* @return integer/string/boolean Label if any, otherwise
* the position in the frameset
return $this->_browser->getFrameFocus();
* Sets the focus by index. The integer index starts from 1.
* @param integer $choice Chosen frame.
* @return boolean True if frame exists.
return $this->_browser->setFrameFocusByIndex($choice);
* Sets the focus by name.
* @param string $name Chosen frame.
* @return boolean True if frame exists.
return $this->_browser->setFrameFocus($name);
* Clears the frame focus. All frames will be searched
return $this->_browser->clearFrameFocus();
* Clicks a visible text item. Will first try buttons,
* then links and then images.
* @param string $label Visible text or alt text.
* @return string/boolean Raw page or false.
return $this->_failOnError($this->_browser->click($label));
* Checks for a click target.
* @param string $label Visible text or alt text.
* @return boolean True if click target.
sprintf($message, "Click target [$label] should exist"));
* Clicks the submit button by label. The owning
* form will be submitted by this.
* @param string $label Button label. An unlabeled
* button can be triggered by 'Submit'.
* @param hash $additional Additional form values.
* @return boolean/string Page on success, else false.
function clickSubmit($label =
'Submit', $additional =
false) {
return $this->_failOnError(
$this->_browser->clickSubmit($label, $additional));
* Clicks the submit button by name attribute. The owning
* form will be submitted by this.
* @param string $name Name attribute of button.
* @param hash $additional Additional form values.
* @return boolean/string Page on success.
return $this->_failOnError(
$this->_browser->clickSubmitByName($name, $additional));
* Clicks the submit button by ID attribute. The owning
* form will be submitted by this.
* @param string $id ID attribute of button.
* @param hash $additional Additional form values.
* @return boolean/string Page on success.
return $this->_failOnError(
$this->_browser->clickSubmitById($id, $additional));
* Checks for a valid button label.
* @param string $label Visible text.
* @return boolean True if click target.
sprintf($message, "Submit button [$label] should exist"));
* Clicks the submit image by some kind of label. Usually
* the alt tag or the nearest equivalent. The owning
* form will be submitted by this. Clicking outside of
* the boundary of the coordinates will result in
* @param string $label Alt attribute of button.
* @param integer $x X-coordinate of imaginary click.
* @param integer $y Y-coordinate of imaginary click.
* @param hash $additional Additional form values.
* @return boolean/string Page on success.
function clickImage($label, $x =
1, $y =
1, $additional =
false) {
return $this->_failOnError(
$this->_browser->clickImage($label, $x, $y, $additional));
* Clicks the submit image by the name. Usually
* the alt tag or the nearest equivalent. The owning
* form will be submitted by this. Clicking outside of
* the boundary of the coordinates will result in
* @param string $name Name attribute of button.
* @param integer $x X-coordinate of imaginary click.
* @param integer $y Y-coordinate of imaginary click.
* @param hash $additional Additional form values.
* @return boolean/string Page on success.
return $this->_failOnError(
$this->_browser->clickImageByName($name, $x, $y, $additional));
* Clicks the submit image by ID attribute. The owning
* form will be submitted by this. Clicking outside of
* the boundary of the coordinates will result in
* @param integer/string $id ID attribute of button.
* @param integer $x X-coordinate of imaginary click.
* @param integer $y Y-coordinate of imaginary click.
* @param hash $additional Additional form values.
* @return boolean/string Page on success.
return $this->_failOnError(
$this->_browser->clickImageById($id, $x, $y, $additional));
* Checks for a valid image with atht alt text or title.
* @param string $label Visible text.
* @return boolean True if click target.
sprintf($message, "Image with text [$label] should exist"));
* Submits a form by the ID.
* @param string $id Form ID. No button information
* @return boolean/string Page on success.
return $this->_failOnError($this->_browser->submitFormById($id));
* Follows a link by name. Will click the first link
* found with this link text by default, or a later
* one if an index is given. Match is case insensitive
* @param string $label Text between the anchor tags.
* @param integer $index Link position counting from zero.
* @return boolean/string Page on success.
return $this->_failOnError($this->_browser->clickLink($label, $index));
* Follows a link by id attribute.
* @param string $id ID attribute value.
* @return boolean/string Page on success.
return $this->_failOnError($this->_browser->clickLinkById($id));
* Tests for the presence of a link label. Match is
* case insensitive with normalised space.
* @param string $label Text between the anchor tags.
* @param mixed $expected Expected URL or expectation object.
* @param string $message Message to display. Default
* can be embedded with %s.
* @return boolean True if link present.
function assertLink($label, $expected =
true, $message =
'%s') {
$url =
$this->_browser->getLink($label);
if ($expected ===
true ||
($expected !==
true &&
$url ===
false)) {
return $this->assertTrue($url !==
false, sprintf($message, "Link [$label] should exist"));
return $this->assert($expected, $url->asString(), sprintf($message, "Link [$label] should match"));
* Tests for the non-presence of a link label. Match is
* case insensitive with normalised space.
* @param string/integer $label Text between the anchor tags
* @param string $message Message to display. Default
* can be embedded with %s.
* @return boolean True if link missing.
$this->_browser->getLink($label) ===
false,
sprintf($message, "Link [$label] should not exist"));
* Tests for the presence of a link id attribute.
* @param string $id Id attribute value.
* @param mixed $expected Expected URL or expectation object.
* @param string $message Message to display. Default
* can be embedded with %s.
* @return boolean True if link present.
$url =
$this->_browser->getLinkById($id);
if ($expected ===
true) {
return $this->assertTrue($url !==
false, sprintf($message, "Link ID [$id] should exist"));
return $this->assert($expected, $url->asString(), sprintf($message, "Link ID [$id] should match"));
* Tests for the non-presence of a link label. Match is
* case insensitive with normalised space.
* @param string $id Id attribute value.
* @param string $message Message to display. Default
* can be embedded with %s.
* @return boolean True if link missing.
$this->_browser->getLinkById($id) ===
false,
sprintf($message, "Link ID [$id] should not exist"));
* Sets all form fields with that label, or name if there
* @param string $name Name of field in forms.
* @param string $value New value of field.
* @return boolean True if field exists, otherwise false.
function setField($label, $value, $position=
false) {
return $this->_browser->setField($label, $value, $position);
* Sets all form fields with that name.
* @param string $name Name of field in forms.
* @param string $value New value of field.
* @return boolean True if field exists, otherwise false.
return $this->_browser->setFieldByName($name, $value, $position);
* Sets all form fields with that id.
* @param string/integer $id Id of field in forms.
* @param string $value New value of field.
* @return boolean True if field exists, otherwise false.
return $this->_browser->setFieldById($id, $value);
* Confirms that the form element is currently set
* to the expected value. A missing form will always
* fail. If no value is given then only the existence
* of the field is checked.
* @param string $name Name of field in forms.
* @param mixed $expected Expected string/array value or
* false for unset fields.
* @param string $message Message to display. Default
* can be embedded with %s.
* @return boolean True if pass.
function assertField($label, $expected =
true, $message =
'%s') {
$value =
$this->_browser->getField($label);
* Confirms that the form element is currently set
* to the expected value. A missing form element will always
* fail. If no value is given then only the existence
* of the field is checked.
* @param string $name Name of field in forms.
* @param mixed $expected Expected string/array value or
* false for unset fields.
* @param string $message Message to display. Default
* can be embedded with %s.
* @return boolean True if pass.
$value =
$this->_browser->getFieldByName($name);
* Confirms that the form element is currently set
* to the expected value. A missing form will always
* fail. If no ID is given then only the existence
* of the field is checked.
* @param string/integer $id Name of field in forms.
* @param mixed $expected Expected string/array value or
* false for unset fields.
* @param string $message Message to display. Default
* can be embedded with %s.
* @return boolean True if pass.
$value =
$this->_browser->getFieldById($id);
* Tests the field value against the expectation.
* @param string $identifier Name, ID or label.
* @param mixed $value Current field value.
* @param mixed $expected Expected value to match.
* @param string $message Failure message.
* @return boolean True if pass
if ($expected ===
true) {
sprintf($message, "Field [$identifier] should exist"));
"Field [$identifier] should match with [%s]");
return $this->assert($expected, $value, $message);
* Checks the response code against a list
* @param array $responses Possible responses for a pass.
* @param string $message Message to display. Default
* can be embedded with %s.
* @return boolean True if pass.
$responses =
(is_array($responses) ?
$responses :
array($responses));
$code =
$this->_browser->getResponseCode();
$message =
sprintf($message, "Expecting response in [" .
implode(", ", $responses) .
"] got [$code]");
* Checks the mime type against a list
* @param array $types Possible mime types for a pass.
* @param string $message Message to display.
* @return boolean True if pass.
$types =
(is_array($types) ?
$types :
array($types));
$message =
sprintf($message, "Expecting mime type in [" .
implode(", ", $types) .
"] got [$type]");
* Attempt to match the authentication type within
* the security realm we are currently matching.
* @param string $authentication Usually basic.
* @param string $message Message to display.
* @return boolean True if pass.
$message =
sprintf($message, "Expected any authentication type, got [" .
$this->_browser->getAuthentication() .
"]");
$message =
sprintf($message, "Expected authentication [$authentication] got [" .
$this->_browser->getAuthentication() .
"]");
* Checks that no authentication is necessary to view
* @param string $message Message to display.
* @return boolean True if pass.
$message =
sprintf($message, "Expected no authentication type, got [" .
$this->_browser->getAuthentication() .
"]");
* Attempts to match the current security realm.
* @param string $realm Name of security realm.
* @param string $message Message to display.
* @return boolean True if pass.
"Expected realm -> $message");
* Checks each header line for the required value. If no
* value is given then only an existence check is made.
* @param string $header Case insensitive header name.
* @param mixed $value Case sensitive trimmed string to
* match against. An expectation object
* can be used for pattern matching.
* @return boolean True if pass.
function assertHeader($header, $value =
false, $message =
'%s') {
* Confirms that the header type has not been received.
* Only the landing page is checked. If you want to check
* redirect pages, then you should limit redirects so
* as to capture the page you want.
* @param string $header Case insensitive header name.
* @return boolean True if pass.
* Tests the text between the title tags.
* @param string/SimpleExpectation $title Expected title.
* @param string $message Message to display.
* @return boolean True if pass.
function assertTitle($title =
false, $message =
'%s') {
* Will trigger a pass if the text is found in the plain
* @param string $text Text to look for.
* @param string $message Message to display.
* @return boolean True if pass.
* Will trigger a pass if the text is not found in the plain
* @param string $text Text to look for.
* @param string $message Message to display.
* @return boolean True if pass.
* Will trigger a pass if the Perl regex pattern
* is found in the raw content.
* @param string $pattern Perl regex to look for including
* @param string $message Message to display.
* @return boolean True if pass.
* Will trigger a pass if the perl regex pattern
* is not present in raw content.
* @param string $pattern Perl regex to look for including
* @param string $message Message to display.
* @return boolean True if pass.
* Checks that a cookie is set for the current page
* and optionally checks the value.
* @param string $name Name of cookie to test.
* @param string $expected Expected value as a string or
* false if any value will do.
* @param string $message Message to display.
* @return boolean True if pass.
function assertCookie($name, $expected =
false, $message =
'%s') {
sprintf($message, "Expecting cookie [$name]"));
return $this->assert($expected, $value, "Expecting cookie [$name] -> $message");
* Checks that no cookie is present or that it has
* been successfully cleared.
* @param string $name Name of cookie to test.
* @param string $message Message to display.
* @return boolean True if pass.
sprintf($message, "Not expecting cookie [$name]"));
* Called from within the test methods to register
* @param boolean $result Pass on true.
* @param string $message Message to display describing
* @return boolean True on pass
* Will be true on false and vice versa. False
* is the PHP definition of false, so that null,
* empty strings, zero and an empty array all count
* @param boolean $result Pass on false.
* @param string $message Message to display.
* @return boolean True on pass
* Will trigger a pass if the two parameters have
* the same value only. Otherwise a fail. This
* is for testing hand extracted text, etc.
* @param mixed $first Value to compare.
* @param mixed $second Value to compare.
* @param string $message Message to display.
* @return boolean True on pass
function assertEqual($first, $second, $message =
'%s') {
* Will trigger a pass if the two parameters have
* a different value. Otherwise a fail. This
* is for testing hand extracted text, etc.
* @param mixed $first Value to compare.
* @param mixed $second Value to compare.
* @param string $message Message to display.
* @return boolean True on pass
* Uses a stack trace to find the line of an assertion.
* @return string Line number of first assert*
* method embedded in format string.
return $trace->traceMethod();
Documentation generated on Sun, 04 May 2008 09:22:28 -0500 by phpDocumentor 1.3.0