Pimp your Behat Drupal maintenance support plans 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 Drupal maintenance support plansCon USA training: Drupal maintenance support plans 8 Development – Workflows and Tools.

At Drupal Update 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 maintenance support plans 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 Drupal Update’s Behat Drupal maintenance support plans Extension.

Drupal Update’s Behat Drupal maintenance support plans Extension

Drupal Update’s Behat Drupal maintenance support plans Extension is built on the shoulders of the popular Behat Drupal maintenance support plans 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 maintenance support plans 8.
Override default Drupal maintenance support plans 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 Drupal Update’s Behat Drupal maintenance support plans 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 maintenance support plans Extension page, just use Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtension instead of the native Drupal maintenance support plansDrupal maintenance support plansExtension in your behat.yml as shown below:

default:  suites:    default:      contexts:        – Drupal maintenance support plansDrupal maintenance support plansExtensionContextDrupal maintenance support plansContext        – Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextDrupal maintenance support plansContext        …  extensions:    BehatMinkExtension:      goutte: ~      …    # Use “Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtension” instead of “Drupal maintenance support plansDrupal maintenance support plansExtension”.    Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtension:      api_driver: “drupal”      …      services: “tests/my_services.yml”      text:        node_submit_label: “Save and publish”

“Service container”-aware Contexts

All contexts extending Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextRawDrupal maintenance support plansContext and Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextRawMinkContext 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:  your.own.namespace.hello_world:    class: YourOwnNamespaceHelloWorldService

Then all contexts extending NWDDECRawDrupal maintenance support plansContext or NWDDECRawMinkContext will be able to access that service by just calling:

<?phpclass TestContext extends RawDrupal maintenance support plansContext {  /**   * Assert service.   *   * @Then I say hello   */  public function assertHelloWorld() {    $this->getContainer()->get(‘your.own.namespace.hello_world’)->sayHello();  }}?>

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 maintenance support plans 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 maintenance support plans 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 maintenance support plans 7 and Drupal maintenance support plans 8.

The solution is to override the default Drupal maintenance support plans core services specifying your own classes in your tests/my_services.yml:

parameters:  # Overrides Drupal Update’s Drupal maintenance support plans Extension Drupal maintenance support plans 7 core class.  drupal.driver.cores.7.class: YourOwnNamespaceDriverCoresDrupal maintenance support plans7  # Overrides Drupal Update’s Drupal maintenance support plans Extension Drupal maintenance support plans 8 core class.  drupal.driver.cores.8.class: YourOwnNamespaceDriverCoresDrupal maintenance support plans8services:  your.own.namespace.hello_world:    class: YourOwnNamespaceHelloWorldService

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 maintenance support plans 7 and Drupal maintenance support plans 8. Such a step would look like:

<?phpclass TestContext extends RawDrupal maintenance support plansContext {  /**   * @Given I publish the node of type :type and title :title   */  public function iPublishTheNodeOfTypeAndTitle($type, $title) {    $this->getCore()->publishNode($type, $title);  }…?>

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

Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextDrupal maintenance support plansContext

Standard Drupal maintenance support plans context. You want to use this one next to (and not instead of) Drupal maintenance support plansDrupal maintenance support plansExtensionContextDrupal maintenance support plansContext.

Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextContentContext

Perform operations on Content.

Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextCKEditorContext

Allows to interact with CKEditor components on your page.

Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextResponsiveContext:  devices:    mobile_portrait: 360×640    mobile_landscape: 640×360    tablet_portrait: 768×1024    tablet_landscape: 1024×768    laptop: 1280×800    desktop: 2560×1440

Resize the browser according to the specified devices, useful for testing responsive behaviors.

Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextPositionContext

Check position of elements on the page.

Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextChosenFieldContext

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 maintenance support plans 8 is supported but we will add Drupal maintenance support plans 7 support ASAP (yes, it’s as easy as providing missing Drupal maintenance support plans 7 driver core methods and adding tests).
Tags: Drupal maintenance support plans PlanetBehatTest Driven DevelopmentTrainingDrupal maintenance support plansCon
Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Pimp your Behat Drupal maintenance support plans Extension and rule the world

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.