Source for file page.php
Documentation is available at page.php
* Base include file for SimpleTest
* @version $Id: page.php 1938 2009-08-05 17:16:23Z dgheath $
* include other SimpleTest class files
require_once(dirname(__FILE__
) .
'/http.php');
require_once(dirname(__FILE__
) .
'/php_parser.php');
require_once(dirname(__FILE__
) .
'/tag.php');
require_once(dirname(__FILE__
) .
'/form.php');
require_once(dirname(__FILE__
) .
'/selector.php');
* A wrapper for a web page.
private $links =
array();
private $forms =
array();
private $frames =
array();
private $transport_error;
* Parses a page ready to access it's contents.
* @param SimpleHttpResponse $response Result of HTTP fetch.
* Extracts all of the response information.
* @param SimpleHttpResponse $response Response being parsed.
$this->transport_error =
$response->getError();
$this->raw =
$response->getContent();
$this->sent =
$response->getSent();
$this->headers =
$response->getHeaders();
$this->method =
$response->getMethod();
$this->url =
$response->getUrl();
$this->request_data =
$response->getRequestData();
* Sets up a missing response.
$this->transport_error =
'No page fetched yet';
$this->request_data =
false;
* Original request as bytes sent down the wire.
* @return mixed Sent content.
* Accessor for raw text of page.
* @return string Raw unparsed content.
* Accessor for plain text of page as a text browser
* @return string Plain text of page.
* Accessor for raw headers of page.
* @return string Header block as text.
return $this->headers->getRaw();
* Original request method.
* @return string GET, POST or HEAD.
* Original resource name.
* @return SimpleUrl Current url.
* Base URL if set via BASE tag page url otherwise
* @return SimpleUrl Base url.
* @return mixed Sent content.
return $this->request_data;
* Accessor for last error.
* @return string Error from last response.
return $this->transport_error;
* Accessor for current MIME type.
* @return string MIME type as string; e.g. 'text/html'
return $this->headers->getMimeType();
* Accessor for HTTP response code.
* @return integer HTTP response code received.
return $this->headers->getResponseCode();
* Accessor for last Authentication type. Only valid
* straight after a challenge (401).
* @return string Description of challenge type.
return $this->headers->getAuthentication();
* Accessor for last Authentication realm. Only valid
* straight after a challenge (401).
* @return string Name of security realm.
return $this->headers->getRealm();
* Accessor for current frame focus. Will be
* @return array Always empty.
* Sets the focus by index. The integer index starts from 1.
* @param integer $choice Chosen frame.
* @return boolean Always false.
* Sets the focus by name. Always fails for a leaf page.
* @param string $name Chosen frame.
* @return boolean False as no frames.
* Clears the frame focus. Does nothing for a leaf page.
* Test to see if link is an absolute one.
* @param string $url Url to test.
* @return boolean True if absolute.
return (boolean)
($parsed->getScheme() &&
$parsed->getHost());
* Adds a link to the page.
* @param SimpleAnchorTag $tag Link to accept.
* @param array $forms An array of SimpleForm objects
* Test for the presence of a frameset.
* @return boolean True if frameset.
return count($this->frames) >
0;
* Accessor for frame name and source URL for every frame that
* will need to be loaded. Immediate children only.
* @return boolean/array False if no frameset or
* otherwise a hash of frame URLs.
* The key is either a numerical
* base one index or the name attribute.
for ($i =
0; $i <
count($this->frames); $i++
) {
$name =
$this->frames[$i]->getAttribute('name');
$url =
new SimpleUrl($this->frames[$i]->getAttribute('src'));
$urls[$name ?
$name :
$i +
1] =
$this->expandUrl($url);
* Fetches a list of loaded frames.
* @return array/string Just the URL for a single page.
* Accessor for a list of all links.
* @return array List of urls with scheme of
* http or https and hostname.
foreach ($this->links as $link) {
$all[] =
$url->asString();
* Accessor for URLs by the link label. Label will match
* regardess of whitespace issues and case.
* @param string $label Text of link.
* @return array List of links with that label.
foreach ($this->links as $link) {
if ($link->getText() ==
$label) {
* Accessor for a URL by the id attribute.
* @param string $id Id attribute of link.
* @return SimpleUrl URL with that id of false if none.
foreach ($this->links as $link) {
if ($link->getAttribute('id') === (string)
$id) {
* Converts a link tag into a target URL.
* @param SimpleAnchor $link Parsed link.
* @return SimpleUrl URL with frame target if any.
if ($link->getAttribute('target')) {
$url->setTarget($link->getAttribute('target'));
* Expands expandomatic URLs into fully qualified
* @param SimpleUrl $url Relative URL.
* @return SimpleUrl Absolute URL.
return $url->makeAbsolute($location->makeAbsolute($this->getUrl()));
* Sets the base url for the page.
* @param string $url Base URL for page.
* Sets the title tag contents.
* @param SimpleTitleTag $tag Title of page.
* Accessor for parsed title.
* @return string Title or false if no title is present.
return $this->title->getText();
* Finds a held form by button label. Will only
* search correctly built forms.
* @param SimpleSelector $selector Button finder.
* @return SimpleForm Form object containing
for ($i =
0; $i <
count($this->forms); $i++
) {
if ($this->forms[$i]->hasSubmit($selector)) {
* Finds a held form by image using a selector.
* Will only search correctly built forms.
* @param SimpleSelector $selector Image finder.
* @return SimpleForm Form object containing
for ($i =
0; $i <
count($this->forms); $i++
) {
if ($this->forms[$i]->hasImage($selector)) {
* Finds a held form by the form ID. A way of
* identifying a specific form when we have control
* @param string $id Form label.
* @return SimpleForm Form object containing the matching ID.
for ($i =
0; $i <
count($this->forms); $i++
) {
if ($this->forms[$i]->getId() ==
$id) {
* Sets a field on each form in which the field is
* @param SimpleSelector $selector Field finder.
* @param string $value Value to set field to.
* @return boolean True if value is valid.
function setField($selector, $value, $position=
false) {
for ($i =
0; $i <
count($this->forms); $i++
) {
if ($this->forms[$i]->setField($selector, $value, $position)) {
* Accessor for a form element value within a page.
* @param SimpleSelector $selector Field finder.
* @return string/boolean A string if the field is
* present, false if unchecked
for ($i =
0; $i <
count($this->forms); $i++
) {
$value =
$this->forms[$i]->getValue($selector);
* Turns HTML into text browser visible text. Images
* are converted to their alt text and tags are supressed.
* Entities are converted to their visible representation.
* @param string $html HTML to convert.
* @return string Plain text.
$text =
preg_replace('#<(script|option|textarea)[^>]*>.*?</\1>#si', '', $text);
$text =
preg_replace('#<img[^>]*alt\s*=\s*("([^"]*)"|\'([^\']*)\'|([a-zA-Z_]+))[^>]*>#', ' \2\3\4 ', $text);
return trim(trim($text), "\xA0"); // TODO: The \xAO is a . Add a test for this.
Documentation generated on Sun, 31 Oct 2010 16:31:54 -0500 by phpDocumentor 1.4.3