Change record status: 
Project: 
Introduced in branch: 
8.5.x
Introduced in version: 
8.5.0
Description: 

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!

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

tedbow’s picture

I ran into an error when running selenium-standalone install

not found: java
Usage: selenium-standalone action [options]

I am using DrupalVm. It doesn't install Java. The directions should probably be updated for this.

mxr576’s picture

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

GrandmaGlassesRopeMan’s picture

fwiw, I didn't need sudo and got some strange errors about file permissions when using it.

zrpnr’s picture

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 of null 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

Nathaniel’s picture

pasan.gamage’s picture

New commands are
To install selenium on mac,
brew install selenium-server
To run the server locally,
selenium-server standalone --port 4444