summaryrefslogtreecommitdiff
path: root/tests/selenium/SeleniumServerManager.php
blob: 9efa509a64e593d52e896bfeaf9d4f2a59797829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
 * Selenium server manager
 *
 * @file
 * @ingroup Testing
 * Copyright (C) 2010 Dan Nessett <dnessett@yahoo.com>
 * http://citizendium.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */

class SeleniumServerManager {
	private $SeleniumStartServer = false;
	private $OS = '';
	private $SeleniumServerPid = 'NaN';
	private $SeleniumServerPort = 4444;
	private $SeleniumServerStartTimeout = 10; // 10 secs.
	private $SeleniumServerExecPath;

	public function __construct( $startServer,
								 $serverPort,
								 $serverExecPath ) {
		$this->OS = (string)PHP_OS;

		if ( isset( $startServer ) ) {
			$this->SeleniumStartServer = $startServer;
		}

		if ( isset( $serverPort ) ) {
			$this->SeleniumServerPort = $serverPort;
		}

		if ( isset( $serverExecPath ) ) {
			$this->SeleniumServerExecPath = $serverExecPath;
		}

		return;
	}

	// Getters for certain private attributes. No setters, since they
	// should not change after the manager object is created.

	public function getSeleniumStartServer() {
		return $this->SeleniumStartServer;
	}

	public function getSeleniumServerPort() {
		return $this->SeleniumServerPort;
	}

	public function getSeleniumServerPid() {
		return $this->SeleniumServerPid;
	}

	// Changing value of SeleniumStartServer allows starting server after
	// creation of the class instance. Only allow setting SeleniumStartServer
	// to true, since after server is started, it is shut down by stop().

	public function setSeleniumStartServer( $startServer ) {
		if ( $startServer == true ) {
			$this->SeleniumStartServer = true;
		}
	}

	// return values are: 1) started - server started, 2) failed -
	// server not started, 3) running - instructed to start server, but
	// server already running

	public function start() {

		if ( !$this->SeleniumStartServer ) {
			return 'failed';
		}

		// commented out cases are untested

		switch ( $this->OS ) {
			case "Linux":
#			case' CYGWIN_NT-5.1':
			case 'Darwin':
#			case 'FreeBSD':
#			case 'HP-UX':
#			case 'IRIX64':
#			case 'NetBSD':
#			case 'OpenBSD':
#			case 'SunOS':
#			case 'Unix':
				// *nix based OS
				return $this->startServerOnUnix();
				break;
			case "Windows":
			case "WIN32":
			case "WINNT":
				// Windows
				return $this->startServerOnWindows();
				break;
			default:
				// An untested OS
				return 'failed';
				break;
		}
	}

	public function stop() {

		// commented out cases are untested

		switch ( $this->OS ) {
			case "Linux":
#			case' CYGWIN_NT-5.1':
			case 'Darwin':
#			case 'FreeBSD':
#			case 'HP-UX':
#			case 'IRIX64':
#			case 'NetBSD':
#			case 'OpenBSD':
#			case 'SunOS':
#			case 'Unix':
				// *nix based OS
				return $this->stopServerOnUnix();
				break;
			case "Windows":
			case "WIN32":
			case "WINNT":
				// Windows
				return $this->stopServerOnWindows();
				break;
			default:
				// An untested OS
				return 'failed';
				break;
		}
	}

	private function startServerOnUnix() {

		$output = array();
		$user = $_ENV['USER'];
		// @todo FIXME: This should be a little more generalized :)
		if ( PHP_OS == 'Darwin' ) {
			// Mac OS X's ps barfs on the 'w' param, but doesn't need it.
			$ps = "ps -U %s";
		} else {
			// Good on Linux
			$ps = "ps -U %s w";
		}
		$psCommand = sprintf( $ps, escapeshellarg( $user ) );
		exec( $psCommand . " | grep -i selenium-server", $output );

		// Start server. If there is already a server running,
		// return running.

		if ( isset( $this->SeleniumServerExecPath ) ) {
			$found = 0;
			foreach ( $output as $string ) {
				$found += preg_match(
					'~^(.*)java(.+)-jar(.+)selenium-server~',
					$string );
			}
			if ( $found == 0 ) {

				// Didn't find the selenium server. Start it up.
				// First set up comamand line suffix.
				// NB: $! is pid of last job run in background
				// The echo guarentees it is put into $op when
				// the exec command is run.

				$commandSuffix = ' > /dev/null 2>&1' . ' & echo $!';
				$portText = ' -port ' . $this->SeleniumServerPort;
				$command = "java -jar " .
					escapeshellarg( $this->SeleniumServerExecPath ) .
					$portText . $commandSuffix;
				exec( $command, $op );
				$pid = (int)$op[0];
				if ( $pid != "" ) {
					$this->SeleniumServerPid = $pid;
				} else {
					$this->SeleniumServerPid = 'NaN';
					// Server start failed.
					return 'failed';
				}
				// Wait for the server to startup and listen
				// on its port. Note: this solution kinda
				// stinks, since it uses a wait loop - dnessett

				wfSuppressWarnings();
				for ( $cnt = 1;
					  $cnt <= $this->SeleniumServerStartTimeout;
					  $cnt++ ) {
					$fp = fsockopen( 'localhost',
						$this->SeleniumServerPort,
						$errno, $errstr, 0 );
					if ( !$fp ) {
						sleep( 1 );
						continue;
						// Server start succeeded.
					} else {
						fclose( $fp );
						return 'started';
					}
				}
				wfRestoreWarnings();
				echo ( "Starting Selenium server timed out.\n" );
				return 'failed';
			} else {
				// server already running.
				return 'running';
			}

		}

		// No Server execution path defined.
		return 'failed';
	}

	private function startServerOnWindows() {
		// Unimplemented.
		return 'failed';
	}

	private function stopServerOnUnix() {

		if ( !empty( $this->SeleniumServerPid ) &&
			$this->SeleniumServerPid != 'NaN'
		) {
			exec( "kill -9 " . $this->SeleniumServerPid );
			return 'stopped';
		} else {
			return 'failed';
		}
	}

	private function stopServerOnWindows() {
		// Unimplemented.
		return 'failed';

	}
}