MediaWiki
REL1_19
|
00001 <?php 00028 class SeleniumServerManager { 00029 private $SeleniumStartServer = false; 00030 private $OS = ''; 00031 private $SeleniumServerPid = 'NaN'; 00032 private $SeleniumServerPort = 4444; 00033 private $SeleniumServerStartTimeout = 10; // 10 secs. 00034 private $SeleniumServerExecPath; 00035 00036 public function __construct( $startServer, 00037 $serverPort, 00038 $serverExecPath ) { 00039 $this->OS = (string) PHP_OS; 00040 if ( isset( $startServer ) ) 00041 $this->SeleniumStartServer = $startServer; 00042 if ( isset( $serverPort ) ) 00043 $this->SeleniumServerPort = $serverPort; 00044 if ( isset( $serverExecPath ) ) 00045 $this->SeleniumServerExecPath = $serverExecPath; 00046 return; 00047 } 00048 00049 // Getters for certain private attributes. No setters, since they 00050 // should not change after the manager object is created. 00051 00052 public function getSeleniumStartServer() { 00053 return $this->SeleniumStartServer; 00054 } 00055 00056 public function getSeleniumServerPort() { 00057 return $this->SeleniumServerPort; 00058 } 00059 00060 public function getSeleniumServerPid() { 00061 return $this->SeleniumServerPid; 00062 } 00063 00064 // Changing value of SeleniumStartServer allows starting server after 00065 // creation of the class instance. Only allow setting SeleniumStartServer 00066 // to true, since after server is started, it is shut down by stop(). 00067 00068 public function setSeleniumStartServer( $startServer ) { 00069 if ( $startServer == true ) $this->SeleniumStartServer = true; 00070 } 00071 00072 // return values are: 1) started - server started, 2) failed - 00073 // server not started, 3) running - instructed to start server, but 00074 // server already running 00075 00076 public function start() { 00077 00078 if ( !$this->SeleniumStartServer ) return 'failed'; 00079 00080 // commented out cases are untested 00081 00082 switch ( $this->OS ) { 00083 case "Linux": 00084 # case' CYGWIN_NT-5.1': 00085 case 'Darwin': 00086 # case 'FreeBSD': 00087 # case 'HP-UX': 00088 # case 'IRIX64': 00089 # case 'NetBSD': 00090 # case 'OpenBSD': 00091 # case 'SunOS': 00092 # case 'Unix': 00093 // *nix based OS 00094 return $this->startServerOnUnix(); 00095 break; 00096 case "Windows": 00097 case "WIN32": 00098 case "WINNT": 00099 // Windows 00100 return $this->startServerOnWindows(); 00101 break; 00102 default: 00103 // An untested OS 00104 return 'failed'; 00105 break; 00106 } 00107 } 00108 00109 public function stop() { 00110 00111 // commented out cases are untested 00112 00113 switch ( $this->OS ) { 00114 case "Linux": 00115 # case' CYGWIN_NT-5.1': 00116 case 'Darwin': 00117 # case 'FreeBSD': 00118 # case 'HP-UX': 00119 # case 'IRIX64': 00120 # case 'NetBSD': 00121 # case 'OpenBSD': 00122 # case 'SunOS': 00123 # case 'Unix': 00124 // *nix based OS 00125 return $this->stopServerOnUnix(); 00126 break; 00127 case "Windows": 00128 case "WIN32": 00129 case "WINNT": 00130 // Windows 00131 return $this->stopServerOnWindows(); 00132 break; 00133 default: 00134 // An untested OS 00135 return 'failed'; 00136 break; 00137 } 00138 } 00139 00140 private function startServerOnUnix() { 00141 00142 $output = array(); 00143 $user = $_ENV['USER']; 00144 // @todo FIXME: This should be a little more generalized :) 00145 if (PHP_OS == 'Darwin') { 00146 // Mac OS X's ps barfs on the 'w' param, but doesn't need it. 00147 $ps = "ps -U %s"; 00148 } else { 00149 // Good on Linux 00150 $ps = "ps -U %s w"; 00151 } 00152 $psCommand = sprintf($ps, escapeshellarg($user)); 00153 exec($psCommand . " | grep -i selenium-server", $output); 00154 00155 // Start server. If there is already a server running, 00156 // return running. 00157 00158 if ( isset( $this->SeleniumServerExecPath ) ) { 00159 $found = 0; 00160 foreach ( $output as $string ) { 00161 $found += preg_match( 00162 '~^(.*)java(.+)-jar(.+)selenium-server~', 00163 $string ); 00164 } 00165 if ( $found == 0 ) { 00166 00167 // Didn't find the selenium server. Start it up. 00168 // First set up comamand line suffix. 00169 // NB: $! is pid of last job run in background 00170 // The echo guarentees it is put into $op when 00171 // the exec command is run. 00172 00173 $commandSuffix = ' > /dev/null 2>&1'. ' & echo $!'; 00174 $portText = ' -port ' . $this->SeleniumServerPort; 00175 $command = "java -jar " . 00176 escapeshellarg($this->SeleniumServerExecPath) . 00177 $portText . $commandSuffix; 00178 exec($command ,$op); 00179 $pid = (int)$op[0]; 00180 if ( $pid != "" ) 00181 $this->SeleniumServerPid = $pid; 00182 else { 00183 $this->SeleniumServerPid = 'NaN'; 00184 // Server start failed. 00185 return 'failed'; 00186 } 00187 // Wait for the server to startup and listen 00188 // on its port. Note: this solution kinda 00189 // stinks, since it uses a wait loop - dnessett 00190 00191 wfSuppressWarnings(); 00192 for ( $cnt = 1; 00193 $cnt <= $this->SeleniumServerStartTimeout; 00194 $cnt++ ) { 00195 $fp = fsockopen ( 'localhost', 00196 $this->SeleniumServerPort, 00197 $errno, $errstr, 0 ); 00198 if ( !$fp ) { 00199 sleep( 1 ); 00200 continue; 00201 // Server start succeeded. 00202 } else { 00203 fclose ( $fp ); 00204 return 'started'; 00205 } 00206 } 00207 wfRestoreWarnings(); 00208 echo ( "Starting Selenium server timed out.\n" ); 00209 return 'failed'; 00210 } 00211 // server already running. 00212 else return 'running'; 00213 00214 } 00215 // No Server execution path defined. 00216 return 'failed'; 00217 } 00218 00219 private function startServerOnWindows() { 00220 // Unimplemented. 00221 return 'failed'; 00222 } 00223 00224 private function stopServerOnUnix() { 00225 00226 if ( !empty( $this->SeleniumServerPid ) && 00227 $this->SeleniumServerPid != 'NaN' ) { 00228 exec( "kill -9 " . $this->SeleniumServerPid ); 00229 return 'stopped'; 00230 } 00231 else return 'failed'; 00232 } 00233 00234 private function stopServerOnWindows() { 00235 // Unimplemented. 00236 return 'failed'; 00237 00238 } 00239 }