3 * Selenium server manager
7 * Copyright (C) 2010 Dan Nessett <dnessett@yahoo.com>
8 * http://citizendium.org/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
28 class SeleniumServerManager
{
29 private $SeleniumStartServer = false;
31 private $SeleniumServerPid = 'NaN';
32 private $SeleniumServerPort = 4444;
33 private $SeleniumServerStartTimeout = 10; // 10 secs.
34 private $SeleniumServerExecPath;
36 public function __construct( $startServer,
39 $this->OS
= (string) PHP_OS
;
40 if ( isset( $startServer ) )
41 $this->SeleniumStartServer
= $startServer;
42 if ( isset( $serverPort ) )
43 $this->SeleniumServerPort
= $serverPort;
44 if ( isset( $serverExecPath ) )
45 $this->SeleniumServerExecPath
= $serverExecPath;
49 // Getters for certain private attributes. No setters, since they
50 // should not change after the manager object is created.
52 public function getSeleniumStartServer() {
53 return $this->SeleniumStartServer
;
56 public function getSeleniumServerPort() {
57 return $this->SeleniumServerPort
;
60 public function getSeleniumServerPid() {
61 return $this->SeleniumServerPid
;
64 // Changing value of SeleniumStartServer allows starting server after
65 // creation of the class instance. Only allow setting SeleniumStartServer
66 // to true, since after server is started, it is shut down by stop().
68 public function setSeleniumStartServer( $startServer ) {
69 if ( $startServer == true ) $this->SeleniumStartServer
= true;
72 // return values are: 1) started - server started, 2) failed -
73 // server not started, 3) running - instructed to start server, but
74 // server already running
76 public function start() {
78 if ( !$this->SeleniumStartServer
) return 'failed';
80 // commented out cases are untested
82 switch ( $this->OS
) {
84 # case' CYGWIN_NT-5.1':
94 return $this->startServerOnUnix();
100 return $this->startServerOnWindows();
109 public function stop() {
111 // commented out cases are untested
113 switch ( $this->OS
) {
115 # case' CYGWIN_NT-5.1':
125 return $this->stopServerOnUnix();
131 return $this->stopServerOnWindows();
140 private function startServerOnUnix() {
143 $user = $_ENV['USER'];
144 // @todo FIXME: This should be a little more generalized :)
145 if (PHP_OS
== 'Darwin') {
146 // Mac OS X's ps barfs on the 'w' param, but doesn't need it.
152 $psCommand = sprintf($ps, escapeshellarg($user));
153 exec($psCommand . " | grep -i selenium-server", $output);
155 // Start server. If there is already a server running,
158 if ( isset( $this->SeleniumServerExecPath
) ) {
160 foreach ( $output as $string ) {
161 $found +
= preg_match(
162 '~^(.*)java(.+)-jar(.+)selenium-server~',
167 // Didn't find the selenium server. Start it up.
168 // First set up comamand line suffix.
169 // NB: $! is pid of last job run in background
170 // The echo guarentees it is put into $op when
171 // the exec command is run.
173 $commandSuffix = ' > /dev/null 2>&1'. ' & echo $!';
174 $portText = ' -port ' . $this->SeleniumServerPort
;
175 $command = "java -jar " .
176 escapeshellarg($this->SeleniumServerExecPath
) .
177 $portText . $commandSuffix;
181 $this->SeleniumServerPid
= $pid;
183 $this->SeleniumServerPid
= 'NaN';
184 // Server start failed.
187 // Wait for the server to startup and listen
188 // on its port. Note: this solution kinda
189 // stinks, since it uses a wait loop - dnessett
191 wfSuppressWarnings();
193 $cnt <= $this->SeleniumServerStartTimeout
;
195 $fp = fsockopen ( 'localhost',
196 $this->SeleniumServerPort
,
197 $errno, $errstr, 0 );
201 // Server start succeeded.
208 echo ( "Starting Selenium server timed out.\n" );
211 // server already running.
212 else return 'running';
215 // No Server execution path defined.
219 private function startServerOnWindows() {
224 private function stopServerOnUnix() {
226 if ( !empty( $this->SeleniumServerPid
) &&
227 $this->SeleniumServerPid
!= 'NaN' ) {
228 exec( "kill -9 " . $this->SeleniumServerPid
);
231 else return 'failed';
234 private function stopServerOnWindows() {