As part of 8.5.x we are introducing a new way to test Javascript code. Previously, we used PhantomJS as announced in https://www.drupal.org/node/2716803.
Requirements
In order to run test using webdriver, you need to install selenium and chromedriver.
OS X
brew install selenium-server-standalone
brew cask install chromedriver
Linux / Windows
npm install selenium-standalone@latest -g
selenium-standalone install
Execute tests
Start the selenium server.
selenium-server -port 4444
The next step is to setup some environment variables. Therefore copy the core phpunit.xml.dist
file to phpunit.xml
and set the following lines:
<env name="SIMPLETEST_DB" value="sqlite://localhost/sites/default/files/.ht.sqlite"></env>
<env name="SIMPLETEST_BASE_URL" value="http://drupal8.dev"></env>
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["chrome", null, "http://localhost:4444/wd/hub"]'/>
Now you can execute tests using phpunit (_www
is the user of the webserver, needed for permission issues on some setups)
sudo -u _www ./vendor/bin/phpunit -v -c ./core core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebWithWebDriverAssertTest.php
Write tests
Extend \Drupal\FunctionalJavascriptTests\JavascriptTestBase and add
use Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver;
to the Test classes use statements.
protected $minkDefaultDriverClass = DrupalSelenium2Driver::class;
to the Test classes protected properties.
Debug tests
Tests will run in Chrome locally so you can watch them in the browser as the test runs!
Comments
Needs Java
I ran into an error when running
selenium-standalone install
I am using DrupalVm. It doesn't install Java. The directions should probably be updated for this.
MINK_DRIVER_CLASS
You can enforce all Javascript tests to use Webdriver with MINK_DRIVER_CLASS environment variable as per: https://github.com/drupal/core/tree/8.5.4/tests but if you do you are going to need this patch: https://www.drupal.org/project/drupal/issues/2978270
sudo not needed
fwiw, I didn't need
sudo
and got some strange errors about file permissions when using it.Chromedriver 75
in chrome 75 they changed "w3c standard" to default to true
https://bugs.chromium.org/p/chromedriver/issues/detail?id=2536&q=&colspe...
in phpunit.xml you can pass
{ "chromeOptions": { "w3c": false } }
instead ofnull
and the latest version of chromedriver should work.In your phpunit.xml file:
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["chrome", { "chromeOptions": { "w3c": false } }, "http://drupal.test:4444/wd/hub"]' />
Also see: https://www.drupal.org/project/drupal/issues/3073342
selenium-standalone start
https://www.npmjs.com/package/selenium-standalone
selenium-standalone start
Updated commands for Mac
New commands are
To install selenium on mac,
brew install selenium-server
To run the server locally,
selenium-server standalone --port 4444