* start / stop selenium server on Linux
[lhc/web/wiklou.git] / maintenance / tests / selenium / SeleniumServerManager.php
1 <?php
2 /**
3 * Selenium server manager
4 *
5 * @file
6 * @ingroup Maintenance
7 * Copyright (C) 2010 Dan Nessett <dnessett@yahoo.com>
8 * http://citizendium.org/
9 *
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.
14 *
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.
19 *
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
24 *
25 * @addtogroup Maintenance
26 */
27
28 class SeleniumServerManager {
29 private $SeleniumStartServer = false;
30 private $OS = '';
31 private $SeleniumServerPid = 'NaN';
32 private $SeleniumServerPort = 4444;
33 private $SeleniumServerStartTimeout = 10; // 10 secs.
34 private $SeleniumServerExecPath;
35
36 public function __construct( $startServer,
37 $serverPort,
38 $serverExecPath ) {
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;
46 return;
47 }
48
49 // Getters for certain private attributes. No setters, since they
50 // should not change after the manager object is created.
51
52 public function getSeleniumStartServer() {
53 return $this->SeleniumStartServer;
54 }
55
56 public function getSeleniumServerPort() {
57 return $this->SeleniumServerPort;
58 }
59
60 public function getSeleniumServerPid() {
61 return $this->SeleniumServerPid;
62 }
63
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().
67
68 public function setSeleniumStartServer( $startServer ) {
69 if ( $startServer == true ) $this->SeleniumStartServer = true;
70 }
71
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
75
76 public function start() {
77
78 if ( !$this->SeleniumStartServer ) return 'failed';
79
80 // commented out cases are untested
81
82 switch ( $this->OS ) {
83 case "Linux":
84 # case' CYGWIN_NT-5.1':
85 # case 'Darwin':
86 # case 'FreeBSD':
87 # case 'HP-UX':
88 # case 'IRIX64':
89 # case 'NetBSD':
90 # case 'OpenBSD':
91 # case 'SunOS':
92 # case 'Unix':
93 // *nix based OS
94 return $this->startServerOnUnix();
95 break;
96 case "Windows":
97 case "WIN32":
98 case "WINNT":
99 // Windows
100 return $this->startServerOnWindows();
101 break;
102 default:
103 // An untested OS
104 return 'failed';
105 break;
106 }
107 }
108
109 public function stop() {
110
111 // commented out cases are untested
112
113 switch ( $this->OS ) {
114 case "Linux":
115 # case' CYGWIN_NT-5.1':
116 # case 'Darwin':
117 # case 'FreeBSD':
118 # case 'HP-UX':
119 # case 'IRIX64':
120 # case 'NetBSD':
121 # case 'OpenBSD':
122 # case 'SunOS':
123 # case 'Unix':
124 // *nix based OS
125 return $this->stopServerOnUnix();
126 break;
127 case "Windows":
128 case "WIN32":
129 case "WINNT":
130 // Windows
131 return $this->stopServerOnWindows();
132 break;
133 default:
134 // An untested OS
135 return 'failed';
136 break;
137 }
138 }
139
140 private function startServerOnUnix() {
141
142 $output = array();
143 $user = $_ENV['USER'];
144 exec("ps -U " . $user . " w | grep -i selenium-server", $output);
145
146 // Start server. If there is already a server running,
147 // return running.
148
149 if ( isset( $this->SeleniumServerExecPath ) ) {
150 $found = 0;
151 foreach ( $output as $string ) {
152 $found += preg_match(
153 '~^(.*)java(.+)-jar(.+)selenium-server~',
154 $string );
155 }
156 if ( $found == 0 ) {
157
158 // Didn't find the selenium server. Start it up.
159 // First set up comamand line suffix.
160 // NB: $! is pid of last job run in background
161 // The echo guarentees it is put into $op when
162 // the exec command is run.
163
164 $commandSuffix = ' > /dev/null 2>&1'. ' & echo $!';
165 $portText = ' -port ' . $this->SeleniumServerPort;
166 $command = "java -jar " .
167 $this->SeleniumServerExecPath .
168 $portText . $commandSuffix;
169 exec($command ,$op);
170 $pid = (int)$op[0];
171 if ( $pid != "" )
172 $this->SeleniumServerPid = $pid;
173 else {
174 $this->SeleniumServerPid = 'NaN';
175 // Server start failed.
176 return 'failed';
177 }
178 // Wait for the server to startup and listen
179 // on its port. Note: this solution kinda
180 // stinks, since it uses a wait loop - dnessett
181
182 for ( $cnt = 1;
183 $cnt <= $this->SeleniumServerStartTimeout;
184 $cnt++ ) {
185 $fp = @fsockopen ( 'localhost',
186 $this->SeleniumServerPort,
187 &$errno, &$errstr, 0 );
188 if ( !$fp ) {
189 sleep( 1 );
190 continue;
191 // Server start succeeded.
192 } else {
193 fclose ( $fp );
194 return 'started';
195 }
196 }
197 echo ( "Starting Selenium server timed out.\n" );
198 return 'failed';
199 }
200 // server already running.
201 else return 'running';
202
203 }
204 // No Server execution path defined.
205 return 'failed';
206 }
207
208 private function startServerOnWindows() {
209 // Unimplemented.
210 return 'failed';
211 }
212
213 private function stopServerOnUnix() {
214
215 if ( !empty( $this->SeleniumServerPid ) &&
216 $this->SeleniumServerPid != 'NaN' ) {
217 exec( "kill -9 " . $this->SeleniumServerPid );
218 return 'stopped';
219 }
220 else return 'failed';
221 }
222
223 private function stopServerOnWindows() {
224 // Unimplemented.
225 return 'failed';
226
227 }
228 }