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

8.6.x
Alex Pott 2018-02-03 09:54:09 +00:00
parent d9bd719b56
commit 6341df364b
8 changed files with 277 additions and 15 deletions

120
composer.lock generated
View File

@ -2896,6 +2896,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",
@ -3036,6 +3097,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",

View File

@ -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",

View File

@ -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">

View File

@ -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);
}
}

View File

@ -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 '';
}
}

View File

@ -2,6 +2,8 @@
namespace Drupal\FunctionalJavascriptTests;
use Zumba\Mink\Driver\PhantomJSDriver;
/**
* Runs a browser test using PhantomJS.
*
@ -9,6 +11,11 @@ namespace Drupal\FunctionalJavascriptTests;
*/
abstract class LegacyJavascriptTestBase extends JavascriptTestBase {
/**
* {@inheritdoc}
*/
protected $minkDefaultDriverClass = PhantomJSDriver::class;
/**
* {@inheritdoc}
*/

View File

@ -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;
}

View File

@ -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: