Skip to content

Commit c86c2ec

Browse files
committed
Add support for Chrome for Behat
1 parent 168b6e6 commit c86c2ec

File tree

10 files changed

+94
-32
lines changed

10 files changed

+94
-32
lines changed

Diff for: .travis.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ addons:
99
packages:
1010
- oracle-java8-installer
1111
- oracle-java8-set-default
12+
- chromium-chromedriver
1213

1314
cache:
1415
directories:
@@ -25,6 +26,7 @@ env:
2526
- MUSTACHE_IGNORE_NAMES=broken.mustache
2627
- DB=pgsql
2728
- MOODLE_BRANCH=master
29+
- PROFILE=chrome
2830

2931
install:
3032
- phpenv config-rm xdebug.ini
@@ -52,7 +54,7 @@ script:
5254
- moodle-plugin-ci mustache
5355
- moodle-plugin-ci grunt
5456
- moodle-plugin-ci phpunit --coverage-text
55-
- moodle-plugin-ci behat
57+
- moodle-plugin-ci behat --profile $PROFILE
5658

5759
jobs:
5860
include:
@@ -69,7 +71,7 @@ jobs:
6971
- php: 7.0
7072
env: MOODLE_BRANCH=MOODLE_33_STABLE
7173
- php: 5.6
72-
env: MOODLE_BRANCH=MOODLE_32_STABLE DB=mysqli
74+
env: MOODLE_BRANCH=MOODLE_32_STABLE DB=mysqli PROFILE=default
7375
- stage: Deploy
7476
addons: skip
7577
install: skip

Diff for: docs/AddExtraConfig.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Sometimes a plugin may require extra config in the Moodle `config.php` file beca
77
non-standard service. This project provides a way to update the `config.php` file with the `add-config` command. Here
88
is an example of how to use it in your `.travis.yml` file:
99

10-
```yml
10+
```yaml
1111
install:
1212
- moodle-plugin-ci install
1313
- moodle-plugin-ci add-config '$CFG->foo = "bar";'

Diff for: docs/AddExtraPlugins.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Sometimes the plugin that you are testing may depend on another plugin or even s
77
provides a way to Git clone the extra plugins and add them to the Moodle test site. Here is an example of how to use
88
it in your `.travis.yml` file:
99

