Skip to content

Commit

Permalink
Configure browser capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Feb 15, 2024
1 parent 8124fe7 commit 347b9e4
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 31 deletions.
15 changes: 9 additions & 6 deletions res/template/config.php.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ $CFG->behat_wwwroot = '{{BEHATWWWROOT}}';
$CFG->behat_faildump_path = '{{BEHATDUMP}}';
$CFG->behat_profiles = [
'default' => [
'browser' => '{{BEHATDEFAULTBROWSER}}',
'wd_host' => '{{BEHATWDHOST}}',
'browser' => '{{BEHATDEFAULTBROWSER}}',
'wd_host' => '{{BEHATWDHOST}}',
'capabilities' => {{BEHATDEFAULTCAPABILITIES}},
],
'chrome' => [
'browser' => 'chrome',
'wd_host' => '{{BEHATWDHOST}}',
'browser' => 'chrome',
'wd_host' => '{{BEHATWDHOST}}',
'capabilities' => {{BEHATCHROMECAPABILITIES}},
],
'firefox' => [
'browser' => 'firefox',
'wd_host' => '{{BEHATWDHOST}}',
'browser' => 'firefox',
'wd_host' => '{{BEHATWDHOST}}',
'capabilities' => {{BEHATFIREFOXCAPABILITIES}},
],
];

