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