10-
```yml
10+
```yaml
1111
install:
1212
- moodle-plugin-ci add-plugin moodlehq/moodle-local_hub
1313
- moodle-plugin-ci install
@@ -21,7 +21,7 @@ By default, the branch that is cloned is the `master` branch. You can use the `
2121
this behavior. If you use the same branch names as Moodle (EG: `MOODLE_XY_STABLE`), then a handy trick is to pass
2222
the `$MOODLE_BRANCH` build variable to the `add-plugin` command. Here is an example:
2323

24-
```yml
24+
```yaml
2525
install:
2626
- moodle-plugin-ci add-plugin --branch $MOODLE_BRANCH username/project
2727
- moodle-plugin-ci install
@@ -30,7 +30,7 @@ install:
3030
If you are not using GitHub and want to provide your own Git clone URL, then you can use the `--clone` (`-c`) option.
3131
Here is an example (Note, you can use the `--branch` option together with the `--clone` option if you need to):
3232

33-
```yml
33+
```yaml
3434
install:
3535
- moodle-plugin-ci add-plugin --clone https://bitbucket.org/username/project.git
3636
- moodle-plugin-ci install

Diff for: docs/CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
1515
environment from Precise to Trusy. On Trusty, the default Firefox version is 55, which is not
1616
compatible with Selenium.
1717

18+
### Added
19+
- Can now use Chrome with Behat, see [help document](Chrome.md) for details.
20+
1821
### Changed
1922
- Set password via environment when connecting with Postgres.
2023

@@ -42,7 +45,7 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
4245
- The Composer self update step from `.travis.dist.yml`.
4346

4447
### Added
45-
- Defining ignore files per command, see [help document](IgnoringFiles.md) for details.
48+
- Defining ignore files per command, see [help document](IgnoringFiles.md) for details.
4649
- `moodle-plugin-ci mustache` command which lints your Mustache template files.
4750
- `moodle-plugin-ci grunt` command which runs Grunt tasks on the plugin. See [help document](TravisFileExplained.md)
4851
for more details about the command.

Diff for: docs/Chrome.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
layout: page
3+
title: Using Chrome with Behat
4+
---
5+
6+
First, a word of warning. On Travis CI, you cannot specify the version of Chrome to install
7+
which might lead to compatibility problems as Travis CI updates their environment. So,
8+
the most _stable_ route is to keep using Firefox. Currently Chrome is working with the `Trusy`
9+
Travis CI environment and the versions as of writing this are:
10+
11+
* ChromeDriver 2.29
12+
* Chromium 60
13+
14+
So, why would you want to use Chrome then? Speed. It is much faster than Firefox for executing
15+
Behat tests. If you find that you are waiting too long for your build results or if you find
16+
that your build exceeds max execution time, then using Chrome with Behat could help. To use
17+
Chrome, modify your `.travis.yml` file.
18+
19+
Under `addons` add `chromium-chromedriver`:
20+
21+
```yaml
22+
addons:
23+
# ...other stuff.
24+
apt:
25+
packages:
26+
# ...other stuff.
27+
- chromium-chromedriver
28+
```
29+
30+
The under `script` update your Behat command:
31+
32+
```yaml
33+
script:
34+
# ...other stuff.
35+
- moodle-plugin-ci behat --profile chrome
36+
```
37+
38+
## Troubleshooting
39+
40+
If Behat starts to misbehave, please revert back to using Firefox to see if it is an issue with
41+
using Chrome or not.
42+
43+
If you ever need to see the version of Chrome or ChromeDriver in your build, then you can add these
44+
to your `.travis.yml` file:
45+
46+
```yaml
47+
install:
48+
- /usr/lib/chromium-browser/chromedriver --version
49+
- chromium-browser --version
50+
# ...other stuff.
51+
```

Diff for: docs/Help.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ changed. Also a good place to look for new goodies.
1414
* [Add extra Moodle configs](AddExtraConfig.html): how to add extra configs to Moodle `config.php`.
1515
* [Add extra plugins](AddExtraPlugins.html): how to add plugin dependencies to Moodle.
1616
* [Ignoring files](IgnoringFiles.html): how to ignore files that might be causing failures.
17+
* [Using Chrome with Behat](Chrome.md): how to use Chrome instead of Firefox for Behat.
1718
* [Testing a plugin against PHP7](PHP7.html): how to test your plugin against PHP7.
1819
* [Generating code coverage](CodeCoverage.html): how to generate code coverage of your plugin.
1920

Diff for: docs/IgnoringFiles.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ In addition, you can ignore additional files by defining `IGNORE_PATHS` and/or `
1515
in your `.travis.yml` file. These environment variables wont work for Grunt tasks, but will for everything else.
1616
Example:
1717

18-
```yml
18+
```yaml
1919
env:
2020
global:
2121
- MOODLE_BRANCH=MOODLE_29_STABLE
@@ -34,7 +34,7 @@ like `/\.php$/`.
3434
If you need to specify ignore paths for a specific command, then you can define additional environment variables. The
3535
variable names are the same as above, but prefixed with `COMMANDNAME_`. Example:
3636

37-
```yml
37+
```yaml
3838
env:
3939
global:
4040
- MOODLE_BRANCH=MOODLE_29_STABLE

Diff for: res/template/config.php.txt

