Source for file browser.php
Documentation is available at browser.php
* Base include file for SimpleTest
* @version $Id: browser.php 1723 2008-04-08 00:34:10Z lastcraft $
* include other SimpleTest class files
require_once(dirname(__FILE__
) .
'/simpletest.php');
require_once(dirname(__FILE__
) .
'/http.php');
require_once(dirname(__FILE__
) .
'/encoding.php');
require_once(dirname(__FILE__
) .
'/page.php');
require_once(dirname(__FILE__
) .
'/selector.php');
require_once(dirname(__FILE__
) .
'/frames.php');
require_once(dirname(__FILE__
) .
'/user_agent.php');
if (!defined('DEFAULT_MAX_NESTED_FRAMES')) {
define('DEFAULT_MAX_NESTED_FRAMES', 3);
* Test for no entries yet.
* @return boolean True if empty.
* Test for being at the beginning.
* @return boolean True if first.
function _atBeginning() {
return ($this->_position ==
0) &&
! $this->_isEmpty();
* Test for being at the last entry.
* @return boolean True if last.
* Adds a successfully fetched page to the history.
* @param SimpleUrl $url URL of fetch.
* @param SimpleEncoding $parameters Any post data with the fetch.
array('url' =>
$url, 'parameters' =>
$parameters));
* Last fully qualified URL for current history
* @return SimpleUrl URL for this position.
* Parameters of last fetch from current history
* @return SimpleFormEncoding Post parameters.
* Step back one place in the history. Stops at
* @return boolean True if any previous entries.
if ($this->_isEmpty() ||
$this->_atBeginning()) {
* Step forward one place. If already at the
* latest entry then nothing will happen.
* @return boolean True if any future entries.
if ($this->_isEmpty() ||
$this->_atEnd()) {
* Ditches all future entries beyond the current
while (! $this->_atEnd()) {
* Simulated web browser. This is an aggregate of
* the user agent, the HTML parsing, request history
* and the last header set.
* Starts with a fresh browser with no
* cookie or any other state information. The
* exception is that a default proxy will be
* set up if specified in the options.
* Creates the underlying user agent.
* @return SimpleFetcher Content fetcher.
* Creates a new empty history list.
* @return SimpleBrowserHistory New list.
* Disables frames support. Frames will not be fetched
* and the frameset page will be used instead.
* Enables frames support. Frames will be fetched from
* Switches off cookie sending and recieving.
* Switches back on the cookie sending and recieving.
* Parses the raw content into a page. Will load further
* frame pages unless frames are disabled.
* @param SimpleHttpResponse $response Response from fetch.
* @param integer $depth Nested frameset depth.
* @return SimplePage Parsed HTML.
function &_parse($response, $depth =
0) {
foreach ($page->getFrameset() as $key =>
$url) {
$frameset->addFrame($frame, $key);
* Assembles the parsing machinery and actually parses
* a single page. Frees all of the builder memory and so
* unjams the PHP memory management.
* @param SimpleHttpResponse $response Response from fetch.
* @return SimplePage Parsed top level page.
$page =
&$builder->parse($response);
* Fetches a page. Jointly recursive with the _parse()
* method as it descends a frameset.
* @param string/SimpleUrl $url Target to fetch.
* @param SimpleEncoding $encoding GET/POST parameters.
* @param integer $depth Nested frameset depth protection.
* @return SimplePage Parsed page.
function &_fetch($url, $encoding, $depth =
0) {
$response =
&$this->_user_agent->fetchResponse($url, $encoding);
if ($response->isError()) {
$page =
&$this->_parse($response, $depth);
* Fetches a page or a single frame if that is the current
* @param SimpleUrl $url Target to fetch.
* @param SimpleEncoding $parameters GET/POST parameters.
* @return string Raw content of page.
function _load($url, $parameters) {
$frame =
$url->getTarget();
if (! $frame ||
! $this->_page->hasFrames() ||
(strtolower($frame) ==
'_top')) {
return $this->_loadPage($url, $parameters);
return $this->_loadFrame(array($frame), $url, $parameters);
* Fetches a page and makes it the current page/frame.
* @param string/SimpleUrl $url Target to fetch as string.
* @param SimplePostEncoding $parameters POST parameters.
* @return string Raw content of page.
function _loadPage($url, $parameters) {
$this->_page =
&$this->_fetch($url, $parameters);
$this->_page->getRequestData());
return $this->_page->getRaw();
* Fetches a frame into the existing frameset replacing the
* @param array $frames List of names to drill down.
* @param string/SimpleUrl $url Target to fetch as string.
* @param SimpleFormEncoding $parameters POST parameters.
* @return string Raw content of page.
function _loadFrame($frames, $url, $parameters) {
$page =
&$this->_fetch($url, $parameters);
$this->_page->setFrame($frames, $page);
* Removes expired and temporary cookies as if
* the browser was closed and re-opened.
* @param string/integer $date Time when session restarted.
* If omitted then all persistent
* Adds a header to every fetch.
* @param string $header Header line to add to every
* Ages the cookies by the specified time.
* @param integer $interval Amount in seconds.
* Sets an additional cookie. If a cookie has
* the same name and path it is replaced.
* @param string $name Cookie key.
* @param string $value Value of cookie.
* @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->_user_agent->setCookie($name, $value, $host, $path, $expiry);
* Reads the most specific cookie value from the
* @param string $host Host to search.
* @param string $path Applicable path.
* @param string $name Name of cookie to read.
* @return string False if not present, else the
return $this->_user_agent->getCookieValue($host, $path, $name);
* Reads the current cookies for the current URL.
* @param string $name Key of cookie to find.
* @return string Null if there is no current URL, false
* if the cookie is not set.
* Sets the maximum number of redirects before
* a page will be loaded anyway.
* @param integer $max Most hops allowed.
* Sets the maximum number of nesting of framed pages
* within a framed page to prevent loops.
* @param integer $max Highest depth allowed.
* Sets the socket timeout for opening a connection.
* @param integer $timeout Maximum time in seconds.
* 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->_user_agent->useProxy($proxy, $username, $password);
* Fetches the page content with a HEAD request.
* Will affect cookies, but will not change the base URL.
* @param string/SimpleUrl $url Target to fetch as string.
* @param hash/SimpleHeadEncoding $parameters Additional parameters for
* @return boolean True if successful.
function head($url, $parameters =
false) {
$url =
$url->makeAbsolute($this->getUrl());
return ! $response->isError();
* Fetches the page content with a simple GET request.
* @param string/SimpleUrl $url Target to fetch.
* @param hash/SimpleFormEncoding $parameters Additional parameters for
* @return string Content of page or false.
function get($url, $parameters =
false) {
$url =
$url->makeAbsolute($this->getUrl());
* Fetches the page content with a POST request.
* @param string/SimpleUrl $url Target to fetch as string.
* @param hash/SimpleFormEncoding $parameters POST parameters.
* @return string Content of page.
function post($url, $parameters =
false) {
$url =
$url->makeAbsolute($this->getUrl());