[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tests_runner/lib/simpletest/extensions/selenium/ -> remote-control.php (source)

   1  <?php
   2  
   3  /**

   4   *

   5   * Based on the Domain51_Testing_Selenium class available at

   6   * http://domain51.googlecode.com/svn/Domain51/trunk/

   7   *

   8   * @author Travis Swicegood <development [at] domain51 [dot] com>

   9   *

  10   */
  11  class SimpleSeleniumRemoteControl
  12  {
  13      private $_browser = '';
  14      private $_browserUrl = '';
  15      private $_host = 'localhost';
  16      private $_port = 4444;
  17      private $_timeout = 30000;
  18      private $_sessionId = null;
  19  
  20      private $_commandMap = array(
  21          'bool' => array(
  22              'verify', 
  23              'verifyTextPresent', 
  24              'verifyTextNotPresent',
  25              'verifyValue'
  26          ),
  27          'string' => array(
  28              'getNewBrowserSession',
  29          ),
  30      );
  31  
  32  	public function __construct($browser, $browserUrl, $host = 'localhost', $port = 4444, $timeout = 30000) {
  33          $this->_browser = $browser;
  34          $this->_browserUrl = $browserUrl;
  35          $this->_host = $host;
  36          $this->_port = $port;
  37          $this->_timeout = $timeout;
  38      }
  39  
  40  	public function sessionIdParser($response) {
  41          return substr($response, 3);
  42      }
  43      
  44  	public function start() {
  45          $response = $this->cmd('getNewBrowserSession', array($this->_browser, $this->_browserUrl));
  46          $this->_sessionId = $this->sessionIdParser($response);
  47      }
  48  
  49  	public function stop() {
  50          $this->cmd('testComplete');
  51          $this->_sessionId = null;
  52      }
  53  
  54  	public function __call($method, $arguments) {
  55          $response = $this->cmd($method, $arguments);
  56          
  57          foreach ($this->_commandMap as $type => $commands) {
  58              if (!in_array($method, $commands)) {
  59                  continue;
  60                  $type = null;
  61              }
  62              break;
  63          }
  64  
  65          switch ($type) {
  66              case 'bool' :
  67                  return substr($response, 0, 2) == 'OK' ? true : false;
  68                  break;
  69  
  70              case 'string' :
  71              default:
  72                  return $response;
  73          }
  74      }
  75      
  76  	private function _server() {
  77          return "http://{$this->_host}:{$this->_port}/selenium-server/driver/";
  78      }
  79  
  80      public function buildUrlCmd($method, $arguments = array()) {
  81          $params = array(
  82              'cmd=' . urlencode($method),
  83          );
  84          $i = 1;
  85          foreach ($arguments as $param) {
  86              $params[] = $i++ . '=' . urlencode(trim($param));
  87          }
  88          if (isset($this->_sessionId)) {
  89              $params[] = 'sessionId=' . $this->_sessionId;
  90          }
  91  
  92          return $this->_server()."?".implode('&', $params);
  93      }
  94  
  95  	public function cmd($method, $arguments = array()) {
  96            $url = $this->buildUrlCmd($method, $arguments);
  97            $response = $this->_sendRequest($url);
  98            return $response;
  99      }
 100  
 101  	public function isUp() {
 102          return (bool)@fsockopen($this->_host, $this->_port, $errno, $errstr, 30);
 103      }
 104      
 105  	private function _initCurl($url) {
 106          if (!function_exists('curl_init')) {
 107              throw new Exception('this code currently requires the curl extension');
 108          }
 109          if (!$ch = curl_init($url)) {
 110              throw new Exception('Unable to setup curl');
 111          }
 112          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 113          curl_setopt($ch, CURLOPT_TIMEOUT, floor($this->_timeout));
 114          return $ch;    
 115      }
 116      
 117  	private function _sendRequest($url) {
 118          $ch = $this->_initCurl($url);
 119          $result = curl_exec($ch);
 120          if (($errno = curl_errno($ch)) != 0) {
 121              throw new Exception('Curl returned non-null errno ' . $errno . ':' . curl_error($ch));
 122          }
 123          curl_close($ch);
 124          return $result;
 125      }
 126  }


Generated: Sun Oct 12 04:41:30 2008 Cross-referenced by PHPXref 0.7