Skip to content

Commit d8c573c

Browse files
committed
Allow selenium image configuration
1 parent 8124fe7 commit d8c573c

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
1010

1111
## [Unreleased]
1212

13+
### Added
14+
- `--selenium` option to `behat` to specify Selenium Docker image.
15+
1316
### Changed
1417
- Updated all uses of `actions/checkout` from `v3` (using node 16) to `v4` (using node 20), because [actions using node 16 are deprecated](https://github.blog/changelog/2023-09-22-github-actions-transitioning-from-node-16-to-node-20/) and will stop working in the future.
1518
* ACTION SUGGESTED: In order to avoid the node 16 deprecation warnings, update your workflows to use `actions/checkout@v4`.

docs/CLI.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ Run Behat on a plugin
241241

242242
### Usage
243243

244-
* `behat [-m|--moodle MOODLE] [-p|--profile PROFILE] [--suite SUITE] [--tags TAGS] [--name NAME] [--start-servers] [--auto-rerun AUTO-RERUN] [--dump] [--] <plugin>`
244+
* `behat [-m|--moodle MOODLE] [-p|--profile PROFILE] [--suite SUITE] [--tags TAGS] [--name NAME] [--start-servers] [--auto-rerun AUTO-RERUN] [--selenium SELENIUM] [--dump] [--] <plugin>`
245245

246246
Run Behat on a plugin
247247

@@ -327,6 +327,16 @@ Number of times to rerun failures
327327
* Is negatable: no
328328
* Default: `2`
329329

330+
#### `--selenium`
331+
332+
Selenium Docker image
333+
334+
* Accept value: yes
335+
* Is value required: no
336+
* Is multiple: no
337+
* Is negatable: no
338+
* Default: `NULL`
339+
330340
#### `--dump`
331341

332342
Print contents of Behat failure HTML files
@@ -2385,4 +2395,4 @@ Do not ask any interactive question
23852395
* Is value required: no
23862396
* Is multiple: no
23872397
* Is negatable: no
2388-
* Default: `false`
2398+
* Default: `false`

src/Command/BehatCommand.php

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,6 @@ class BehatCommand extends AbstractMoodleCommand
2525
{
2626
use ExecuteTrait;
2727

28-
/**
29-
* Selenium legacy Firefox image.
30-
*/
31-
private string $seleniumLegacyFirefoxImage = 'selenium/standalone-firefox:2.53.1';
32-
33-
/**
34-
* Selenium standalone Firefox image.
35-
*
36-
* @todo: Make this configurable.
37-
*/
38-
private string $seleniumFirefoxImage = 'selenium/standalone-firefox:3';
39-
40-
/**
41-
* Selenium standalone Chrome image.
42-
*
43-
* @todo: Make this configurable.
44-
*/
45-
private string $seleniumChromeImage = 'selenium/standalone-chrome:3';
46-
4728
/**
4829
* Wait this many microseconds for Selenium server to start/stop.
4930
*
@@ -65,6 +46,7 @@ protected function configure(): void
6546
->addOption('name', null, InputOption::VALUE_REQUIRED, 'Behat name option to use', '')
6647
->addOption('start-servers', null, InputOption::VALUE_NONE, 'Start Selenium and PHP servers')
6748
->addOption('auto-rerun', null, InputOption::VALUE_REQUIRED, 'Number of times to rerun failures', 2)
49+
->addOption('selenium', null, InputOption::VALUE_NONE, 'Selenium Docker image')
6850
->addOption('dump', null, InputOption::VALUE_NONE, 'Print contents of Behat failure HTML files')
6951
->setDescription('Run Behat on a plugin');
7052
}
@@ -150,13 +132,7 @@ private function startServerProcesses(InputInterface $input): void
150132
$profile = getenv('MOODLE_BEHAT_DEFAULT_BROWSER') ?: (getenv('MOODLE_APP') ? 'chrome' : 'firefox');
151133
}
152134

153-
if ($profile === 'chrome') {
154-
$image = $this->seleniumChromeImage;
155-
} elseif ($this->usesLegacyPhpWebdriver()) {
156-
$image = $this->seleniumLegacyFirefoxImage;
157-
} else {
158-
$image = $this->seleniumFirefoxImage;
159-
}
135+
$image = $this->getSeleniumImage($input, $profile);
160136

161137
$cmd = [
162138
'docker',
@@ -226,4 +202,23 @@ private function usesLegacyPhpWebdriver(): bool
226202

227203
return strpos(file_get_contents($composerlock), 'instaclick/php-webdriver') !== false;
228204
}
205+
206+
private function getSeleniumImage(InputInterface $input, string $profile): string
207+
{
208+
$image = $input->getOption('selenium') ?? getenv('MOODLE_BEHAT_SELENIUM_IMAGE');
209+
210+
if (!empty($image)) {
211+
return $image;
212+
}
213+
214+
if ($profile === 'chrome') {
215+
return getenv('MOODLE_BEHAT_SELENIUM_CHROME_IMAGE') ?: 'selenium/standalone-chrome:3';
216+
}
217+
218+
if ($this->usesLegacyPhpWebdriver()) {
219+
return 'selenium/standalone-firefox:2.53.1';
220+
}
221+
222+
return getenv('MOODLE_BEHAT_SELENIUM_FIREFOX_IMAGE') ?: 'selenium/standalone-firefox:3';
223+
}
229224
}

tests/Command/BehatCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ public function testExecuteWithTags()
6969
$this->assertDoesNotMatchRegularExpression('/--tags=@local_ci/', $this->lastCmd);
7070
}
7171

72+
public function testExecuteWithSeleniumImage()
73+
{
74+
$commandTester = $this->executeCommand(null, null, ['--selenium' => 'seleniarm/standalone-chromium:latest']);
75+
$this->assertSame(0, $commandTester->getStatusCode());
76+
$this->assertMatchesRegularExpression('/seleniarm\/standalone-chromium:latest/', $this->lastCmd);
77+
}
78+
7279
public function testExecuteWithName()
7380
{
7481
$featureName = 'With "double quotes" and \'single quotes\'';

0 commit comments

Comments
 (0)