summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-02-03 09:54:09 (GMT)
committerAlex Pott2018-02-03 09:54:09 (GMT)
commit6341df364b5ced731d7b465163aa6ccec560e25a (patch)
treed69a03c29d2d18180340a7a2afc86417f60bf5a6
parentd9bd719b56a37fba097756412414197929e174b5 (diff)
Issue #2775653 by dawehner, droplet, michielnugter, Lendude, jibran, alexpott, Mixologic, klausi, juampynr, cosmicdreams, cspitzlay, justafish, drpal, larowlan, claudiu.cristea, Berdir, Mile23, andypost, xjm, mtodor: JavascriptTests with webDriver
-rw-r--r--composer.lock120
-rw-r--r--core/composer.json3
-rw-r--r--core/phpunit.xml.dist2
-rw-r--r--core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php35
-rw-r--r--core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php40
-rw-r--r--core/tests/Drupal/FunctionalJavascriptTests/LegacyJavascriptTestBase.php7
-rw-r--r--core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebWithWebDriverAssertTest.php19
-rw-r--r--core/tests/README.md66
8 files changed, 277 insertions, 15 deletions
diff --git a/composer.lock b/composer.lock
index 14a575f..29d4ecb 100644
--- a/composer.lock
+++ b/composer.lock
@@ -2897,6 +2897,67 @@
"time": "2016-03-05T09:04:22+00:00"
},
{
+ "name": "behat/mink-selenium2-driver",
+ "version": "v1.3.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/minkphp/MinkSelenium2Driver.git",
+ "reference": "473a9f3ebe0c134ee1e623ce8a9c852832020288"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/minkphp/MinkSelenium2Driver/zipball/473a9f3ebe0c134ee1e623ce8a9c852832020288",
+ "reference": "473a9f3ebe0c134ee1e623ce8a9c852832020288",
+ "shasum": ""
+ },
+ "require": {
+ "behat/mink": "~1.7@dev",
+ "instaclick/php-webdriver": "~1.1",
+ "php": ">=5.3.1"
+ },
+ "require-dev": {
+ "symfony/phpunit-bridge": "~2.7"
+ },
+ "type": "mink-driver",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.3.x-dev"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "Behat\\Mink\\Driver\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Konstantin Kudryashov",
+ "email": "ever.zet@gmail.com",
+ "homepage": "http://everzet.com"
+ },
+ {
+ "name": "Pete Otaqui",
+ "email": "pete@otaqui.com",
+ "homepage": "https://github.com/pete-otaqui"
+ }
+ ],
+ "description": "Selenium2 (WebDriver) driver for Mink framework",
+ "homepage": "http://mink.behat.org/",
+ "keywords": [
+ "ajax",
+ "browser",
+ "javascript",
+ "selenium",
+ "testing",
+ "webdriver"
+ ],
+ "time": "2016-03-05T09:10:18+00:00"
+ },
+ {
"name": "doctrine/instantiator",
"version": "1.0.5",
"source": {
@@ -3037,6 +3098,65 @@
"time": "2017-01-03T13:21:43+00:00"
},
{
+ "name": "instaclick/php-webdriver",
+ "version": "1.4.5",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/instaclick/php-webdriver.git",
+ "reference": "6fa959452e774dcaed543faad3a9d1a37d803327"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/instaclick/php-webdriver/zipball/6fa959452e774dcaed543faad3a9d1a37d803327",
+ "reference": "6fa959452e774dcaed543faad3a9d1a37d803327",
+ "shasum": ""
+ },
+ "require": {
+ "ext-curl": "*",
+ "php": ">=5.3.2"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "^4.8",
+ "satooshi/php-coveralls": "^1.0||^2.0"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.4.x-dev"
+ }
+ },
+ "autoload": {
+ "psr-0": {
+ "WebDriver": "lib/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "Apache-2.0"
+ ],
+ "authors": [
+ {
+ "name": "Justin Bishop",
+ "email": "jubishop@gmail.com",
+ "role": "Developer"
+ },
+ {
+ "name": "Anthon Pang",
+ "email": "apang@softwaredevelopment.ca",
+ "role": "Fork Maintainer"
+ }
+ ],
+ "description": "PHP WebDriver for Selenium 2",
+ "homepage": "http://instaclick.com/",
+ "keywords": [
+ "browser",
+ "selenium",
+ "webdriver",
+ "webtest"
+ ],
+ "time": "2017-06-30T04:02:48+00:00"
+ },
+ {
"name": "ircmaxell/password-compat",
"version": "v1.0.4",
"source": {
diff --git a/core/composer.json b/core/composer.json
index 1f60805..a6d52d1 100644
--- a/core/composer.json
+++ b/core/composer.json
@@ -48,7 +48,8 @@
"phpspec/prophecy": "^1.4",
"symfony/css-selector": "^3.4.0",
"symfony/phpunit-bridge": "^3.4.3",
- "symfony/debug": "^3.4.0"
+ "symfony/debug": "^3.4.0",
+ "behat/mink-selenium2-driver": "^1.3"
},
"replace": {
"drupal/action": "self.version",
diff --git a/core/phpunit.xml.dist b/core/phpunit.xml.dist
index 963d921..2a55d68 100644
--- a/core/phpunit.xml.dist
+++ b/core/phpunit.xml.dist
@@ -30,8 +30,10 @@
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value=""/>
<!-- To disable deprecation testing uncomment the next line. -->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak_vendors"/>
+ <!-- Example for changing the driver class for mink tests MINK_DRIVER_CLASS value: 'Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver' -->
<!-- Example for changing the driver args to mink tests MINK_DRIVER_ARGS value: '["http://127.0.0.1:8510"]' -->
<!-- Example for changing the driver args to phantomjs tests MINK_DRIVER_ARGS_PHANTOMJS value: '["http://127.0.0.1:8510"]' -->
+ <!-- Example for changing the driver args to webdriver tests MINK_DRIVER_ARGS_WEBDRIVER value: '["firefox", null, "http://localhost:4444/wd/hub"]' -->
</php>
<testsuites>
<testsuite name="unit">
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php
new file mode 100644
index 0000000..5f243af
--- /dev/null
+++ b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Drupal\FunctionalJavascriptTests;
+
+use Behat\Mink\Driver\Selenium2Driver;
+
+/**
+ * Provides a driver for Selenium testing.
+ */
+class DrupalSelenium2Driver extends Selenium2Driver {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setCookie($name, $value = NULL) {
+ if ($value === NULL) {
+ $this->getWebDriverSession()->deleteCookie($name);
+ return;
+ }
+
+ $cookieArray = [
+ 'name' => $name,
+ 'value' => urlencode($value),
+ 'secure' => FALSE,
+ // Unlike \Behat\Mink\Driver\Selenium2Driver::setCookie we set a domain
+ // and an expire date, as otherwise cookies leak from one test site into
+ // another.
+ 'domain' => parse_url($this->getWebDriverSession()->url(), PHP_URL_HOST),
+ 'expires' => time() + 80000,
+ ];
+
+ $this->getWebDriverSession()->setCookie($cookieArray);
+ }
+
+}
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php
index d49d46e..55b1284 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php
@@ -15,6 +15,9 @@ abstract class JavascriptTestBase extends BrowserTestBase {
/**
* {@inheritdoc}
+ *
+ * To use a webdriver based approach, please use DrupalSelenium2Driver::class.
+ * We will switch the default later.
*/
protected $minkDefaultDriverClass = PhantomJSDriver::class;
@@ -22,14 +25,19 @@ abstract class JavascriptTestBase extends BrowserTestBase {
* {@inheritdoc}
*/
protected function initMink() {
- // Set up the template cache used by the PhantomJS mink driver.
- $path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache';
- $this->minkDefaultDriverArgs = [
- 'http://127.0.0.1:8510',
- $path,
- ];
- if (!file_exists($path)) {
- mkdir($path);
+ if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
+ $this->minkDefaultDriverArgs = ['chrome', NULL, 'http://localhost:4444/'];
+ }
+ elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
+ // Set up the template cache used by the PhantomJS mink driver.
+ $path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache';
+ $this->minkDefaultDriverArgs = [
+ 'http://127.0.0.1:8510',
+ $path,
+ ];
+ if (!file_exists($path)) {
+ mkdir($path);
+ }
}
try {
@@ -67,7 +75,13 @@ abstract class JavascriptTestBase extends BrowserTestBase {
* {@inheritdoc}
*/
protected function getMinkDriverArgs() {
- return getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
+ if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
+ return getenv('MINK_DRIVER_ARGS_WEBDRIVER') ?: getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
+ }
+ elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
+ return getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
+ }
+ return parent::getMinkDriverArgs();
}
/**
@@ -176,4 +190,12 @@ EndOfScript;
return $this->getSession()->evaluateScript($script) ?: [];
}
+ /**
+ * {@inheritdoc}
+ */
+ protected function getHtmlOutputHeaders() {
+ // The webdriver API does not support fetching headers.
+ return '';
+ }
+
}
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/LegacyJavascriptTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/LegacyJavascriptTestBase.php
index 7cef5b8..b3baf6e 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/LegacyJavascriptTestBase.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/LegacyJavascriptTestBase.php
@@ -2,6 +2,8 @@
namespace Drupal\FunctionalJavascriptTests;
+use Zumba\Mink\Driver\PhantomJSDriver;
+
/**
* Runs a browser test using PhantomJS.
*
@@ -12,6 +14,11 @@ abstract class LegacyJavascriptTestBase extends JavascriptTestBase {
/**
* {@inheritdoc}
*/
+ protected $minkDefaultDriverClass = PhantomJSDriver::class;
+
+ /**
+ * {@inheritdoc}
+ */
public function assertSession($name = NULL) {
// Return a WebAssert that supports status code and header assertions.
return new JSWebAssert($this->getSession($name), $this->baseUrl);
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebWithWebDriverAssertTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebWithWebDriverAssertTest.php
new file mode 100644
index 0000000..d31a9a5
--- /dev/null
+++ b/core/tests/Drupal/FunctionalJavascriptTests/Tests/JSWebWithWebDriverAssertTest.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Drupal\FunctionalJavascriptTests\Tests;
+
+use Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver;
+
+/**
+ * Tests for the JSWebAssert class using webdriver.
+ *
+ * @group javascript
+ */
+class JSWebWithWebDriverAssertTest extends JSWebAssertTest {
+
+ /**
+ * {@inheritdoc}
+ */
+ protected $minkDefaultDriverClass = DrupalSelenium2Driver::class;
+
+}
diff --git a/core/tests/README.md b/core/tests/README.md
index 5dd6cc6..fcaa5cc 100644
--- a/core/tests/README.md
+++ b/core/tests/README.md
@@ -2,16 +2,11 @@
## Functional tests
-* Start PhantomJS:
- ```
- phantomjs --ssl-protocol=any --ignore-ssl-errors=true ./vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768 2>&1 >> /dev/null &
- ```
* Run the functional tests:
```
export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
export SIMPLETEST_BASE_URL='http://d8.dev'
./vendor/bin/phpunit -c core --testsuite functional
- ./vendor/bin/phpunit -c core --testsuite functional-javascript
```
Note: functional tests have to be invoked with a user in the same group as the
@@ -37,6 +32,67 @@ User <your-user>
Group <your-group>
```
+## Functional javascript tests
+
+Javascript tests use the Selenium2Driver which allows you to control a
+big range of browsers. By default Drupal uses chromedriver to run tests.
+For help installing and starting selenium, see http://mink.behat.org/en/latest/drivers/selenium2.html
+
+* Make sure you have a recent version of chrome installed
+
+* Install selenium-server-standalone and chromedriver
+
+Example for Mac:
+
+```
+brew install selenium-server-standalone
+brew install chromedriver
+```
+
+* Before running tests make sure that selenium-server is running
+```
+selenium-server -port 4444
+```
+
+* Set the correct driver args and run the tests:
+```
+export MINK_DRIVER_ARGS_WEBDRIVER='["chrome", null, "http://localhost:4444/wd/hub"]'
+./vendor/bin/phpunit -c core --testsuite functional-javascript
+```
+
+* It is possible to use alternate browsers if the required dependencies are
+installed. For example to use Firefox:
+
+```
+export MINK_DRIVER_ARGS_WEBDRIVER='["firefox", null, "http://localhost:4444/wd/hub"]'
+./vendor/bin/phpunit -c core --testsuite functional-javascript
+```
+
+* To force all BrowserTestBase (including legacy JavascriptTestBase) tests to use
+webdriver:
+
+```
+export MINK_DRIVER_CLASS='Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver'
+./vendor/bin/phpunit -c core --testsuite functional-javascript
+```
+
+## Running legacy javascript tests
+
+Older javascript test may use the PhantomJSDriver. To run these tests you will
+have to install and start PhantomJS.
+
+* Start PhantomJS:
+ ```
+ phantomjs --ssl-protocol=any --ignore-ssl-errors=true ./vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768 2>&1 >> /dev/null &
+ ```
+
+* Then you can run the test:
+```
+./vendor/bin/phpunit -c core --testsuite functional-javascript
+```
+
+## Running tests with a different user
+
If the default user is e.g. `www-data`, the above functional tests will have to
be invoked with sudo instead: