-
Notifications
You must be signed in to change notification settings - Fork 25
/
Phantoman.php
323 lines (280 loc) · 9.85 KB
/
Phantoman.php
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
namespace Codeception\Extension;
/**
* Phantoman.
*
* The Codeception extension for automatically starting and stopping PhantomJS
* when running tests.
*
* Originally based off of PhpBuiltinServer Codeception extension
* https://github.com/tiger-seo/PhpBuiltinServer
*/
use Codeception\Event\SuiteEvent;
use Codeception\Events;
use Codeception\Exception\ExtensionException;
use Codeception\Extension;
/**
* Class Phantoman.
*
* @package Codeception\Extension
*/
class Phantoman extends Extension
{
/**
* Events to listen to.
*
* @var array
*/
protected static $events = [
Events::SUITE_INIT => 'suiteInit',
];
/**
* A resource representing the PhantomJS process.
*
* @var resource
*/
private $resource;
/**
* File pointers that correspond to PHP's end of any pipes that are created.
*
* @var array
*/
private $pipes;
/**
* Phantoman constructor.
*
* @param array $config
* Current extension configuration.
* @param array $options
* Passed running options.
*
* @throws \Codeception\Exception\ExtensionException
*/
public function __construct(array $config, array $options)
{
// Codeception has an option called silent, which suppresses the console
// output. Unfortunately there is no builtin way to activate this mode for
// a single extension. This is why the option will passed from the
// extension configuration ($config) to the global configuration ($options);
// Note: This must be done before calling the parent constructor.
if (isset($config['silent']) && $config['silent']) {
$options['silent'] = true;
}
parent::__construct($config, $options);
// Set default path for PhantomJS to "vendor/bin/phantomjs" for if it was
// installed via composer.
if (!isset($this->config['path'])) {
$this->config['path'] = 'vendor/bin/phantomjs';
}
// Add .exe extension if running on the windows.
if ($this->isWindows() && file_exists(realpath($this->config['path'] . '.exe'))) {
$this->config['path'] .= '.exe';
}
if (!file_exists(realpath($this->config['path']))) {
throw new ExtensionException($this, "PhantomJS executable not found: {$this->config['path']}");
}
// Set default WebDriver port.
if (!isset($this->config['port'])) {
$this->config['port'] = 4444;
}
// Set default debug mode.
if (!isset($this->config['debug'])) {
$this->config['debug'] = false;
}
}
/**
* Stop the server when we get destroyed.
*/
public function __destruct()
{
$this->stopServer();
}
/**
* Start PhantomJS server.
*
* @throws \Codeception\Exception\ExtensionException
*/
private function startServer()
{
if ($this->resource !== null) {
return;
}
$this->writeln(PHP_EOL);
$this->writeln('Starting PhantomJS Server.');
$command = $this->getCommand();
if ($this->config['debug']) {
$this->writeln(PHP_EOL);
// Output the generated command.
$this->writeln('Generated PhantomJS Command:');
$this->writeln($command);
$this->writeln(PHP_EOL);
}
$descriptorSpec = [
['pipe', 'r'],
['file', $this->getLogDir() . 'phantomjs.output.txt', 'w'],
['file', $this->getLogDir() . 'phantomjs.errors.txt', 'a'],
];
$this->resource = proc_open($command, $descriptorSpec, $this->pipes, null, null, ['bypass_shell' => true]);
if (!is_resource($this->resource) || !proc_get_status($this->resource)['running']) {
proc_close($this->resource);
throw new ExtensionException($this, 'Failed to start PhantomJS server.');
}
// Wait till the server is reachable before continuing.
$max_checks = 10;
$checks = 0;
$this->write('Waiting for the PhantomJS server to be reachable.');
while (true) {
if ($checks >= $max_checks) {
throw new ExtensionException($this, 'PhantomJS server never became reachable.');
}
$fp = @fsockopen('127.0.0.1', $this->config['port'], $errCode, $errStr, 10);
if ($fp) {
$this->writeln('');
$this->writeln('PhantomJS server now accessible.');
fclose($fp);
break;
}
$this->write('.');
$checks++;
// Wait before checking again.
sleep(1);
}
// Clear progress line writing.
$this->writeln('');
}
/**
* Stop PhantomJS server.
*/
private function stopServer()
{
if ($this->resource !== null) {
$this->write('Stopping PhantomJS Server.');
// Wait till the server has been stopped.
$max_checks = 10;
for ($i = 0; $i < $max_checks; $i++) {
// If we're on the last loop, and it's still not shut down, just
// unset resource to allow the tests to finish.
if ($i === $max_checks - 1 && proc_get_status($this->resource)['running'] === true) {
$this->writeln('');
$this->writeln('Unable to properly shutdown PhantomJS server.');
unset($this->resource);
break;
}
// Check if the process has stopped yet.
if (proc_get_status($this->resource)['running'] === false) {
$this->writeln('');
$this->writeln('PhantomJS server stopped.');
unset($this->resource);
break;
}
foreach ($this->pipes as $pipe) {
if (is_resource($pipe)) {
fclose($pipe);
}
}
// Terminate the process.
// Note: Use of SIGINT adds dependency on PCTNL extension so we
// use the integer value instead.
proc_terminate($this->resource, 2);
$this->write('.');
// Wait before checking again.
sleep(1);
}
}
}
/**
* Build the parameters for our command.
*
* @return string
* All parameters separated by spaces.
*/
private function getCommandParameters()
{
// Map our config options to PhantomJS options.
$mapping = [
'port' => '--webdriver',
'proxy' => '--proxy',
'proxyType' => '--proxy-type',
'proxyAuth' => '--proxy-auth',
'webSecurity' => '--web-security',
'ignoreSslErrors' => '--ignore-ssl-errors',
'sslProtocol' => '--ssl-protocol',
'sslCertificatesPath' => '--ssl-certificates-path',
'remoteDebuggerPort' => '--remote-debugger-port',
'remoteDebuggerAutorun' => '--remote-debugger-autorun',
'cookiesFile' => '--cookies-file',
'diskCache' => '--disk-cache',
'maxDiskCacheSize' => '--max-disk-cache-size',
'loadImage' => '--load-images',
'localStoragePath' => '--local-storage-path',
'localStorageQuota' => '--local-storage-quota',
'localToRemoteUrlAccess' => '--local-to-remote-url-access',
'outputEncoding' => '--output-encoding',
'scriptEncoding' => '--script-encoding',
'webdriverLoglevel' => '--webdriver-loglevel',
'webdriverLogfile' => '--webdriver-logfile',
];
$params = [];
foreach ($this->config as $configKey => $configValue) {
if (!empty($mapping[$configKey])) {
if (is_bool($configValue)) {
// Make sure the value is true/false and not 1/0.
$configValue = $configValue ? 'true' : 'false';
}
$params[] = $mapping[$configKey] . '=' . $configValue;
}
}
return implode(' ', $params);
}
/**
* Get PhantomJS command.
*
* @return string
* Command to execute.
*/
private function getCommand()
{
// Prefix command with exec on non Windows systems to ensure that we
// receive the correct pid.
// See http://php.net/manual/en/function.proc-get-status.php#93382
$commandPrefix = $this->isWindows() ? '' : 'exec ';
return $commandPrefix . escapeshellarg(realpath($this->config['path'])) . ' ' . $this->getCommandParameters();
}
/**
* Checks if the current machine is Windows.
*
* @return bool
* True if the machine is windows.
*/
private function isWindows()
{
return stripos(PHP_OS, 'WIN') === 0;
}
/**
* Suite Init.
*
* @param \Codeception\Event\SuiteEvent $e
* The event with suite, result and settings.
*
* @throws \Codeception\Exception\ExtensionException
*/
public function suiteInit(SuiteEvent $e)
{
// Check if PhantomJS should only be started for specific suites.
if (isset($this->config['suites'])) {
if (is_string($this->config['suites'])) {
$suites = [$this->config['suites']];
} else {
$suites = $this->config['suites'];
}
// If the current suites aren't in the desired array, return without
// starting PhantomJS.
if (!in_array($e->getSuite()->getBaseName(), $suites, true)
&& !in_array($e->getSuite()->getName(), $suites, true)) {
return;
}
}
// Start the PhantomJS server.
$this->startServer();
}
}