Skip to content

Commit 78f8cc3

Browse files
committed
Allow selenium image configuration
1 parent ce309ac commit 78f8cc3

File tree

6 files changed

+66
-31
lines changed

6 files changed

+66
-31
lines changed

docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).
1010

1111
## [Unreleased]
12+
13+
### Added
14+
- `--selenium` option or `MOODLE_BEHAT_SELENIUM_IMAGE` env variable to `behat` to specify Selenium Docker image.
15+
1216
### Changed
1317
- 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.
1418
- ACTION SUGGESTED: In order to avoid the node 16 deprecation warnings, update your workflows to use `actions/checkout@v4`.

docs/CLI.md

+12-2
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

+21-26
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 'selenium/standalone-chrome:3';
216+
}
217+
218+
if ($this->usesLegacyPhpWebdriver()) {
219+
return 'selenium/standalone-firefox:2.53.1';
220+
}
221+
222+
return 'selenium/standalone-firefox:3';
223+
}
229224
}

tests/Command/BehatCommandTest.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ protected function executeCommand($pluginDir = null, $moodleDir = null, array $c
4646
$cmdOptions
4747
);
4848
$commandTester->execute($cmdOptions);
49-
$this->lastCmd = $command->execute->lastCmd; // We need this for assertions against the command run.
49+
50+
// We need these for assertions against the commands run.
51+
$this->allCmds = $command->execute->allCmds;
52+
$this->lastCmd = $command->execute->lastCmd;
5053

5154
return $commandTester;
5255
}
@@ -69,6 +72,22 @@ public function testExecuteWithTags()
6972
$this->assertDoesNotMatchRegularExpression('/--tags=@local_ci/', $this->lastCmd);
7073
}
7174

75+
public function testExecuteWithSeleniumImageOption()
76+
{
77+
$commandTester = $this->executeCommand(null, null, ['--start-servers' => true, '--selenium' => 'seleniarm/standalone-chromium:latest']);
78+
$this->assertSame(0, $commandTester->getStatusCode());
79+
$this->assertMatchesRegularExpression('/seleniarm\/standalone-chromium:latest/', $this->allCmds[1]);
80+
}
81+
82+
public function testExecuteWithSeleniumImageEnv()
83+
{
84+
putenv('MOODLE_BEHAT_SELENIUM_IMAGE=seleniarm/standalone-chromium:latest');
85+
86+
$commandTester = $this->executeCommand(null, null, ['--start-servers' => true]);
87+
$this->assertSame(0, $commandTester->getStatusCode());
88+
$this->assertMatchesRegularExpression('/seleniarm\/standalone-chromium:latest/', $this->allCmds[1]);
89+
}
90+
7291
public function testExecuteWithName()
7392
{
7493
$featureName = 'With "double quotes" and \'single quotes\'';

tests/Command/PHPUnitCommandTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ protected function executeCommand($pluginDir = null, $moodleDir = null, array $c
4545
$cmdOptions
4646
);
4747
$commandTester->execute($cmdOptions);
48-
$this->lastCmd = $command->execute->lastCmd; // We need this for assertions against the command run.
48+
49+
// We need these for assertions against the commands run.
50+
$this->allCmds = $command->execute->allCmds;
51+
$this->lastCmd = $command->execute->lastCmd;
4952

5053
return $commandTester;
5154
}

tests/MoodleTestCase.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ class MoodleTestCase extends FilesystemTestCase
1616
{
1717
protected string $moodleDir;
1818
protected string $pluginDir;
19-
protected string $lastCmd = ''; // We need this for assertions against the command run.
19+
20+
// We need these for assertions against the commands run.
21+
protected array $allCmds = [];
22+
protected string $lastCmd = '';
2023

2124
protected function setUp(): void
2225
{
2326
parent::setUp();
2427

2528
$this->moodleDir = $this->tempDir;
2629
$this->pluginDir = $this->tempDir . '/local/ci';
30+
$this->allCmds = [];
2731
$this->lastCmd = '';
2832

2933
$this->fs->mirror(__DIR__ . '/Fixture/moodle', $this->moodleDir);

0 commit comments

Comments
 (0)