+7-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@ $CFG->behat_wwwroot = '{{BEHATWWWROOT}}';
3838
$CFG->behat_faildump_path = '{{BEHATDUMP}}';
3939
$CFG->behat_profiles = [
4040
'default' => [
41-
'browser' => 'firefox',
42-
'wd_host' => 'http://localhost:4444/wd/hub',
43-
'extensions' => [
44-
'Behat\MinkExtension' => [
45-
'selenium2' => [
46-
'browser' => 'firefox',
47-
]
48-
]
49-
]
50-
]
41+
'browser' => 'firefox',
42+
'wd_host' => 'http://localhost:4444/wd/hub',
43+
],
44+
'chrome' => [
45+
'browser' => 'chrome',
46+
'wd_host' => 'http://localhost:4444/wd/hub',
47+
],
5148
];
5249

5350
{{EXTRACONFIG}}

Diff for: src/Command/BehatCommand.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected function configure()
4545
$jar = getenv('MOODLE_SELENIUM_JAR') !== false ? getenv('MOODLE_SELENIUM_JAR') : null;
4646

4747
$this->setName('behat')
48+
->addOption('profile', 'p', InputOption::VALUE_REQUIRED, 'Behat profile to use', 'default')
4849
->addOption('start-servers', null, InputOption::VALUE_NONE, 'Start Selenium and PHP servers')
4950
->addOption('jar', null, InputOption::VALUE_REQUIRED, 'Path to Selenium Jar file', $jar)
5051
->addOption('auto-rerun', null, InputOption::VALUE_REQUIRED, 'Number of times to rerun failures', 2)
@@ -71,12 +72,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
7172
$servers = $input->getOption('start-servers');
7273
}
7374

74-
$servers && $this->startServerProcesses($input->getOption('jar'));
75+
$servers && $this->startServerProcesses($input->getOption('jar'), $input);
7576

7677
$builder = ProcessBuilder::create()
7778
->setPrefix('php')
7879
->add('admin/tool/behat/cli/run.php')
7980
->add('--tags=@'.$this->plugin->getComponent())
81+
->add('--profile='.$input->getOption('profile'))
8082
->add('--auto-rerun='.$input->getOption('auto-rerun'))
8183
->add('--verbose')
8284
->add('-vvv')
@@ -97,12 +99,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
9799
return $process->isSuccessful() ? 0 : 1;
98100
}
99101

100-
private function startServerProcesses($seleniumJarFile)
102+
private function startServerProcesses($seleniumJarFile, InputInterface $input)
101103
{
102104
if (!is_file($seleniumJarFile)) {
103105
throw new \InvalidArgumentException(sprintf('Invalid Selenium Jar file path: %s', $seleniumJarFile));
104106
}
105-
$selenium = new Process(sprintf('xvfb-run -a --server-args="-screen 0 1024x768x24" java -jar %s', $seleniumJarFile));
107+
$cmd = sprintf('xvfb-run -a --server-args="-screen 0 1024x768x24" java -jar %s', $seleniumJarFile);
108+
if ($input->getOption('profile') === 'chrome') {
109+
$driver = '/usr/lib/chromium-browser/chromedriver';
110+
if (!file_exists($driver)) {
111+
throw new \RuntimeException('chromedriver not found, please install it, see help docs');
112+
}
113+
$cmd .= ' -Dwebdriver.chrome.driver='.$driver;
114+
}
115+
116+
$selenium = new Process($cmd);
106117
$selenium->setTimeout(0);
107118
$selenium->disableOutput();
108119
$selenium->start();

Diff for: tests/Fixture/example-config.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@
3838
$CFG->behat_faildump_path = '/path/to/moodledata/behat_dump';
3939
$CFG->behat_profiles = [
4040
'default' => [
41-
'browser' => 'firefox',
42-
'wd_host' => 'http://localhost:4444/wd/hub',
43-
'extensions' => [
44-
'Behat\MinkExtension' => [
45-
'selenium2' => [
46-
'browser' => 'firefox',
47-
]
48-
]
49-
]
50-
]
41+
'browser' => 'firefox',
42+
'wd_host' => 'http://localhost:4444/wd/hub',
43+
],
44+
'chrome' => [
45+
'browser' => 'chrome',
46+
'wd_host' => 'http://localhost:4444/wd/hub',
47+
],
5148
];
5249

5350
// Extra config.

0 commit comments

Comments
 (0)