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

Documentation generated on Thu, 01 Oct 2009 20:54:24 -0500 by phpDocumentor 1.4.2