Skip to content

Commit b8e0c2e

Browse files
committed
Configure browser capabilities
1 parent 90bc221 commit b8e0c2e

File tree

7 files changed

+202
-31
lines changed

7 files changed

+202
-31
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
1212

1313
### Added
1414
- `--selenium` option or `MOODLE_BEHAT_SELENIUM_IMAGE` env variable to `behat` to specify Selenium Docker image.
15+
- `MOODLE_BEHAT_CHROME_CAPABILITIES` and `MOODLE_BEHAT_FIREFOX_CAPABILITIES` env variables to configure additional browser capabilities.
1516

1617
### Changed
1718
- 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.

res/template/config.php.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,19 @@ $CFG->behat_wwwroot = '{{BEHATWWWROOT}}';
4343
$CFG->behat_faildump_path = '{{BEHATDUMP}}';
4444
$CFG->behat_profiles = [
4545
'default' => [
46-
'browser' => '{{BEHATDEFAULTBROWSER}}',
47-
'wd_host' => '{{BEHATWDHOST}}',
46+
'browser' => '{{BEHATDEFAULTBROWSER}}',
47+
'wd_host' => '{{BEHATWDHOST}}',
48+
'capabilities' => {{BEHATDEFAULTCAPABILITIES}},
4849
],
4950
'chrome' => [
50-
'browser' => 'chrome',
51-
'wd_host' => '{{BEHATWDHOST}}',
51+
'browser' => 'chrome',
52+
'wd_host' => '{{BEHATWDHOST}}',
53+
'capabilities' => {{BEHATCHROMECAPABILITIES}},
5254
],
5355
'firefox' => [
54-
'browser' => 'firefox',
55-
'wd_host' => '{{BEHATWDHOST}}',
56+
'browser' => 'firefox',
57+
'wd_host' => '{{BEHATWDHOST}}',
58+
'capabilities' => {{BEHATFIREFOXCAPABILITIES}},
5659
],
5760
];
5861

