Source for file browser.php

Documentation is available at browser.php

  1. <?php
  2. /**
  3.  *  Base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @subpackage WebTester
  6.  *  @version    $Id: browser.php 1972 2009-12-13 21:38:19Z lastcraft $
  7.  */
  8.  
  9. /**#@+
  10.  *  include other SimpleTest class files
  11.  */
  12. require_once(dirname(__FILE__'/simpletest.php');
  13. require_once(dirname(__FILE__'/http.php');
  14. require_once(dirname(__FILE__'/encoding.php');
  15. require_once(dirname(__FILE__'/page.php');
  16. require_once(dirname(__FILE__'/php_parser.php');
  17. require_once(dirname(__FILE__'/tidy_parser.php');
  18. require_once(dirname(__FILE__'/selector.php');
  19. require_once(dirname(__FILE__'/frames.php');
  20. require_once(dirname(__FILE__'/user_agent.php');
  21. if (SimpleTest::getParsers()) {
  22.     SimpleTest::setParsers(array(new SimpleTidyPageBuilder()new SimplePHPPageBuilder()));
  23.     //SimpleTest::setParsers(array(new SimplePHPPageBuilder()));
  24. }
  25. /**#@-*/
  26.  
  27. if (defined('DEFAULT_MAX_NESTED_FRAMES')) {
  28.     define('DEFAULT_MAX_NESTED_FRAMES'3);
  29. }
  30.  
  31. /**
  32.  *    Browser history list.
  33.  *    @package SimpleTest
  34.  *    @subpackage WebTester
  35.  */
  36.     private $sequence array();
  37.     private $position = -1;
  38.  
  39.     /**
  40.      *    Test for no entries yet.
  41.      *    @return boolean        True if empty.
  42.      *    @access private
  43.      */
  44.     protected function isEmpty({
  45.         return ($this->position == -1);
  46.     }
  47.  
  48.     /**
  49.      *    Test for being at the beginning.
  50.      *    @return boolean        True if first.
  51.      *    @access private
  52.      */
  53.     protected function atBeginning({
  54.         return ($this->position == 0&& $this->isEmpty();
  55.     }
  56.  
  57.     /**
  58.      *    Test for being at the last entry.
  59.      *    @return boolean        True if last.
  60.      *    @access private
  61.      */
  62.     protected function atEnd({
  63.         return ($this->position >= count($this->sequence)) && $this->isEmpty();
  64.     }
  65.  
  66.     /**
  67.      *    Adds a successfully fetched page to the history.
  68.      *    @param SimpleUrl $url                 URL of fetch.
  69.      *    @param SimpleEncoding $parameters     Any post data with the fetch.
  70.      *    @access public
  71.      */
  72.     function recordEntry($url$parameters{
  73.         $this->dropFuture();
  74.         array_push(
  75.                 $this->sequence,
  76.                 array('url' => $url'parameters' => $parameters));
  77.         $this->position++;
  78.     }
  79.  
  80.     /**
  81.      *    Last fully qualified URL for current history
  82.      *    position.
  83.      *    @return SimpleUrl        URL for this position.
  84.      *    @access public
  85.      */
  86.     function getUrl({
  87.         if ($this->isEmpty()) {
  88.             return false;
  89.         }
  90.         return $this->sequence[$this->position]['url'];
  91.     }
  92.  
  93.     /**
  94.      *    Parameters of last fetch from current history
  95.      *    position.
  96.      *    @return SimpleFormEncoding    Post parameters.
  97.      *    @access public
  98.      */
  99.     function getParameters({
  100.         if ($this->isEmpty()) {
  101.             return false;
  102.         }
  103.         return $this->sequence[$this->position]['parameters'];
  104.     }
  105.  
  106.     /**
  107.      *    Step back one place in the history. Stops at
  108.      *    the first page.
  109.      *    @return boolean     True if any previous entries.
  110.      *    @access public
  111.      */
  112.     function back({
  113.         if ($this->isEmpty(|| $this->atBeginning()) {
  114.             return false;
  115.         }
  116.         $this->position--;
  117.         return true;
  118.     }
  119.  
  120.     /**
  121.      *    Step forward one place. If already at the
  122.      *    latest entry then nothing will happen.
  123.      *    @return boolean     True if any future entries.
  124.      *    @access public
  125.      */
  126.     function forward({
  127.         if ($this->isEmpty(|| $this->atEnd()) {
  128.             return false;
  129.         }
  130.         $this->position++;
  131.         return true;
  132.     }
  133.  
  134.     /**
  135.      *    Ditches all future entries beyond the current
  136.      *    point.
  137.      *    @access private
  138.      */
  139.     protected function dropFuture({
  140.         if ($this->isEmpty()) {
  141.             return;
  142.         }
  143.         while ($this->atEnd()) {
  144.             array_pop($this->sequence);
  145.         }
  146.     }
  147. }
  148.  
  149. /**
  150.  *    Simulated web browser. This is an aggregate of
  151.  *    the user agent, the HTML parsing, request history
  152.  *    and the last header set.
  153.  *    @package SimpleTest
  154.  *    @subpackage WebTester
  155.  */
  156. class SimpleBrowser {
  157.     private $user_agent;
  158.     private $page;
  159.     private $history;
  160.     private $ignore_frames;
  161.     private $maximum_nested_frames;
  162.     private $parser;
  163.  
  164.     /**
  165.      *    Starts with a fresh browser with no
  166.      *    cookie or any other state information. The
  167.      *    exception is that a default proxy will be
  168.      *    set up if specified in the options.
  169.      *    @access public
  170.      */
  171.     function __construct({
  172.         $this->user_agent $this->createUserAgent();
  173.         $this->user_agent->useProxy(
  174.                 SimpleTest::getDefaultProxy(),
  175.                 SimpleTest::getDefaultProxyUsername(),
  176.                 SimpleTest::getDefaultProxyPassword());
  177.         $this->page new SimplePage();
  178.         $this->history $this->createHistory();
  179.         $this->ignore_frames false;
  180.         $this->maximum_nested_frames DEFAULT_MAX_NESTED_FRAMES;
  181.     }
  182.  
  183.     /**
  184.      *    Creates the underlying user agent.
  185.      *    @return SimpleFetcher    Content fetcher.
  186.      *    @access protected
  187.      */
  188.     protected function createUserAgent({
  189.         return new SimpleUserAgent();
  190.     }
  191.  
  192.     /**
  193.      *    Creates a new empty history list.
  194.      *    @return SimpleBrowserHistory    New list.
  195.      *    @access protected
  196.      */
  197.     protected function createHistory({
  198.         return new SimpleBrowserHistory();
  199.     }
  200.  
  201.     /**
  202.      *    Get the HTML parser to use. Can be overridden by
  203.      *    setParser. Otherwise scans through the available parsers and
  204.      *    uses the first one which is available.
  205.      *    @return object SimplePHPPageBuilder or SimpleTidyPageBuilder
  206.      */
  207.     protected function getParser({
  208.         if ($this->parser{
  209.             return $this->parser;
  210.         }
  211.         foreach (SimpleTest::getParsers(as $parser{
  212.             if ($parser->can()) {
  213.                 return $parser;
  214.             }
  215.         }
  216.     }
  217.  
  218.     /**
  219.      *    Override the default HTML parser, allowing parsers to be plugged in.
  220.      *    @param object           parser object instance.
  221.      */
  222.     public function setParser($parser{
  223.         $this->parser $parser;
  224.     }
  225.  
  226.     /**
  227.      *    Disables frames support. Frames will not be fetched
  228.      *    and the frameset page will be used instead.
  229.      *    @access public
  230.      */
  231.     function ignoreFrames({
  232.         $this->ignore_frames true;
  233.     }
  234.  
  235.     /**
  236.      *    Enables frames support. Frames will be fetched from
  237.      *    now on.
  238.      *    @access public
  239.      */
  240.     function useFrames({
  241.         $this->ignore_frames false;
  242.     }
  243.  
  244.     /**
  245.      *    Switches off cookie sending and recieving.
  246.      *    @access public
  247.      */
  248.     function ignoreCookies({
  249.         $this->user_agent->ignoreCookies();
  250.     }
  251.  
  252.     /**
  253.      *    Switches back on the cookie sending and recieving.
  254.      *    @access public
  255.      */
  256.     function useCookies({
  257.         $this->user_agent->useCookies();
  258.     }
  259.  
  260.     /**
  261.      *    Parses the raw content into a page. Will load further
  262.      *    frame pages unless frames are disabled.
  263.      *    @param SimpleHttpResponse $response    Response from fetch.
  264.      *    @param integer $depth                  Nested frameset depth.
  265.      *    @return SimplePage                     Parsed HTML.
  266.      *    @access private
  267.      */
  268.     protected function parse($response$depth 0{
  269.         $page $this->buildPage($response);
  270.         if ($this->ignore_frames || $page->hasFrames(|| ($depth $this->maximum_nested_frames)) {
  271.             return $page;
  272.         }
  273.         $frameset new SimpleFrameset($page);
  274.         foreach ($page->getFrameset(as $key => $url{
  275.             $frame $this->fetch($urlnew SimpleGetEncoding()$depth 1);
  276.             $frameset->addFrame($frame$key);
  277.         }
  278.         return $frameset;
  279.     }
  280.  
  281.     /**
  282.      *    Assembles the parsing machinery and actually parses
  283.      *    a single page. Frees all of the builder memory and so
  284.      *    unjams the PHP memory management.
  285.      *    @param SimpleHttpResponse $response    Response from fetch.
  286.      *    @return SimplePage                     Parsed top level page.
  287.      */
  288.     protected function buildPage($response{
  289.         return $this->getParser()->parse($response);
  290.     }
  291.  
  292.     /**
  293.      *    Fetches a page. Jointly recursive with the parse()
  294.      *    method as it descends a frameset.
  295.      *    @param string/SimpleUrl $url          Target to fetch.
  296.      *    @param SimpleEncoding $encoding       GET/POST parameters.
  297.      *    @param integer $depth                 Nested frameset depth protection.
  298.      *    @return SimplePage                    Parsed page.
  299.      *    @access private
  300.      */
  301.     protected function fetch($url$encoding$depth 0{
  302.         $response $this->user_agent->fetchResponse($url$encoding);
  303.         if ($response->isError()) {
  304.             return new SimplePage($response);
  305.         }
  306.         return $this->parse($response$depth);
  307.     }
  308.  
  309.     /**
  310.      *    Fetches a page or a single frame if that is the current
  311.      *    focus.
  312.      *    @param SimpleUrl $url                   Target to fetch.
  313.      *    @param SimpleEncoding $parameters       GET/POST parameters.
  314.      *    @return string                          Raw content of page.
  315.      *    @access private
  316.      */
  317.     protected function load($url$parameters{
  318.         $frame $url->getTarget();
  319.         if ($frame || $this->page->hasFrames(|| (strtolower($frame== '_top')) {
  320.             return $this->loadPage($url$parameters);
  321.         }
  322.         return $this->loadFrame(array($frame)$url$parameters);
  323.     }
  324.  
  325.     /**
  326.      *    Fetches a page and makes it the current page/frame.
  327.      *    @param string/SimpleUrl $url            Target to fetch as string.
  328.      *    @param SimplePostEncoding $parameters   POST parameters.
  329.      *    @return string                          Raw content of page.
  330.      *    @access private
  331.      */
  332.     protected function loadPage($url$parameters{
  333.         $this->page $this->fetch($url$parameters);
  334.         $this->history->recordEntry(
  335.                 $this->page->getUrl(),
  336.                 $this->page->getRequestData());
  337.         return $this->page->getRaw();
  338.     }
  339.  
  340.     /**
  341.      *    Fetches a frame into the existing frameset replacing the
  342.      *    original.
  343.      *    @param array $frames                    List of names to drill down.
  344.      *    @param string/SimpleUrl $url            Target to fetch as string.
  345.      *    @param SimpleFormEncoding $parameters   POST parameters.
  346.      *    @return string                          Raw content of page.
  347.      *    @access private
  348.      */
  349.     protected function loadFrame($frames$url$parameters{
  350.         $page $this->fetch($url$parameters);
  351.         $this->page->setFrame($frames$page);
  352.         return $page->getRaw();
  353.     }
  354.  
  355.     /**
  356.      *    Removes expired and temporary cookies as if
  357.      *    the browser was closed and re-opened.
  358.      *    @param string/integer $date   Time when session restarted.
  359.      *                                   If omitted then all persistent
  360.      *                                   cookies are kept.
  361.      *    @access public
  362.      */
  363.     function restart($date false{
  364.         $this->user_agent->restart($date);
  365.     }
  366.  
  367.     /**
  368.      *    Adds a header to every fetch.
  369.      *    @param string $header       Header line to add to every
  370.      *                                 request until cleared.
  371.      *    @access public
  372.      */
  373.     function addHeader($header{
  374.         $this->user_agent->addHeader($header);
  375.     }
  376.  
  377.     /**
  378.      *    Ages the cookies by the specified time.
  379.      *    @param integer $interval    Amount in seconds.
  380.      *    @access public
  381.      */
  382.     function ageCookies($interval{
  383.         $this->user_agent->ageCookies($interval);
  384.     }
  385.  
  386.     /**
  387.      *    Sets an additional cookie. If a cookie has
  388.      *    the same name and path it is replaced.
  389.      *    @param string $name       Cookie key.
  390.      *    @param string $value      Value of cookie.
  391.      *    @param string $host       Host upon which the cookie is valid.
  392.      *    @param string $path       Cookie path if not host wide.
  393.      *    @param string $expiry     Expiry date.
  394.      *    @access public
  395.      */
  396.     function setCookie($name$value$host false$path '/'$expiry false{
  397.         $this->user_agent->setCookie($name$value$host$path$expiry);
  398.     }
  399.  
  400.     /**
  401.      *    Reads the most specific cookie value from the
  402.      *    browser cookies.
  403.      *    @param string $host        Host to search.
  404.      *    @param string $path        Applicable path.
  405.      *    @param string $name        Name of cookie to read.
  406.      *    @return string             False if not present, else the
  407.      *                                value as a string.
  408.      *    @access public
  409.      */
  410.     function getCookieValue($host$path$name{
  411.         return $this->user_agent->getCookieValue($host$path$name);
  412.     }
  413.  
  414.     /**
  415.      *    Reads the current cookies for the current URL.
  416.      *    @param string $name   Key of cookie to find.
  417.      *    @return string        Null if there is no current URL, false
  418.      *                           if the cookie is not set.
  419.      *    @access public
  420.      */
  421.     function getCurrentCookieValue($name{
  422.         return $this->user_agent->getBaseCookieValue($name$this->page->getUrl());
  423.     }
  424.  
  425.     /**
  426.      *    Sets the maximum number of redirects before
  427.      *    a page will be loaded anyway.
  428.      *    @param integer $max        Most hops allowed.
  429.      *    @access public
  430.      */
  431.     function setMaximumRedirects($max{
  432.         $this->user_agent->setMaximumRedirects($max);
  433.     }
  434.  
  435.     /**
  436.      *    Sets the maximum number of nesting of framed pages
  437.      *    within a framed page to prevent loops.
  438.      *    @param integer $max        Highest depth allowed.
  439.      *    @access public
  440.      */
  441.     function setMaximumNestedFrames($max{
  442.         $this->maximum_nested_frames $max;
  443.     }
  444.  
  445.     /**
  446.      *    Sets the socket timeout for opening a connection.
  447.      *    @param integer $timeout      Maximum time in seconds.
  448.      *    @access public
  449.      */
  450.     function setConnectionTimeout($timeout{
  451.         $this->user_agent->setConnectionTimeout($timeout);
  452.     }
  453.  
  454.     /**
  455.      *    Sets proxy to use on all requests for when
  456.      *    testing from behind a firewall. Set URL
  457.      *    to false to disable.
  458.      *    @param string $proxy        Proxy URL.
  459.      *    @param string $username     Proxy username for authentication.
  460.      *    @param string $password     Proxy password for authentication.
  461.      *    @access public
  462.      */
  463.     function useProxy($proxy$username false$password false{
  464.         $this->user_agent->useProxy($proxy$username$password);
  465.     }
  466.  
  467.     /**
  468.      *    Fetches the page content with a HEAD request.
  469.      *    Will affect cookies, but will not change the base URL.
  470.      *    @param string/SimpleUrl $url                Target to fetch as string.
  471.      *    @param hash/SimpleHeadEncoding $parameters  Additional parameters for
  472.      *                                                 HEAD request.
  473.      *    @return boolean                             True if successful.
  474.      *    @access public
  475.      */
  476.     function head($url$parameters false{
  477.         if (is_object($url)) {
  478.             $url new SimpleUrl($url);
  479.         }
  480.         if ($this->getUrl()) {
  481.             $url $url->makeAbsolute($this->getUrl());
  482.         }
  483.         $response $this->user_agent->fetchResponse($urlnew SimpleHeadEncoding($parameters));
  484.         $this->page new SimplePage($response);
  485.         return $response->isError();
  486.     }
  487.  
  488.     /**
  489.      *    Fetches the page content with a simple GET request.
  490.      *    @param string/SimpleUrl $url                Target to fetch.
  491.      *    @param hash/SimpleFormEncoding $parameters  Additional parameters for
  492.      *                                                 GET request.
  493.      *    @return string                              Content of page or false.
  494.      *    @access public
  495.      */
  496.     function get($url$parameters false{
  497.         if (is_object($url)) {
  498.             $url new SimpleUrl($url);
  499.         }
  500.         if ($this->getUrl()) {
  501.             $url $url->makeAbsolute($this->getUrl());
  502.         }
  503.         return $this->load($urlnew SimpleGetEncoding($parameters));
  504.     }
  505.  
  506.     /**
  507.      *    Fetches the page content with a POST request.
  508.      *    @param string/SimpleUrl $url                Target to fetch as string.
  509.      *    @param hash/SimpleFormEncoding $parameters  POST parameters or request body.
  510.      *    @param string $content_type                 MIME Content-Type of the request body
  511.      *    @return string                              Content of page.
  512.      *    @access public
  513.      */
  514.     function post($url$parameters false$content_type false{
  515.         if (is_object($url)) {
  516.             $url new SimpleUrl($url);
  517.         }
  518.         if ($this->getUrl()) {
  519.             $url $url->makeAbsolute($this->getUrl());
  520.         }
  521.         return $this->load($urlnew SimplePostEncoding($parameters$content_type));
  522.     }
  523.  
  524.     /**
  525.      *    Fetches the page content with a PUT request.
  526.      *    @param string/SimpleUrl $url                Target to fetch as string.
  527.      *    @param hash/SimpleFormEncoding $parameters  PUT request body.
  528.      *    @param string $content_type                 MIME Content-Type of the request body
  529.      *    @return string                              Content of page.
  530.      *    @access public
  531.      */
  532.     function put($url$parameters false$content_type false{
  533.         if (is_object($url)) {
  534.             $url new SimpleUrl($url);
  535.         }
  536.         return $this->load($urlnew SimplePutEncoding($parameters$content_type));
  537.     }
  538.  
  539.     /**
  540.      *    Sends a DELETE request and fetches the response.
  541.      *    @param string/SimpleUrl $url                Target to fetch.
  542.      *    @param hash/SimpleFormEncoding $parameters  Additional parameters for
  543.      *                                                 DELETE request.
  544.      *    @return string                              Content of page or false.
  545.      *    @access public
  546.      */
  547.     function delete($url$parameters false{
  548.         if (is_object($url)) {
  549.             $url new SimpleUrl($url);
  550.         }
  551.         return $this->load($urlnew SimpleDeleteEncoding($parameters));
  552.     }
  553.  
  554.     /**
  555.      *    Equivalent to hitting the retry button on the
  556.      *    browser. Will attempt to repeat the page fetch. If
  557.      *    there is no history to repeat it will give false.
  558.      *    @return string/boolean   Content if fetch succeeded
  559.      *                              else false.
  560.      *    @access public
  561.      */
  562.     function retry({
  563.         $frames $this->page->getFrameFocus();
  564.         if (count($frames0{
  565.             $this->loadFrame(
  566.                     $frames,
  567.                     $this->page->getUrl(),
  568.                     $this->page->getRequestData());
  569.             return $this->page->getRaw();
  570.         }
  571.         if ($url $this->history->getUrl()) {
  572.             $this->page $this->fetch($url$this->history->getParameters());
  573.             return $this->page->getRaw();
  574.         }
  575.         return false;
  576.     }
  577.  
  578.     /**
  579.      *    Equivalent to hitting the back button on the
  580.      *    browser. The browser history is unchanged on
  581.      *    failure. The page content is refetched as there
  582.      *    is no concept of content caching in SimpleTest.
  583.      *    @return boolean     True if history entry and
  584.      *                         fetch succeeded
  585.      *    @access public
  586.      */
  587.     function back({
  588.         if ($this->history->back()) {
  589.             return false;
  590.         }
  591.         $content $this->retry();
  592.         if ($content{
  593.             $this->history->forward();
  594.         }
  595.         return $content;
  596.     }
  597.  
  598.     /**
  599.      *    Equivalent to hitting the forward button on the
  600.      *    browser. The browser history is unchanged on
  601.      *    failure. The page content is refetched as there
  602.      *    is no concept of content caching in SimpleTest.
  603.      *    @return boolean     True if history entry and
  604.      *                         fetch succeeded
  605.      *    @access public
  606.      */
  607.     function forward({
  608.         if ($this->history->forward()) {
  609.             return false;
  610.         }
  611.         $content $this->retry();
  612.         if ($content{
  613.             $this->history->back();
  614.         }
  615.         return $content;
  616.     }
  617.  
  618.     /**
  619.      *    Retries a request after setting the authentication
  620.      *    for the current realm.
  621.      *    @param string $username    Username for realm.
  622.      *    @param string $password    Password for realm.
  623.      *    @return boolean            True if successful fetch. Note
  624.      *                                that authentication may still have
  625.      *                                failed.
  626.      *    @access public
  627.      */
  628.     function authenticate($username$password{
  629.         if ($this->page->getRealm()) {
  630.             return false;
  631.         }
  632.         $url $this->page->getUrl();
  633.         if ($url{
  634.             return false;
  635.         }
  636.         $this->user_agent->setIdentity(
  637.                 $url->getHost(),
  638.                 $this->page->getRealm(),
  639.                 $username,
  640.                 $password);
  641.         return $this->retry();
  642.     }
  643.  
  644.     /**
  645.      *    Accessor for a breakdown of the frameset.
  646.      *    @return array   Hash tree of frames by name
  647.      *                     or index if no name.
  648.      *    @access public
  649.      */
  650.     function getFrames({
  651.         return $this->page->getFrames();
  652.     }
  653.  
  654.     /**
  655.      *    Accessor for current frame focus. Will be
  656.      *    false if no frame has focus.
  657.      *    @return integer/string/boolean    Label if any, otherwise
  658.      *                                       the position in the frameset
  659.      *                                       or false if none.
  660.      *    @access public
  661.      */
  662.     function getFrameFocus({
  663.         return $this->page->getFrameFocus();
  664.     }
  665.  
  666.     /**
  667.      *    Sets the focus by index. The integer index starts from 1.
  668.      *    @param integer $choice    Chosen frame.
  669.      *    @return boolean           True if frame exists.
  670.      *    @access public
  671.      */
  672.     function setFrameFocusByIndex($choice{
  673.         return $this->page->setFrameFocusByIndex($choice);
  674.     }
  675.  
  676.     /**
  677.      *    Sets the focus by name.
  678.      *    @param string $name    Chosen frame.
  679.      *    @return boolean        True if frame exists.
  680.      *    @access public
  681.      */
  682.     function setFrameFocus($name{
  683.         return $this->page->setFrameFocus($name);
  684.     }
  685.  
  686.     /**
  687.      *    Clears the frame focus. All frames will be searched
  688.      *    for content.
  689.      *    @access public
  690.      */
  691.     function clearFrameFocus({
  692.         return $this->page->clearFrameFocus();
  693.     }
  694.  
  695.     /**
  696.      *    Accessor for last error.
  697.      *    @return string        Error from last response.
  698.      *    @access public
  699.      */
  700.     function getTransportError({
  701.         return $this->page->getTransportError();
  702.     }
  703.  
  704.     /**
  705.      *    Accessor for current MIME type.
  706.      *    @return string    MIME type as string; e.g. 'text/html'
  707.      *    @access public
  708.      */
  709.     function getMimeType({
  710.         return $this->page->getMimeType();
  711.     }
  712.  
  713.     /**
  714.      *    Accessor for last response code.
  715.      *    @return integer    Last HTTP response code received.
  716.      *    @access public
  717.      */
  718.     function getResponseCode({
  719.         return $this->page->getResponseCode();
  720.     }
  721.  
  722.     /**
  723.      *    Accessor for last Authentication type. Only valid
  724.      *    straight after a challenge (401).
  725.      *    @return string    Description of challenge type.
  726.      *    @access public
  727.      */
  728.     function getAuthentication({
  729.         return $this->page->getAuthentication();
  730.     }
  731.  
  732.     /**
  733.      *    Accessor for last Authentication realm. Only valid
  734.      *    straight after a challenge (401).
  735.      *    @return string    Name of security realm.
  736.      *    @access public
  737.      */
  738.     function getRealm({
  739.         return $this->page->getRealm();
  740.     }
  741.  
  742.     /**
  743.      *    Accessor for current URL of page or frame if
  744.      *    focused.
  745.      *    @return string    Location of current page or frame as
  746.      *                       a string.
  747.      */
  748.     function getUrl({
  749.         $url $this->page->getUrl();
  750.         return $url $url->asString(false;
  751.     }
  752.  
  753.     /**
  754.      *    Accessor for base URL of page if set via BASE tag
  755.      *    @return string    base URL
  756.      */
  757.     function getBaseUrl({
  758.         $url $this->page->getBaseUrl();
  759.         return $url $url->asString(false;
  760.     }
  761.  
  762.     /**
  763.      *    Accessor for raw bytes sent down the wire.
  764.      *    @return string      Original text sent.
  765.      *    @access public
  766.      */
  767.     function getRequest({
  768.         return $this->page->getRequest();
  769.     }
  770.  
  771.     /**
  772.      *    Accessor for raw header information.
  773.      *    @return string      Header block.
  774.      *    @access public
  775.      */
  776.     function getHeaders({
  777.         return $this->page->getHeaders();
  778.     }
  779.  
  780.     /**
  781.      *    Accessor for raw page information.
  782.      *    @return string      Original text content of web page.
  783.      *    @access public
  784.      */
  785.     function getContent({
  786.         return $this->page->getRaw();
  787.     }
  788.  
  789.     /**
  790.      *    Accessor for plain text version of the page.
  791.      *    @return string      Normalised text representation.
  792.      *    @access public
  793.      */
  794.     function getContentAsText({
  795.         return $this->page->getText();
  796.     }
  797.  
  798.     /**
  799.      *    Accessor for parsed title.
  800.      *    @return string     Title or false if no title is present.
  801.      *    @access public
  802.      */
  803.     function getTitle({
  804.         return $this->page->getTitle();
  805.     }
  806.  
  807.     /**
  808.      *    Accessor for a list of all links in current page.
  809.      *    @return array   List of urls with scheme of
  810.      *                     http or https and hostname.
  811.      *    @access public
  812.      */
  813.     function getUrls({
  814.         return $this->page->getUrls();
  815.     }
  816.  
  817.     /**
  818.      *    Sets all form fields with that name.
  819.      *    @param string $label   Name or label of field in forms.
  820.      *    @param string $value   New value of field.
  821.      *    @return boolean        True if field exists, otherwise false.
  822.      *    @access public
  823.      */
  824.     function setField($label$value$position=false{
  825.         return $this->page->setField(new SimpleByLabelOrName($label)$value$position);
  826.     }
  827.  
  828.     /**
  829.      *    Sets all form fields with that name. Will use label if
  830.      *    one is available (not yet implemented).
  831.      *    @param string $name    Name of field in forms.
  832.      *    @param string $value   New value of field.
  833.      *    @return boolean        True if field exists, otherwise false.
  834.      *    @access public
  835.      */
  836.     function setFieldByName($name$value$position=false{
  837.         return $this->page->setField(new SimpleByName($name)$value$position);
  838.     }
  839.  
  840.     /**
  841.      *    Sets all form fields with that id attribute.
  842.      *    @param string/integer $id   Id of field in forms.
  843.      *    @param string $value        New value of field.
  844.      *    @return boolean             True if field exists, otherwise false.
  845.      *    @access public
  846.      */
  847.     function setFieldById($id$value{
  848.         return $this->page->setField(new SimpleById($id)$value);
  849.     }
  850.  
  851.     /**
  852.      *    Accessor for a form element value within the page.
  853.      *    Finds the first match.
  854.      *    @param string $label       Field label.
  855.      *    @return string/boolean     A value if the field is
  856.      *                                present, false if unchecked
  857.      *                                and null if missing.
  858.      *    @access public
  859.      */
  860.     function getField($label{
  861.         return $this->page->getField(new SimpleByLabelOrName($label));
  862.     }
  863.  
  864.     /**
  865.      *    Accessor for a form element value within the page.
  866.      *    Finds the first match.
  867.      *    @param string $name        Field name.
  868.      *    @return string/boolean     A string if the field is
  869.      *                                present, false if unchecked
  870.      *                                and null if missing.
  871.      *    @access public
  872.      */
  873.     function getFieldByName($name{
  874.         return $this->page->getField(new SimpleByName($name));
  875.     }
  876.  
  877.     /**
  878.      *    Accessor for a form element value within the page.
  879.      *    @param string/integer $id  Id of field in forms.
  880.      *    @return string/boolean     A string if the field is
  881.      *                                present, false if unchecked
  882.      *                                and null if missing.
  883.      *    @access public
  884.      */
  885.     function getFieldById($id{
  886.         return $this->page->getField(new SimpleById($id));
  887.     }
  888.  
  889.     /**
  890.      *    Clicks the submit button by label. The owning
  891.      *    form will be submitted by this.
  892.      *    @param string $label    Button label. An unlabeled
  893.      *                             button can be triggered by 'Submit'.
  894.      *    @param hash $additional Additional form data.
  895.      *    @return string/boolean  Page on success.
  896.      *    @access public
  897.      */
  898.     function clickSubmit($label 'Submit'$additional false{
  899.         if (($form $this->page->getFormBySubmit(new SimpleByLabel($label)))) {
  900.             return false;
  901.         }
  902.         $success $this->load(
  903.                 $form->getAction(),
  904.                 $form->submitButton(new SimpleByLabel($label)$additional));
  905.         return ($success $this->getContent($success);
  906.     }
  907.  
  908.     /**
  909.      *    Clicks the submit button by name attribute. The owning
  910.      *    form will be submitted by this.
  911.      *    @param string $name     Button name.
  912.      *    @param hash $additional Additional form data.
  913.      *    @return string/boolean  Page on success.
  914.      *    @access public
  915.      */
  916.     function clickSubmitByName($name$additional false{
  917.         if (($form $this->page->getFormBySubmit(new SimpleByName($name)))) {
  918.             return false;
  919.         }
  920.         $success $this->load(
  921.                 $form->getAction(),
  922.                 $form->submitButton(new SimpleByName($name)$additional));
  923.         return ($success $this->getContent($success);
  924.     }
  925.  
  926.     /**
  927.      *    Clicks the submit button by ID attribute of the button
  928.      *    itself. The owning form will be submitted by this.
  929.      *    @param string $id       Button ID.
  930.      *    @param hash $additional Additional form data.
  931.      *    @return string/boolean  Page on success.
  932.      *    @access public
  933.      */
  934.     function clickSubmitById($id$additional false{
  935.         if (($form $this->page->getFormBySubmit(new SimpleById($id)))) {
  936.             return false;
  937.         }
  938.         $success $this->load(
  939.                 $form->getAction(),
  940.                 $form->submitButton(new SimpleById($id)$additional));
  941.         return ($success $this->getContent($success);
  942.     }
  943.  
  944.     /**
  945.      *    Tests to see if a submit button exists with this
  946.      *    label.
  947.      *    @param string $label    Button label.
  948.      *    @return boolean         True if present.
  949.      *    @access public
  950.      */
  951.     function isSubmit($label{
  952.         return (boolean)$this->page->getFormBySubmit(new SimpleByLabel($label));
  953.     }
  954.  
  955.     /**
  956.      *    Clicks the submit image by some kind of label. Usually
  957.      *    the alt tag or the nearest equivalent. The owning
  958.      *    form will be submitted by this. Clicking outside of
  959.      *    the boundary of the coordinates will result in
  960.      *    a failure.
  961.      *    @param string $label    ID attribute of button.
  962.      *    @param integer $x       X-coordinate of imaginary click.
  963.      *    @param integer $y       Y-coordinate of imaginary click.
  964.      *    @param hash $additional Additional form data.
  965.      *    @return string/boolean  Page on success.
  966.      *    @access public
  967.      */
  968.     function clickImage($label$x 1$y 1$additional false{
  969.         if (($form $this->page->getFormByImage(new SimpleByLabel($label)))) {
  970.             return false;
  971.         }
  972.         $success $this->load(
  973.                 $form->getAction(),
  974.                 $form->submitImage(new SimpleByLabel($label)$x$y$additional));
  975.         return ($success $this->getContent($success);
  976.     }
  977.  
  978.     /**
  979.      *    Clicks the submit image by the name. Usually
  980.      *    the alt tag or the nearest equivalent. The owning
  981.      *    form will be submitted by this. Clicking outside of
  982.      *    the boundary of the coordinates will result in
  983.      *    a failure.
  984.      *    @param string $name     Name attribute of button.
  985.      *    @param integer $x       X-coordinate of imaginary click.
  986.      *    @param integer $y       Y-coordinate of imaginary click.
  987.      *    @param hash $additional Additional form data.
  988.      *    @return string/boolean  Page on success.
  989.      *    @access public
  990.      */
  991.     function clickImageByName($name$x 1$y 1$additional false{
  992.         if (($form $this->page->getFormByImage(new SimpleByName($name)))) {
  993.             return false;
  994.         }
  995.         $success $this->load(
  996.                 $form->getAction(),
  997.                 $form->submitImage(new SimpleByName($name)$x$y$additional));
  998.         return ($success $this->getContent($success);
  999.     }
  1000.  
  1001.     /**
  1002.      *    Clicks the submit image by ID attribute. The owning
  1003.      *    form will be submitted by this. Clicking outside of
  1004.      *    the boundary of the coordinates will result in
  1005.      *    a failure.
  1006.      *    @param integer/string $id    ID attribute of button.
  1007.      *    @param integer $x            X-coordinate of imaginary click.
  1008.      *    @param integer $y            Y-coordinate of imaginary click.
  1009.      *    @param hash $additional      Additional form data.
  1010.      *    @return string/boolean       Page on success.
  1011.      *    @access public
  1012.      */
  1013.     function clickImageById($id$x 1$y 1$additional false{
  1014.         if (($form $this->page->getFormByImage(new SimpleById($id)))) {
  1015.             return false;
  1016.         }
  1017.         $success $this->load(
  1018.                 $form->getAction(),
  1019.                 $form->submitImage(new SimpleById($id)$x$y$additional));
  1020.         return ($success $this->getContent($success);
  1021.     }
  1022.  
  1023.     /**
  1024.      *    Tests to see if an image exists with this
  1025.      *    title or alt text.
  1026.      *    @param string $label    Image text.
  1027.      *    @return boolean         True if present.
  1028.      *    @access public
  1029.      */
  1030.     function isImage($label{
  1031.         return (boolean)$this->page->getFormByImage(new SimpleByLabel($label));
  1032.     }
  1033.  
  1034.     /**
  1035.      *    Submits a form by the ID.
  1036.      *    @param string $id       The form ID. No submit button value
  1037.      *                             will be sent.
  1038.      *    @return string/boolean  Page on success.
  1039.      *    @access public
  1040.      */
  1041.     function submitFormById($id{
  1042.         if (($form $this->page->getFormById($id))) {
  1043.             return false;
  1044.         }
  1045.         $success $this->load(
  1046.                 $form->getAction(),
  1047.                 $form->submit());
  1048.         return ($success $this->getContent($success);
  1049.     }
  1050.  
  1051.     /**
  1052.      *    Finds a URL by label. Will find the first link
  1053.      *    found with this link text by default, or a later
  1054.      *    one if an index is given. The match ignores case and
  1055.      *    white space issues.
  1056.      *    @param string $label     Text between the anchor tags.
  1057.      *    @param integer $index    Link position counting from zero.
  1058.      *    @return string/boolean   URL on success.
  1059.      *    @access public
  1060.      */
  1061.     function getLink($label$index 0{
  1062.         $urls $this->page->getUrlsByLabel($label);
  1063.         if (count($urls== 0{
  1064.             return false;
  1065.         }
  1066.         if (count($urls$index 1{
  1067.             return false;
  1068.         }
  1069.         return $urls[$index];
  1070.     }
  1071.  
  1072.     /**
  1073.      *    Follows a link by label. Will click the first link
  1074.      *    found with this link text by default, or a later
  1075.      *    one if an index is given. The match ignores case and
  1076.      *    white space issues.
  1077.      *    @param string $label     Text between the anchor tags.
  1078.      *    @param integer $index    Link position counting from zero.
  1079.      *    @return string/boolean   Page on success.
  1080.      *    @access public
  1081.      */
  1082.     function clickLink($label$index 0{
  1083.         $url $this->getLink($label$index);
  1084.         if ($url === false{
  1085.             return false;
  1086.         }
  1087.         $this->load($urlnew SimpleGetEncoding());
  1088.         return $this->getContent();
  1089.     }
  1090.  
  1091.     /**
  1092.      *    Finds a link by id attribute.
  1093.      *    @param string $id        ID attribute value.
  1094.      *    @return string/boolean   URL on success.
  1095.      *    @access public
  1096.      */
  1097.     function getLinkById($id{
  1098.         return $this->page->getUrlById($id);
  1099.     }
  1100.  
  1101.     /**
  1102.      *    Follows a link by id attribute.
  1103.      *    @param string $id        ID attribute value.
  1104.      *    @return string/boolean   Page on success.
  1105.      *    @access public
  1106.      */
  1107.     function clickLinkById($id{
  1108.         if (($url $this->getLinkById($id))) {
  1109.             return false;
  1110.         }
  1111.         $this->load($urlnew SimpleGetEncoding());
  1112.         return $this->getContent();
  1113.     }
  1114.  
  1115.     /**
  1116.      *    Clicks a visible text item. Will first try buttons,
  1117.      *    then links and then images.
  1118.      *    @param string $label        Visible text or alt text.
  1119.      *    @return string/boolean      Raw page or false.
  1120.      *    @access public
  1121.      */
  1122.     function click($label{
  1123.         $raw $this->clickSubmit($label);
  1124.         if ($raw{
  1125.             $raw $this->clickLink($label);
  1126.         }
  1127.         if ($raw{
  1128.             $raw $this->clickImage($label);
  1129.         }
  1130.         return $raw;
  1131.     }
  1132.  
  1133.     /**
  1134.      *    Tests to see if a click target exists.
  1135.      *    @param string $label    Visible text or alt text.
  1136.      *    @return boolean         True if target present.
  1137.      *    @access public
  1138.      */
  1139.     function isClickable($label{
  1140.         return $this->isSubmit($label|| ($this->getLink($label!== false|| $this->isImage($label);
  1141.     }
  1142. }
  1143. ?>

Documentation generated on Sun, 31 Oct 2010 16:31:00 -0500 by phpDocumentor 1.4.3