Pimp your Behat Drupal Extension and rule the world
Make the most out of your Behat tests by using custom contexts, dependency injection and much more.
This post is an excerpt from the topics covered by our DrupalCon Dublin training: Drupal 8 Development - Workflows and Tools.
At Nuvole we consider writing good tests as a fundamental part of development and, when it comes to testing a complex site, there is nothing better than extensive behavioral tests using Behat. The benefits of such a choice are quite obvious:
- Tests are very easy to write.
- Behat scenarios serve as a solid communication mean between business and developers.
As a site grows in complexity, however, the default step definitions provided by the excellent Behat Drupal Extension might not be specific enough and you will quickly find yourself adding custom step to your FeatureContext
or creating custom Behat contexts, as advocated by all official documentation.
This is all fine except that your boilerplate test code might soon start to grow into a non-reusable, non-tested bunch of code.
Enter Nuvole's Behat Drupal Extension.
Nuvole's Behat Drupal Extension
Nuvole's Behat Drupal Extension is built on the shoulders of the popular Behat Drupal Extension and it focuses on step re-usability and testability by allowing developers to:
- Organize their code in services by providing a
YAML
service description file, pretty much like we all are used to do nowadays with Drupal 8. - Override default Drupal Behat Extension services with their own.
- Benefit of many ready-to-use contexts that are provided by the extension out of the box.
Installation and setup
Install Nuvole's Behat Drupal Extension with Composer by running:
bash$ composer require nuvoleweb/drupal-behat
Setup the extension by following the Quick start section available on the original Behat Drupal Extension page, just use NuvoleWeb\Drupal\DrupalExtension
instead of the native Drupal\DrupalExtension
in your behat.yml
as shown below:
default:<br> suites:<br> default:<br> contexts:<br> - Drupal\DrupalExtension\Context\DrupalContext<br> - NuvoleWeb\Drupal\DrupalExtension\Context\DrupalContext<br> ...<br> extensions:<br> Behat\MinkExtension:<br> goutte: ~<br> ...<br> # Use "NuvoleWeb\Drupal\DrupalExtension" instead of "Drupal\DrupalExtension".<br> NuvoleWeb\Drupal\DrupalExtension:<br> api_driver: "drupal"<br> ...<br> services: "tests/my_services.yml"<br> text:<br> node_submit_label: "Save and publish"
"Service container"-aware Contexts
All contexts extending \NuvoleWeb\Drupal\DrupalExtension\Context\RawDrupalContext
and \NuvoleWeb\Drupal\DrupalExtension\Context\RawMinkContext
are provided with direct access to the current Behat service container. Developers can also define their own services by adding a YAML
description file to their project and setting the services:
parameter to point to its current location (as shown above).
The service description file can describe both custom services and override already defined services. For example, given a tests/my_services.yml
containing:
services:<br> your.own.namespace.hello_world:<br> class: Your\Own\Namespace\HelloWorldService
Then all contexts extending \NW\D\DE\C\RawDrupalContext
or \NW\D\DE\C\RawMinkContext
will be able to access that service by just calling:
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">class </span><span style="color: #0000BB">TestContext </span><span style="color: #007700">extends </span><span style="color: #0000BB">RawDrupalContext </span><span style="color: #007700">{<br><br> </span><span style="color: #FF8000">/**<br> * Assert service.<br> *<br> * @Then I say hello<br> */<br> </span><span style="color: #007700">public function </span><span style="color: #0000BB">assertHelloWorld</span><span style="color: #007700">() {<br> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getContainer</span><span style="color: #007700">()-></span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'your.own.namespace.hello_world'</span><span style="color: #007700">)-></span><span style="color: #0000BB">sayHello</span><span style="color: #007700">();<br> }<br><br>}<br></span><span style="color: #0000BB">?></span></span>
The your.own.namespace.hello_world
service class itself can be easily tested using PHPUnit. Also, since Behat uses Symfony's Service Container you can list services your service depends on as arguments so to remove any hardcoded dependency, following Dependency Injection best practices.
Override existing services
Say that, while working on your Drupal 7 project, you have defined a step that publishes a node given its content type and title and you want to use the same exact step on your Drupal 8 project, something like:
Given I publish the node of type "page" and title "My page title"
The problem here is that the actual API calls to load and save a node differs between Drupal 7 and Drupal 8.
The solution is to override the default Drupal core services specifying your own classes in your tests/my_services.yml
:
parameters:<br> # Overrides Nuvole's Drupal Extension Drupal 7 core class.<br> drupal.driver.cores.7.class: Your\Own\Namespace\Driver\Cores\Drupal7<br> # Overrides Nuvole's Drupal Extension Drupal 8 core class.<br> drupal.driver.cores.8.class: Your\Own\Namespace\Driver\Cores\Drupal8<br><br>services:<br> your.own.namespace.hello_world:<br> class: Your\Own\Namespace\HelloWorldService
You'll then delegate the core-specific business logic to the new core classes allowing your custom step to be transparently run on both Drupal 7 and Drupal 8. Such a step would look like:
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">class </span><span style="color: #0000BB">TestContext </span><span style="color: #007700">extends </span><span style="color: #0000BB">RawDrupalContext </span><span style="color: #007700">{<br><br> </span><span style="color: #FF8000">/**<br> * @Given I publish the node of type :type and title :title<br> */<br> </span><span style="color: #007700">public function </span><span style="color: #0000BB">iPublishTheNodeOfTypeAndTitle</span><span style="color: #007700">(</span><span style="color: #0000BB">$type</span><span style="color: #007700">, </span><span style="color: #0000BB">$title</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getCore</span><span style="color: #007700">()-></span><span style="color: #0000BB">publishNode</span><span style="color: #007700">(</span><span style="color: #0000BB">$type</span><span style="color: #007700">, </span><span style="color: #0000BB">$title</span><span style="color: #007700">);<br> }<br><br>...<br></span><span style="color: #0000BB">?></span></span>
Ready to use contexts
The extension also provides some utility contexts that you can use right away in your tests. Below a quick overview of what's currently available:
Context
Description
NuvoleWeb\Drupal\DrupalExtension\Context\DrupalContext <br>
Standard Drupal context. You want to use this one next to (and not instead of) Drupal\DrupalExtension\Context\DrupalContext
.
NuvoleWeb\Drupal\DrupalExtension\Context\ContentContext<br>
Perform operations on Content.
NuvoleWeb\Drupal\DrupalExtension\Context\CKEditorContext<br>
Allows to interact with CKEditor components on your page.
NuvoleWeb\Drupal\DrupalExtension\Context\ResponsiveContext:<br> devices:<br> mobile_portrait: 360x640<br> mobile_landscape: 640x360<br> tablet_portrait: 768x1024<br> tablet_landscape: 1024x768<br> laptop: 1280x800<br> desktop: 2560x1440<br>
Resize the browser according to the specified devices, useful for testing responsive behaviors.
NuvoleWeb\Drupal\DrupalExtension\Context\PositionContext<br>
Check position of elements on the page.
NuvoleWeb\Drupal\DrupalExtension\Context\ChosenFieldContext<br>
Interact with Chosen elements on the page.
We will share more steps in the future enriching the current contexts as well as providing new ones so keep an eye on the project repository!
Disclaimer
At the moment only Drupal 8 is supported but we will add Drupal 7 support ASAP (yes, it's as easy as providing missing Drupal 7 driver core methods and adding tests).
Tags: Drupal PlanetBehatTest Driven DevelopmentTrainingDrupalCon