This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 394
/
Copy pathBehatCommand.php
364 lines (325 loc) · 10.8 KB
/
BehatCommand.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
namespace Acquia\Blt\Robo\Commands\Tests;
use Acquia\Blt\Robo\Exceptions\BltException;
use Acquia\Blt\Robo\Wizards\TestsWizard;
use Robo\Contract\VerbosityThresholdInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Defines commands in the "tests" namespace.
*/
class BehatCommand extends TestsCommandBase {
/**
* The filename of the selenium log file.
*
* @var string
*/
protected $seleniumLogFile;
/**
* The URL at which Selenium server listens.
*
* @var string
*/
protected $seleniumUrl;
/**
* @var int
*/
protected $seleniumPort;
/**
* @var string
*/
protected $serverUrl;
/**
* @var int
*/
protected $serverPort;
/**
* The directory containing Behat logs.
*
* @var string
*/
protected $behatLogDir;
/**
* @var string
*/
protected $chromePort;
/**
* This hook will fire for all commands in this command file.
*
* @hook init
*/
public function initialize() {
$this->seleniumLogFile = $this->getConfigValue('reports.localDir') . "/selenium2.log";
$this->behatLogDir = $this->getConfigValue('reports.localDir') . "/behat";
$this->chromePort = $this->getConfigValue('behat.chrome.port');
$this->seleniumPort = $this->getConfigValue('behat.selenium.port');
$this->seleniumUrl = $this->getConfigValue('behat.selenium.url');
$this->serverPort = $this->getConfigValue('tests.server.port');
$this->serverUrl = $this->getConfigValue('tests.server.url');
}
/**
* Executes all behat tests.
*
* @command tests:behat
* @description Executes all behat tests. This optionally launch PhantomJS or Selenium prior to execution.
* @usage
* Executes all configured tests.
* @usage -D behat.paths=${PWD}/tests/behat/features/Examples.feature
* Executes scenarios in the Examples.feature file.
* @usage -D behat.paths=${PWD}/tests/behat/features/Examples.feature:4
* Executes only the scenario on line 4 of Examples.feature.
*
* @interactGenerateSettingsFiles
* @interactInstallDrupal
* @interactConfigureBehat
* @validateMySqlAvailable
* @validateDrupalIsInstalled
* @validateBehatIsConfigured
* @validateInsideVm
* @launchWebServer
* @executeInDrupalVm
*/
public function behat() {
// Log config for debugging purposes.
$this->logConfig($this->getConfigValue('behat'), 'behat');
$this->logConfig($this->getInspector()->getLocalBehatConfig()->export());
$this->createReportsDir();
try {
$this->launchWebDriver();
$this->executeBehatTests();
$this->killWebDriver();
}
catch (\Exception $e) {
// Kill web driver a server to prevent Pipelines from hanging after fail.
$this->killWebDriver();
throw $e;
}
}
/**
* Lists available Behat step definitions.
*
* @command tests:behat:definitions
*
* @option mode l (default), i, or needle. Use l to just list definition expressions, i to show definitions with extended info, or needle to find specific definitions.
*
* @validateMySqlAvailable
*/
public function behatDefinitions($options = ['mode' => 'l']) {
$task = $this->taskBehat($this->getConfigValue('composer.bin') . '/behat')
->format('pretty')
->noInteraction()
->printMetadata(FALSE)
->option('definitions', $options['mode'])
->option('config', $this->getConfigValue('behat.config'))
->option('profile', $this->getConfigValue('behat.profile'))
->detectInteractive();
if ($this->output()->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
$task->verbose();
}
if ($this->getConfigValue('behat.extra')) {
$task->arg($this->getConfigValue('behat.extra'));
}
$result = $task->run();
return $result;
}
/**
* Launch the appropriate web driver based on configuration.
*/
protected function launchWebDriver() {
if ($this->getConfigValue('behat.web-driver') == 'phantomjs') {
$this->launchPhantomJs();
}
elseif ($this->getConfigValue('behat.web-driver') == 'selenium') {
$this->launchSelenium();
}
elseif ($this->getConfigValue('behat.web-driver') == 'chrome') {
$this->launchChrome();
}
}
/**
* Launches a headless chrome process.
*/
protected function launchChrome() {
$this->killChrome();
$chrome_bin = $this->findChrome();
$this->checkChromeVersion($chrome_bin);
$this->logger->info("Launching headless chrome...");
$this->getContainer()
->get('executor')
->execute("'$chrome_bin' --headless --disable-gpu --remote-debugging-port={$this->chromePort} https://www.chromestatus.com --disable-web-security --user-data-dir > /dev/null 2>&1")
->background(TRUE)
->printOutput(TRUE)
->printMetadata(TRUE)
->run();
$this->getContainer()->get('executor')->waitForUrlAvailable("localhost:{$this->chromePort}");
}
/**
* Kills headless chrome process running on $this->chromePort.
*/
protected function killChrome() {
$this->logger->info("Killing running google-chrome processes...");
$this->getContainer()->get('executor')->killProcessByPort($this->chromePort);
}
/**
* Finds the local Chrome binary.
*
* @return null|string
* NULL if Chrome could not be found.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
* Throws exception if google-chrome cannot be found.
*/
protected function findChrome() {
if ($this->getInspector()->commandExists('google-chrome')) {
return 'google-chrome';
}
$osx_path = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
if ($this->getInspector()->isOsx() && file_exists($osx_path)) {
return $osx_path;
}
throw new BltException("Could not find Google Chrome. Please add an alias for \"google-chrome\" to your CLI environment.");
}
/**
* Verifies that Google Chrome meets minimum version requirement.
*
* @param string $bin
* Absolute file path to the google chrome bin.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
* Throws exception if minimum version is not met.
*/
protected function checkChromeVersion($bin) {
$version = (int) $this->getContainer()->get('executor')
->execute("'$bin' --version | cut -f3 -d' ' | cut -f1 -d'.'")
->run()
->getMessage();
if ($version < 59) {
throw new BltException("You must have Google Chrome version 59+ to execute headless tests.");
}
}
/**
* Kills the appropriate web driver based on configuration.
*/
protected function killWebDriver() {
if ($this->getConfigValue('behat.web-driver') == 'phantomjs') {
$this->killPhantomJs();
}
elseif ($this->getConfigValue('behat.web-driver') == 'selenium') {
$this->killSelenium();
}
elseif ($this->getConfigValue('behat.web-driver') == 'chrome') {
$this->killChrome();
}
}
/**
* Launches selenium server and waits for it to become available.
*/
protected function launchSelenium() {
$this->createSeleniumLogs();
$this->killSelenium();
$this->logger->info("Launching Selenium standalone server...");
$this->getContainer()
->get('executor')
->execute($this->getConfigValue('composer.bin') . "/selenium-server-standalone -port {$this->seleniumPort} -log {$this->seleniumLogFile} > /dev/null 2>&1")
->background(TRUE)
// @todo Print output when this command fails.
->printOutput(TRUE)
->dir($this->getConfigValue('repo.root'))
->run();
$this->getContainer()->get('executor')->waitForUrlAvailable($this->seleniumUrl);
}
/**
* Kills any Selenium processes already running.
*/
protected function killSelenium() {
$this->logger->info("Killing any running Selenium processes...");
$this->getContainer()->get('executor')->killProcessByPort($this->seleniumPort);
$this->getContainer()->get('executor')->killProcessByName('selenium-server-standalone');
}
/**
* Creates selenium log file.
*/
protected function createSeleniumLogs() {
$this->seleniumLogFile;
$this->logger->info("Creating Selenium2 log file at {$this->seleniumLogFile}...");
$this->taskFilesystemStack()
->touch($this->seleniumLogFile)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
}
/**
* Launches selenium web driver.
*/
protected function launchPhantomJs() {
if (!$this->getInspector()->isPhantomJsBinaryPresent()) {
$this->setupPhantomJs();
}
$this->killPhantomJs();
$this->say("Launching PhantomJS GhostDriver...");
$this->taskExec("'{$this->getConfigValue('composer.bin')}/phantomjs'")
->option("webdriver", $this->seleniumPort)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->background()
->timeout(6000)
->silent(TRUE)
->interactive(FALSE)
->run();
}
/**
* Kills any running PhantomJS processes.
*/
protected function killPhantomJs() {
$this->getContainer()->get('executor')->killProcessByPort($this->seleniumPort);
$this->getContainer()->get('executor')->killProcessByName('bin/phantomjs');
}
/**
* Ensures that the PhantomJS binary is present.
*
* Sometimes the download fails during `composer install`.
*
* @command tests:configure-phantomjs
*/
public function setupPhantomJs() {
/** @var \Acquia\Blt\Robo\Wizards\TestsWizard $tests_wizard */
$tests_wizard = $this->getContainer()->get(TestsWizard::class);
$tests_wizard->wizardInstallPhantomJsBinary();
}
/**
* Executes all behat tests in behat.paths configuration array.
*
* @throws \Exception
* Throws an exception if any Behat test fails.
*/
protected function executeBehatTests() {
$exit_code = 0;
$behat_paths = $this->getConfigValue('behat.paths');
if (is_string($behat_paths)) {
$behat_paths = [$behat_paths];
}
foreach ($behat_paths as $behat_path) {
// Output errors.
// @todo replace base_url in behat config when internal server is being used.
$task = $this->taskBehat($this->getConfigValue('composer.bin') . '/behat')
->format('pretty')
->arg($behat_path)
->option('colors')
->noInteraction()
->printMetadata(FALSE)
->stopOnFail()
->option('strict')
->option('config', $this->getConfigValue('behat.config'))
->option('profile', $this->getConfigValue('behat.profile'))
->option('tags', $this->getConfigValue('behat.tags'))
->detectInteractive();
if ($this->output()->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
$task->verbose();
}
if ($this->getConfigValue('behat.extra')) {
$task->arg($this->getConfigValue('behat.extra'));
}
$result = $task->run();
if (!$result->wasSuccessful()) {
throw new BltException("Behat tests failed!");
}
}
}
}