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