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

Documentation generated on Sun, 04 May 2008 09:21:19 -0500 by phpDocumentor 1.3.0