src/Bridge/MoodleConfig.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,31 @@ class MoodleConfig
3232
*/
3333
public function createContents(AbstractDatabase $database, string $dataDir): string
3434
{
35-
$template = file_get_contents(__DIR__ . '/../../res/template/config.php.txt');
36-
$variables = [
37-
'{{DBTYPE}}' => $database->type,
38-
'{{DBLIBRARY}}' => $database->library,
39-
'{{DBHOST}}' => $database->host,
40-
'{{DBPORT}}' => $database->port,
41-
'{{DBNAME}}' => $database->name,
42-
'{{DBUSER}}' => $database->user,
43-
'{{DBPASS}}' => $database->pass,
44-
'{{WWWROOT}}' => 'http://localhost/moodle',
45-
'{{DATAROOT}}' => $dataDir,
46-
'{{PHPUNITDATAROOT}}' => $dataDir . '/phpu_moodledata',
47-
'{{BEHATDATAROOT}}' => $dataDir . '/behat_moodledata',
48-
'{{BEHATDUMP}}' => $dataDir . '/behat_dump',
49-
'{{BEHATWWWROOT}}' => getenv('MOODLE_BEHAT_WWWROOT') ?: 'http://localhost:8000',
50-
'{{BEHATWDHOST}}' => getenv('MOODLE_BEHAT_WDHOST') ?: 'http://localhost:4444/wd/hub',
51-
'{{BEHATDEFAULTBROWSER}}' => getenv('MOODLE_BEHAT_DEFAULT_BROWSER') ?: (getenv('MOODLE_APP') ? 'chrome' : 'firefox'),
52-
'{{BEHATIONICWWWROOT}}' => getenv('MOODLE_APP') ? 'http://localhost:8100' : (getenv('MOODLE_BEHAT_IONIC_WWWROOT') ?: ''),
53-
'{{EXTRACONFIG}}' => self::PLACEHOLDER,
35+
$template = file_get_contents(__DIR__ . '/../../res/template/config.php.txt');
36+
$behatdefaultbrowser = getenv('MOODLE_BEHAT_DEFAULT_BROWSER') ?: (getenv('MOODLE_APP') ? 'chrome' : 'firefox');
37+
$behatchromecapabilities = getenv('MOODLE_BEHAT_CHROME_CAPABILITIES') ?: '[]';
38+
$behatfirefoxcapabilities = getenv('MOODLE_BEHAT_FIREFOX_CAPABILITIES') ?: '[]';
39+
$variables = [
40+
'{{DBTYPE}}' => $database->type,
41+
'{{DBLIBRARY}}' => $database->library,
42+
'{{DBHOST}}' => $database->host,
43+
'{{DBPORT}}' => $database->port,
44+
'{{DBNAME}}' => $database->name,
45+
'{{DBUSER}}' => $database->user,
46+
'{{DBPASS}}' => $database->pass,
47+
'{{WWWROOT}}' => 'http://localhost/moodle',
48+
'{{DATAROOT}}' => $dataDir,
49+
'{{PHPUNITDATAROOT}}' => $dataDir . '/phpu_moodledata',
50+
'{{BEHATDATAROOT}}' => $dataDir . '/behat_moodledata',
51+
'{{BEHATDUMP}}' => $dataDir . '/behat_dump',
52+
'{{BEHATWWWROOT}}' => getenv('MOODLE_BEHAT_WWWROOT') ?: 'http://localhost:8000',
53+
'{{BEHATWDHOST}}' => getenv('MOODLE_BEHAT_WDHOST') ?: 'http://localhost:4444/wd/hub',
54+
'{{BEHATDEFAULTBROWSER}}' => $behatdefaultbrowser,
55+
'{{BEHATIONICWWWROOT}}' => getenv('MOODLE_APP') ? 'http://localhost:8100' : (getenv('MOODLE_BEHAT_IONIC_WWWROOT') ?: ''),
56+
'{{BEHATDEFAULTCAPABILITIES}}' => $behatdefaultbrowser === 'chrome' ? $behatchromecapabilities : $behatfirefoxcapabilities,
57+
'{{BEHATCHROMECAPABILITIES}}' => $behatchromecapabilities,
58+
'{{BEHATFIREFOXCAPABILITIES}}' => $behatfirefoxcapabilities,
59+
'{{EXTRACONFIG}}' => self::PLACEHOLDER,
5460
];
5561

5662
return str_replace(array_keys($variables), array_values($variables), $template);

tests/Bridge/MoodleConfigTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818

1919
class MoodleConfigTest extends FilesystemTestCase
2020
{
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
putenv('MOODLE_BEHAT_CHROME_CAPABILITIES=');
26+
putenv('MOODLE_BEHAT_FIREFOX_CAPABILITIES=');
27+
}
28+
2129
public function testCreateContents()
2230
{
2331
$config = new MoodleConfig();
@@ -26,6 +34,24 @@ public function testCreateContents()
2634
$this->assertSame(file_get_contents(__DIR__ . '/../Fixture/example-config.php'), $contents);
2735
}
2836

37+
public function testConfigureChromeBrowserCapabilities()
38+
{
39+
putenv("MOODLE_BEHAT_CHROME_CAPABILITIES=['extra_capabilities'=>['chromeOptions'=>['args'=>['--ignore-certificate-errors','--allow-running-insecure-content']]]]");
40+
$config = new MoodleConfig();
41+
$contents = $config->createContents(new MySQLDatabase(), '/path/to/moodledata');
42+
43+
$this->assertSame(file_get_contents(__DIR__ . '/../Fixture/example-config-with-chrome-capabilities.php'), $contents);
44+
}
45+
46+
public function testConfigureFirefoxBrowserCapabilities()
47+
{
48+
putenv("MOODLE_BEHAT_FIREFOX_CAPABILITIES=['extra_capabilities'=>['firefoxOptions'=>['args'=>['-headless']]]]");
49+
$config = new MoodleConfig();
50+
$contents = $config->createContents(new MySQLDatabase(), '/path/to/moodledata');
51+
52+
$this->assertSame(file_get_contents(__DIR__ . '/../Fixture/example-config-with-firefox-capabilities.php'), $contents);
53+
}
54+
2955
public function testInjectLineIntoConfig()
3056
{
3157
$before = <<<'EOT'
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php // Moodle configuration file
2+
3+
unset($CFG);
4+
global $CFG;
5+
$CFG = new stdClass();
6+
7+
$CFG->dbtype = 'mysqli';
8+
$CFG->dblibrary = 'native';
9+
$CFG->dbhost = 'localhost';
10+
$CFG->dbname = 'moodle';
11+
$CFG->dbuser = 'root';
12+
$CFG->dbpass = '';
13+
$CFG->prefix = 'mdl_';
14+
$CFG->dboptions = [
15+
'dbport' => '',
16+
];
17+
18+
$CFG->wwwroot = 'http://localhost/moodle';
19+
$CFG->dataroot = '/path/to/moodledata';
20+
$CFG->admin = 'admin';
21+
22+
$CFG->directorypermissions = 02777;
23+
24+
// Show debugging messages.
25+
$CFG->debug = (E_ALL | E_STRICT);
26+
$CFG->debugdisplay = 1;
27+
28+
// No emails.
29+
$CFG->noemailever = true;
30+
$CFG->noreplyaddress = '[email protected]';
31+
32+
// App settings.
33+
$CFG->behat_ionic_wwwroot = '';
34+
35+
// PHPUnit settings.
36+
$CFG->phpunit_prefix = 'phpu_';
37+
$CFG->phpunit_dataroot = '/path/to/moodledata/phpu_moodledata';
38+
39+
// Behat settings.
40+
$CFG->behat_prefix = 'behat_';
41+
$CFG->behat_dataroot = '/path/to/moodledata/behat_moodledata';
42+
$CFG->behat_wwwroot = 'http://localhost:8000';
43+
$CFG->behat_faildump_path = '/path/to/moodledata/behat_dump';
44+
$CFG->behat_profiles = [
45+
'default' => [
46+
'browser' => 'firefox',
47+
'wd_host' => 'http://localhost:4444/wd/hub',
48+
'capabilities' => [],
49+
],
50+
'chrome' => [
51+
'browser' => 'chrome',
52+
'wd_host' => 'http://localhost:4444/wd/hub',
53+
'capabilities' => ['extra_capabilities'=>['chromeOptions'=>['args'=>['--ignore-certificate-errors','--allow-running-insecure-content']]]],
54+
],
55+
'firefox' => [
56+
'browser' => 'firefox',
57+
'wd_host' => 'http://localhost:4444/wd/hub',
58+
'capabilities' => [],
59+
],
60+
];
61+
62+
// Extra config.
63+
64+
require_once(__DIR__.'/lib/setup.php');
65+
// There is no php closing tag in this file,
66+
// it is intentional because it prevents trailing whitespace problems!
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php // Moodle configuration file
2+
3+
unset($CFG);
4+
global $CFG;
5+
$CFG = new stdClass();
6+
7+
$CFG->dbtype = 'mysqli';
8+
$CFG->dblibrary = 'native';
9+
$CFG->dbhost = 'localhost';
10+
$CFG->dbname = 'moodle';
11+
$CFG->dbuser = 'root';
12+
$CFG->dbpass = '';
13+
$CFG->prefix = 'mdl_';
14+
$CFG->dboptions = [
15+
'dbport' => '',
16+
];
17+
18+
$CFG->wwwroot = 'http://localhost/moodle';
19+
$CFG->dataroot = '/path/to/moodledata';
20+
$CFG->admin = 'admin';
21+
22+
$CFG->directorypermissions = 02777;
23+
24+
// Show debugging messages.
25+
$CFG->debug = (E_ALL | E_STRICT);
26+
$CFG->debugdisplay = 1;
27+
28+
// No emails.
29+
$CFG->noemailever = true;
30+
$CFG->noreplyaddress = '[email protected]';
31+
32+
// App settings.
33+
$CFG->behat_ionic_wwwroot = '';
34+
35+
// PHPUnit settings.
36+
$CFG->phpunit_prefix = 'phpu_';
37+
$CFG->phpunit_dataroot = '/path/to/moodledata/phpu_moodledata';
38+
39+
// Behat settings.
40+
$CFG->behat_prefix = 'behat_';
41+
$CFG->behat_dataroot = '/path/to/moodledata/behat_moodledata';
42+
$CFG->behat_wwwroot = 'http://localhost:8000';
43+
$CFG->behat_faildump_path = '/path/to/moodledata/behat_dump';
44+
$CFG->behat_profiles = [
45+
'default' => [
46+
'browser' => 'firefox',
47+
'wd_host' => 'http://localhost:4444/wd/hub',
48+
'capabilities' => ['extra_capabilities'=>['firefoxOptions'=>['args'=>['-headless']]]],
49+
],
50+
'chrome' => [
51+
'browser' => 'chrome',
52+
'wd_host' => 'http://localhost:4444/wd/hub',
53+
'capabilities' => [],
54+
],
55+
'firefox' => [
56+
'browser' => 'firefox',
57+
'wd_host' => 'http://localhost:4444/wd/hub',
58+
'capabilities' => ['extra_capabilities'=>['firefoxOptions'=>['args'=>['-headless']]]],
59+
],
60+
];
61+
62+
// Extra config.
63+
64+
require_once(__DIR__.'/lib/setup.php');
65+
// There is no php closing tag in this file,
66+
// it is intentional because it prevents trailing whitespace problems!

tests/Fixture/example-config.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,19 @@
4343
$CFG->behat_faildump_path = '/path/to/moodledata/behat_dump';
4444
$CFG->behat_profiles = [
4545
'default' => [
46-
'browser' => 'firefox',
47-
'wd_host' => 'http://localhost:4444/wd/hub',
46+
'browser' => 'firefox',
47+
'wd_host' => 'http://localhost:4444/wd/hub',
48+
'capabilities' => [],
4849
],
4950
'chrome' => [
50-
'browser' => 'chrome',
51-
'wd_host' => 'http://localhost:4444/wd/hub',
51+
'browser' => 'chrome',
52+
'wd_host' => 'http://localhost:4444/wd/hub',
53+
'capabilities' => [],
5254
],
5355
'firefox' => [
54-
'browser' => 'firefox',
55-
'wd_host' => 'http://localhost:4444/wd/hub',
56+
'browser' => 'firefox',
57+
'wd_host' => 'http://localhost:4444/wd/hub',
58+
'capabilities' => [],
5659
],
5760
];
5861

0 commit comments

Comments
 (0)