Expand Down
44 changes: 25 additions & 19 deletions src/Bridge/MoodleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,31 @@ class MoodleConfig
*/
public function createContents(AbstractDatabase $database, string $dataDir): string
{
$template = file_get_contents(__DIR__ . '/../../res/template/config.php.txt');
$variables = [
'{{DBTYPE}}' => $database->type,
'{{DBLIBRARY}}' => $database->library,
'{{DBHOST}}' => $database->host,
'{{DBPORT}}' => $database->port,
'{{DBNAME}}' => $database->name,
'{{DBUSER}}' => $database->user,
'{{DBPASS}}' => $database->pass,
'{{WWWROOT}}' => 'http://localhost/moodle',
'{{DATAROOT}}' => $dataDir,
'{{PHPUNITDATAROOT}}' => $dataDir . '/phpu_moodledata',
'{{BEHATDATAROOT}}' => $dataDir . '/behat_moodledata',
'{{BEHATDUMP}}' => $dataDir . '/behat_dump',
'{{BEHATWWWROOT}}' => getenv('MOODLE_BEHAT_WWWROOT') ?: 'http://localhost:8000',
'{{BEHATWDHOST}}' => getenv('MOODLE_BEHAT_WDHOST') ?: 'http://localhost:4444/wd/hub',
'{{BEHATDEFAULTBROWSER}}' => getenv('MOODLE_BEHAT_DEFAULT_BROWSER') ?: (getenv('MOODLE_APP') ? 'chrome' : 'firefox'),
'{{BEHATIONICWWWROOT}}' => getenv('MOODLE_APP') ? 'http://localhost:8100' : (getenv('MOODLE_BEHAT_IONIC_WWWROOT') ?: ''),
'{{EXTRACONFIG}}' => self::PLACEHOLDER,
$template = file_get_contents(__DIR__ . '/../../res/template/config.php.txt');
$behatdefaultbrowser = getenv('MOODLE_BEHAT_DEFAULT_BROWSER') ?: (getenv('MOODLE_APP') ? 'chrome' : 'firefox');
$behatchromecapabilities = getenv('MOODLE_BEHAT_CHROME_CAPABILITIES') ?: '[]';
$behatfirefoxcapabilities = getenv('MOODLE_BEHAT_FIREFOX_CAPABILITIES') ?: '[]';
$variables = [
'{{DBTYPE}}' => $database->type,
'{{DBLIBRARY}}' => $database->library,
'{{DBHOST}}' => $database->host,
'{{DBPORT}}' => $database->port,
'{{DBNAME}}' => $database->name,
'{{DBUSER}}' => $database->user,
'{{DBPASS}}' => $database->pass,
'{{WWWROOT}}' => 'http://localhost/moodle',
'{{DATAROOT}}' => $dataDir,
'{{PHPUNITDATAROOT}}' => $dataDir . '/phpu_moodledata',
'{{BEHATDATAROOT}}' => $dataDir . '/behat_moodledata',
'{{BEHATDUMP}}' => $dataDir . '/behat_dump',
'{{BEHATWWWROOT}}' => getenv('MOODLE_BEHAT_WWWROOT') ?: 'http://localhost:8000',
'{{BEHATWDHOST}}' => getenv('MOODLE_BEHAT_WDHOST') ?: 'http://localhost:4444/wd/hub',
'{{BEHATDEFAULTBROWSER}}' => $behatdefaultbrowser,
'{{BEHATIONICWWWROOT}}' => getenv('MOODLE_APP') ? 'http://localhost:8100' : (getenv('MOODLE_BEHAT_IONIC_WWWROOT') ?: ''),
'{{BEHATDEFAULTCAPABILITIES}}' => $behatdefaultbrowser === 'chrome' ? $behatchromecapabilities : $behatfirefoxcapabilities,
'{{BEHATCHROMECAPABILITIES}}' => $behatchromecapabilities,
'{{BEHATFIREFOXCAPABILITIES}}' => $behatfirefoxcapabilities,
'{{EXTRACONFIG}}' => self::PLACEHOLDER,
];

return str_replace(array_keys($variables), array_values($variables), $template);
Expand Down
24 changes: 24 additions & 0 deletions tests/Bridge/MoodleConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

class MoodleConfigTest extends FilesystemTestCase
{
protected function setUp(): void
{
putenv('MOODLE_BEHAT_CHROME_CAPABILITIES=');
putenv('MOODLE_BEHAT_FIREFOX_CAPABILITIES=');
}

public function testCreateContents()
{
$config = new MoodleConfig();
Expand All @@ -26,6 +32,24 @@ public function testCreateContents()
$this->assertSame(file_get_contents(__DIR__ . '/../Fixture/example-config.php'), $contents);
}

public function testConfigureChromeBrowserCapabilities()
{
putenv("MOODLE_BEHAT_CHROME_CAPABILITIES=['extra_capabilities'=>['chromeOptions'=>['args'=>['--ignore-certificate-errors','--allow-running-insecure-content']]]]");
$config = new MoodleConfig();
$contents = $config->createContents(new MySQLDatabase(), '/path/to/moodledata');

$this->assertSame(file_get_contents(__DIR__ . '/../Fixture/example-config-with-chrome-capabilities.php'), $contents);
}

public function testConfigureFirefoxBrowserCapabilities()
{
putenv("MOODLE_BEHAT_FIREFOX_CAPABILITIES=['extra_capabilities'=>['firefoxOptions'=>['args'=>['-headless']]]]");
$config = new MoodleConfig();
$contents = $config->createContents(new MySQLDatabase(), '/path/to/moodledata');

$this->assertSame(file_get_contents(__DIR__ . '/../Fixture/example-config-with-firefox-capabilities.php'), $contents);
}

public function testInjectLineIntoConfig()
{
$before = <<<'EOT'
Expand Down
66 changes: 66 additions & 0 deletions tests/Fixture/example-config-with-chrome-capabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php // Moodle configuration file

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost = 'localhost';
$CFG->dbname = 'moodle';
$CFG->dbuser = 'root';
$CFG->dbpass = '';
$CFG->prefix = 'mdl_';
$CFG->dboptions = [
'dbport' => '',
];

$CFG->wwwroot = 'http://localhost/moodle';
$CFG->dataroot = '/path/to/moodledata';
$CFG->admin = 'admin';

$CFG->directorypermissions = 02777;

// Show debugging messages.
$CFG->debug = (E_ALL | E_STRICT);
$CFG->debugdisplay = 1;

// No emails.
$CFG->noemailever = true;
$CFG->noreplyaddress = '[email protected]';

// App settings.
$CFG->behat_ionic_wwwroot = '';

// PHPUnit settings.
$CFG->phpunit_prefix = 'phpu_';
$CFG->phpunit_dataroot = '/path/to/moodledata/phpu_moodledata';

// Behat settings.
$CFG->behat_prefix = 'behat_';
$CFG->behat_dataroot = '/path/to/moodledata/behat_moodledata';
$CFG->behat_wwwroot = 'http://localhost:8000';
$CFG->behat_faildump_path = '/path/to/moodledata/behat_dump';
$CFG->behat_profiles = [
'default' => [
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => [],
],
'chrome' => [
'browser' => 'chrome',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => ['extra_capabilities'=>['chromeOptions'=>['args'=>['--ignore-certificate-errors','--allow-running-insecure-content']]]],
],
'firefox' => [
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => [],
],
];

// Extra config.

require_once(__DIR__.'/lib/setup.php');
// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!
66 changes: 66 additions & 0 deletions tests/Fixture/example-config-with-firefox-capabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php // Moodle configuration file

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost = 'localhost';
$CFG->dbname = 'moodle';
$CFG->dbuser = 'root';
$CFG->dbpass = '';
$CFG->prefix = 'mdl_';
$CFG->dboptions = [
'dbport' => '',
];

$CFG->wwwroot = 'http://localhost/moodle';
$CFG->dataroot = '/path/to/moodledata';
$CFG->admin = 'admin';

$CFG->directorypermissions = 02777;

// Show debugging messages.
$CFG->debug = (E_ALL | E_STRICT);
$CFG->debugdisplay = 1;

// No emails.
$CFG->noemailever = true;
$CFG->noreplyaddress = '[email protected]';

// App settings.
$CFG->behat_ionic_wwwroot = '';

// PHPUnit settings.
$CFG->phpunit_prefix = 'phpu_';
$CFG->phpunit_dataroot = '/path/to/moodledata/phpu_moodledata';

// Behat settings.
$CFG->behat_prefix = 'behat_';
$CFG->behat_dataroot = '/path/to/moodledata/behat_moodledata';
$CFG->behat_wwwroot = 'http://localhost:8000';
$CFG->behat_faildump_path = '/path/to/moodledata/behat_dump';
$CFG->behat_profiles = [
'default' => [
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => ['extra_capabilities'=>['firefoxOptions'=>['args'=>['-headless']]]],
],
'chrome' => [
'browser' => 'chrome',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => [],
],
'firefox' => [
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => ['extra_capabilities'=>['firefoxOptions'=>['args'=>['-headless']]]],
],
];

// Extra config.

require_once(__DIR__.'/lib/setup.php');
// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!
15 changes: 9 additions & 6 deletions tests/Fixture/example-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@
$CFG->behat_faildump_path = '/path/to/moodledata/behat_dump';
$CFG->behat_profiles = [
'default' => [
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => [],
],
'chrome' => [
'browser' => 'chrome',
'wd_host' => 'http://localhost:4444/wd/hub',
'browser' => 'chrome',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => [],
],
'firefox' => [
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
'capabilities' => [],
],
];

Expand Down

0 comments on commit 347b9e4

Please sign in to comment.