Welcome to Drupal Driver’ documentation!¶
The Drupal Drivers are a collection of light-weight drivers with a common interface for interacting with Drupal. These are generally intended for testing, and are not meant to be API-complete.
Contents:
Installation¶
To utilize the Drupal Drivers in your own project, they are installed via composer.
{
"require": {
"drupal/drupal-driver": "~1.0"
}
}
and then install and run composer
$> curl -sS http://getcomposer.org/installer | php
$> php composer.phar install
If you plan on using the Drush driver, then you need to ensure that the behat-drush-endpoint is available in the target Drupal site. There are two ways to do this:
- Copy the files manually. The project can be found at:
https://github.com/drush-ops/behat-drush-endpoint
- Use Composer.
If you are using Composer to manage your Drupal site, then you only need to require drupal/drupal-driver and composer/installers, and the behat-drush-endpoint files will be copied to the correct location.
Comparison of Drivers¶
The available drivers for interacting with your site, which are compatible with Drupal 7, and 8. Each driver has its own limitiations.
Feature | Blackbox | Drush | Drupal API |
---|---|---|---|
Create users | No | Yes | Yes |
Create nodes | No | [*] | Yes |
Create vocabularies | No | No | Yes |
Create taxonomy terms | No | [*] | Yes |
Run tests and site on different servers | Yes | Yes | No |
- [*] Possible if behat.d7.drush.inc or behat.d8.drush.inc,
- as appropriate, is installed in the target Drupal site.
Usage¶
Drupal API driver¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php
use Drupal\Driver\DrupalDriver;
use Drupal\Driver\Cores\Drupal8;
require 'vendor/autoload.php';
// Path to Drupal.
$path = './drupal-8';
// Host.
$uri = 'http://d8.devl';
$driver = new DrupalDriver($path, $uri);
$driver->setCoreFromVersion();
// Bootstrap Drupal.
$driver->bootstrap();
// Create a node.
$node = (object) array(
'type' => 'article',
'uid' => 1,
'title' => $driver->getRandom()->name(),
);
$driver->createNode($node);
|
Drush driver¶
1 2 3 4 5 6 7 8 9 10 | <?php
use Drupal\Driver\DrushDriver;
...
$alias = '@mysite';
$driver = new DrushDriver($alias);
...
|
Blackbox¶
Note, the blackbox driver has no ability to control Drupal, and is provided as a fallback for when some tests can run without such access.
Any testing application should catch unsupported driver exceptions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php
use Drupal\Driver\BlackboxDriver;
use Drupal\Driver\Exception\UnsupportedDriverActionException;
...
$driver = new BlackboxDriver($alias);
try {
// Create a node.
$node = (object) array(
'type' => 'article',
'uid' => 1,
'title' => $driver->getRandom()->name(),
);
$driver->createNode($node);
}
catch (UnsupportedDriverActionException $e) {
// Mark test as skipped.
}
|
Practical example with PHPUnit¶
By using the phpunit/mink project in conjunction with the Drupal Driver, one can use PHPUnit to drive browser sessions and control Drupal.
To install:
1 2 3 4 5 6 | {
"require": {
"aik099/phpunit-mink": "~2.0",
"drupal/drupal-driver": "~1.0"
}
}
|
and then, in the tests directory, a sample test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <?php
use aik099\PHPUnit\BrowserTestCase;
use Drupal\Driver\DrupalDriver;
class GeneralTest extends BrowserTestCase
{
/**
* @var \Drupal\Driver\DriverInterface
*/
protected static $driver;
// Path to a Drupal install. This example assumes the directory is in the same one as the `composer.json` file.
protected static $drupalRoot = './drupal';
// Url to the homepage of the Drupal install.
protected static $uri = 'http://d8.devl';
public static $browsers = array(
// Selenium info.
array(
'host' => 'localhost',
'port' => 4444,
'browserName' => 'firefox',
'baseUrl' => 'http://d8.devl',
),
);
public static function setUpBeforeClass() {
self::$driver = new DrupalDriver(static::$drupalRoot, static::$uri);
self::$driver->setCoreFromVersion();
self::$driver->bootstrap();
}
public function testUsingSession()
{
// This is Mink's Session.
$session = $this->getSession();
// Go to a page.
$session->visit(static::$uri);
// Validate text presence on a page.
$this->assertTrue($session->getPage()->hasContent('Site-Install'));
}
public function testUsingBrowser()
{
// Prints the name of used browser.
echo sprintf(
"I'm executed using '%s' browser",
$this->getBrowser()->getBrowserName()
);
}
public function testNodeCreate() {
$drupal = self::$driver;
$node = (object) [
'title' => $drupal->getRandom()->string(),
'type' => 'article',
];
$drupal->createNode($node);
$session = $this->getSession();
$session->visit(static::$uri . '/node/' . $node->nid);
$this->assertTrue($session->getPage()->hasContent($node->title));
}
}
|
\ Sort by:\ best rated\ newest\ oldest\
\\
Add a comment\ (markup):
\``code``
, \ code blocks:::
and an indented block after blank line