Source for file remote-control.php

Documentation is available at remote-control.php

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

Documentation generated on Sun, 04 May 2008 09:22:00 -0500 by phpDocumentor 1.3.0