/
RunCommand.php
388 lines (342 loc) · 13.9 KB
/
RunCommand.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php declare(strict_types=1);
namespace Lmc\Steward\Console\Command;
use Facebook\WebDriver\Remote\WebDriverBrowserType;
use Lmc\Steward\Console\CommandEvents;
use Lmc\Steward\Console\Configuration\ConfigOptions;
use Lmc\Steward\Console\Event\BasicConsoleEvent;
use Lmc\Steward\Console\Event\ExtendedConsoleEvent;
use Lmc\Steward\Process\ExecutionLoop;
use Lmc\Steward\Process\MaxTotalDelayStrategy;
use Lmc\Steward\Process\ProcessSetCreator;
use Lmc\Steward\Publisher\XmlPublisher;
use Lmc\Steward\Selenium\SeleniumServerAdapter;
use OndraM\CiDetector\CiDetector;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
/**
* Run tests command is used to start Steward test planner and execute tests one by one,
* optionally with defined delay and relations.
*/
class RunCommand extends Command
{
/** @var SeleniumServerAdapter */
protected $seleniumAdapter;
/** @var ProcessSetCreator */
protected $processSetCreator;
/** @var array Lowercase name => WebDriver identifier */
protected $supportedBrowsers = [
'firefox' => WebDriverBrowserType::FIREFOX,
'chrome' => WebDriverBrowserType::CHROME,
'microsoftedge' => WebDriverBrowserType::MICROSOFT_EDGE,
'internet explorer' => WebDriverBrowserType::IE,
'safari' => WebDriverBrowserType::SAFARI,
];
public const ARGUMENT_ENVIRONMENT = 'environment';
public const ARGUMENT_BROWSER = 'browser';
public const OPTION_SERVER_URL = 'server-url';
public const OPTION_CAPABILITY = 'capability';
public const OPTION_TESTS_DIR = 'tests-dir';
public const OPTION_LOGS_DIR = 'logs-dir';
public const OPTION_PATTERN = 'pattern';
public const OPTION_GROUP = 'group';
public const OPTION_EXCLUDE_GROUP = 'exclude-group';
public const OPTION_FILTER = 'filter';
public const OPTION_NO_EXIT = 'no-exit';
public const OPTION_IGNORE_DELAYS = 'ignore-delays';
public const OPTION_PARALLEL_LIMIT = 'parallel-limit';
/**
* @internal
*/
public function setSeleniumAdapter(SeleniumServerAdapter $seleniumAdapter): void
{
$this->seleniumAdapter = $seleniumAdapter;
}
/**
* @internal
*/
public function setProcessSetCreator(ProcessSetCreator $processSetCreator): void
{
$this->processSetCreator = $processSetCreator;
}
/**
* Configure command
*/
protected function configure(): void
{
$this->setName('run')
->setDescription('Run tests planner and execute tests')
->addArgument(
self::ARGUMENT_ENVIRONMENT,
InputArgument::REQUIRED,
'Environment name (must be specified to avoid unintentional run against production)'
)
->addArgument(
self::ARGUMENT_BROWSER,
InputArgument::REQUIRED,
'Browser in which tests should be run'
)
->addOption(
self::OPTION_SERVER_URL,
null,
InputOption::VALUE_REQUIRED,
'Selenium server (hub) URL ',
'http://localhost:4444/wd/hub'
)
->addOption(
self::OPTION_CAPABILITY,
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Extra DesiredCapabilities to be passed to WebDriver, use format capabilityName:value'
)
->addOption(
self::OPTION_TESTS_DIR,
null,
InputOption::VALUE_REQUIRED,
'Path to directory with tests',
STEWARD_BASE_DIR . DIRECTORY_SEPARATOR . 'tests'
)
->addOption(
self::OPTION_LOGS_DIR,
null,
InputOption::VALUE_REQUIRED,
'Path to directory with logs',
STEWARD_BASE_DIR . DIRECTORY_SEPARATOR . 'logs'
)
->addOption(
self::OPTION_PATTERN,
null,
InputOption::VALUE_REQUIRED,
'Pattern for test files to be run',
'*Test.php'
)
->addOption(
self::OPTION_GROUP,
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Only run testcases with specified @group of this name'
)
->addOption(
self::OPTION_EXCLUDE_GROUP,
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Exclude testcases with specified @group from being run'
)
->addOption(
self::OPTION_FILTER,
null,
InputOption::VALUE_REQUIRED,
'Run only testcases/tests with name matching this filter'
)
->addOption(
self::OPTION_NO_EXIT,
null,
InputOption::VALUE_NONE,
'Always exit with code 0 <comment>(by default any failed test causes the command to return 1)</comment>'
)
->addOption(
self::OPTION_IGNORE_DELAYS,
'i',
InputOption::VALUE_NONE,
'Ignore delays defined between testcases'
)
->addOption(
self::OPTION_PARALLEL_LIMIT,
'l',
InputOption::VALUE_REQUIRED,
'Number of maximum testcases being executed in a parallel',
50
);
$this->addUsage('staging firefox');
$this->addUsage(
'--group=foo --group=bar --exclude-group=baz --server-url=http://localhost:4444/wd/hub -vv staging chrome'
);
$this->getDispatcher()->dispatch(CommandEvents::CONFIGURE, new BasicConsoleEvent($this));
}
/**
* Initialize, check arguments and options values etc.
*/
protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);
$output->writeln(
sprintf(
'<info>Steward</info> <comment>%s</comment> is running the tests...%s',
$this->getApplication()->getVersion(),
(!(new CiDetector())->isCiDetected() ? ' Just for you <fg=red><3</fg=red>!' : '')
)
);
// If browser name or env is empty, ends initialization and let the Console/Command fail on input validation
if (empty($input->getArgument(self::ARGUMENT_BROWSER))
|| empty($input->getArgument(self::ARGUMENT_ENVIRONMENT))
) {
return;
}
// Browser name is case insensitive, normalize it to lower case
$browserNormalized = mb_strtolower($input->getArgument(self::ARGUMENT_BROWSER));
// Check if browser is supported
if (!isset($this->supportedBrowsers[$browserNormalized])) {
throw new \RuntimeException(
sprintf(
'Browser "%s" is not supported (use one of: %s)',
$browserNormalized,
implode(', ', array_keys($this->supportedBrowsers))
)
);
}
// Set WebDriver browser identifier back to the argument value
$input->setArgument(self::ARGUMENT_BROWSER, $this->supportedBrowsers[$browserNormalized]);
if ($output->isVerbose()) {
$output->writeln(sprintf('Browser: %s', $input->getArgument(self::ARGUMENT_BROWSER)));
$output->writeln(sprintf('Environment: %s', $input->getArgument(self::ARGUMENT_ENVIRONMENT)));
}
// Initialize Selenium server adapter and normalize server URL
$seleniumAdapter = $this->getSeleniumAdapter($input->getOption(self::OPTION_SERVER_URL));
$input->setOption(self::OPTION_SERVER_URL, $seleniumAdapter->getServerUrl());
// Make sure parallel-limit is greater than 0
$parallelLimit = (int) $input->getOption(self::OPTION_PARALLEL_LIMIT);
if ($parallelLimit === 0) {
throw new \RuntimeException('Parallel limit must be a whole number greater than 0');
}
$input->setOption(self::OPTION_PARALLEL_LIMIT, $parallelLimit);
$this->getDispatcher()->dispatch(
CommandEvents::RUN_TESTS_INIT,
new ExtendedConsoleEvent($this, $input, $output)
);
if ($output->isVeryVerbose()) {
$output->writeln(
sprintf('Path to logs: %s', $this->config[ConfigOptions::LOGS_DIR])
);
$output->writeln(
sprintf('Ignore delays: %s', ($input->getOption(self::OPTION_IGNORE_DELAYS)) ? 'yes' : 'no')
);
$output->writeln(
sprintf('Parallel limit: %d', $input->getOption(self::OPTION_PARALLEL_LIMIT))
);
}
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->testSeleniumConnection()) {
return 1;
}
// Find all files holding test-cases
if ($this->io->isVeryVerbose()) {
$this->io->writeln('Searching for testcases:');
$this->io->writeln(sprintf(' - in directory "%s"', $this->config[ConfigOptions::TESTS_DIR]));
$this->io->writeln(sprintf(' - by pattern "%s"', $input->getOption(self::OPTION_PATTERN)));
}
$files = (new Finder())
->files()
->in($this->config[ConfigOptions::TESTS_DIR])
->name($input->getOption(self::OPTION_PATTERN));
if (!count($files)) {
$this->io->error(
sprintf(
'No testcases found, exiting.%s',
!$this->io->isVeryVerbose() ? ' (use -vv or -vvv option for more information)' : ''
)
);
return 1;
}
$processSetCreator = $this->getProcessSetCreator($input, $this->io);
$processSet = $processSetCreator->createFromFiles(
$files,
$input->getOption(self::OPTION_GROUP),
$input->getOption(self::OPTION_EXCLUDE_GROUP),
$input->getOption(self::OPTION_FILTER),
$input->getOption(self::OPTION_IGNORE_DELAYS)
);
if (!count($processSet)) {
$this->io->error('No testcases matched given groups, exiting.');
return 1;
}
$maxParallelLimit = $input->getOption(self::OPTION_PARALLEL_LIMIT);
$executionLoop = new ExecutionLoop($processSet, $this->io, new MaxTotalDelayStrategy(), $maxParallelLimit);
$allTestsPassed = $executionLoop->start();
if ($input->getOption(self::OPTION_NO_EXIT)) {
return 0;
}
return $allTestsPassed ? 0 : 1;
}
/**
* @codeCoverageIgnore
*/
protected function getSeleniumAdapter(string $seleniumServerUrl): SeleniumServerAdapter
{
if ($this->seleniumAdapter === null) {
$this->seleniumAdapter = new SeleniumServerAdapter($seleniumServerUrl);
}
return $this->seleniumAdapter;
}
/**
* @codeCoverageIgnore
*/
protected function getProcessSetCreator(InputInterface $input, OutputInterface $output): ProcessSetCreator
{
if ($this->processSetCreator === null) {
$xmlPublisher = new XmlPublisher();
$xmlPublisher->setFileDir($this->config[ConfigOptions::LOGS_DIR]);
$xmlPublisher->clean();
$this->processSetCreator = new ProcessSetCreator($this, $input, $output, $xmlPublisher, $this->config);
}
return $this->processSetCreator;
}
/**
* Try connection to Selenium server
*/
protected function testSeleniumConnection(): bool
{
$seleniumAdapter = $this->seleniumAdapter;
$this->io->write(
sprintf('Selenium server (hub) url: %s, trying connection...', $seleniumAdapter->getServerUrl()),
false,
OutputInterface::VERBOSITY_VERY_VERBOSE
);
if (!$seleniumAdapter->isAccessible()) {
$this->io->writeln(
sprintf(
'<error>%s ("%s")</error>',
$this->io->isVeryVerbose() ? 'connection error' : 'Error connecting to Selenium server',
$seleniumAdapter->getLastError()
)
);
$this->io->error(
sprintf(
'Make sure your Selenium server is really accessible on url "%s" '
. 'or change it using --server-url option',
$seleniumAdapter->getServerUrl()
)
);
return false;
}
if (!$seleniumAdapter->isSeleniumServer()) {
$this->io->writeln(
sprintf(
'<error>%s (%s)</error>',
$this->io->isVeryVerbose() ? 'unexpected response' : 'Unexpected response from Selenium server',
$seleniumAdapter->getLastError()
)
);
$this->io->error(
sprintf(
'URL "%s" is occupied by something else than Selenium server. Make sure Selenium server is really'
. ' accessible on this URL or change it using --server-url option.'
. ' If using Selenium 3.x, also make sure you are not be missing "/wd/hub" part in the server URL.',
$seleniumAdapter->getServerUrl()
)
);
return false;
}
if ($this->io->isVeryVerbose()) {
$cloudService = $seleniumAdapter->getCloudService();
$this->io->writeln(
'OK' . ($cloudService ? ' (' . $cloudService . ' cloud service detected)' : ''),
OutputInterface::VERBOSITY_VERY_VERBOSE
);
}
return true;
}
}