forked from acquia/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBehatCommand.php
206 lines (185 loc) · 6.06 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
<?php
namespace Acquia\Blt\Robo\Commands\Tests;
use Acquia\Blt\Robo\Wizards\TestsWizard;
use Robo\Contract\VerbosityThresholdInterface;
/**
* 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;
/**
* This hook will fire for all commands in this command file.
*
* @hook init
*/
public function initialize() {
parent::initialize();
$this->seleniumLogFile = $this->getConfigValue('reports.localDir') . "/selenium2.log";
$this->seleniumUrl = "http://127.0.0.1:4444/wd/hub";
}
/**
* 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.
*
* @interactLaunchPhpWebServer
* @interactGenerateSettingsFiles
* @interactInstallDrupal
* @interactConfigureBehat
* @validateMySqlAvailable
* @validateDrupalIsInstalled
* @validateBehatIsConfigured
* @validateInsideVm
*/
public function behat() {
// Log config for debugging purposes.
$this->logConfig($this->getConfigValue('behat'), 'behat');
$this->logConfig($this->getInspector()->getLocalBehatConfig()->export());
$this->createReportsDir();
$this->launchWebDriver();
$this->executeBehatTests();
$this->killWebDriver();
}
/**
* Launch the appropriate web driver based on configuration.
*/
protected function launchWebDriver() {
if ($this->getConfigValue('behat.launch-phantomjs')) {
$this->launchPhantomJs();
}
elseif ($this->getConfigValue('behat.launch-selenium')) {
$this->launchSelenium();
}
}
/**
* Kills the appropriate web driver based on configuration.
*/
protected function killWebDriver() {
if ($this->getConfigValue('behat.launch-phantomjs')) {
$this->killPhantomJs();
}
elseif ($this->getConfigValue('behat.launch-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 4444 -log {$this->seleniumLogFile} > /dev/null 2>&1")
->background(TRUE)
->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('4444');
$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()->isPhantomJsConfigured()) {
$this->setupPhantomJs();
}
$this->killPhantomJs();
$this->say("Launching PhantomJS GhostDriver.");
$this->taskExec("{$this->getConfigValue('composer.bin')}/phantomjs")
->option("webdriver", 4444)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->background()
->timeout(6000)
->silent(true)
->run();
}
/**
* Kills any running PhantomJS processes.
*/
protected function killPhantomJs() {
$this->getContainer()->get('executor')->killProcessByPort('4444');
$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
*
* @validatePhantomJsIsConfigured
*/
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() {
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)
->noInteraction()
->printMetadata(FALSE)
->stopOnFail()
->option('strict')
->option('config', $this->getConfigValue('behat.config'))
->option('profile', $this->getConfigValue('behat.profile'))
->option('tags', $this->getConfigValue('behat.tags'))
// @todo Make verbose if blt.verbose is true.
->interactive(TRUE);
if ($this->getConfigValue('behat.extra')) {
$task->arg($this->getConfigValue('behat.extra'));
}
$result = $task->run();
if (!$result->wasSuccessful()) {
throw new \Exception("Behat tests failed");
}
}
}
}