forked from acquia/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBehatCommand.php
280 lines (250 loc) · 8.16 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
<?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;
/**
* 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->seleniumPort = $this->getConfigValue('behat.selenium.port');
$this->seleniumUrl = $this->getConfigValue('behat.selenium.url');
$this->serverPort = $this->getConfigValue('behat.server.port');
$this->serverUrl = $this->getConfigValue('behat.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
* @validateSettingsFilesPresent
* @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();
}
}
/**
* 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();
}
}
/**
* 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;
foreach ($this->getConfigValue('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!");
}
}
}
}