Automated testing with Drupal 8 + Selenium + Behat + Gherkins + Jenkins + Cucumber

Here's a simple guide on how to setup automated testing with Behat, Behat Gherkins, Selenium, Jenkins and Cucumber with Drupal 8/9.

In this guide, we will be using a Drupal 8 instance with composer.

Setup Behat for Drupal 8/9

  1. First thing we need to do is to install all the composer libraries that we need by installing drupal/drupal-extension.
    composer require drupal/drupal-extension --dev
  2. Creating a file called behat.yml inside the root directory of your project 

            - FeatureContext
            - Drupal\DrupalExtension\Context\DrupalContext
            - Drupal\DrupalExtension\Context\MinkContext
          goutte: ~
          javascript_session: selenium2
          browser_name: 'chrome'
            wd_host: http://localhost:4444/wd/hub
            capabilities: { "browser": "chrome", "version": "*", 'chrome': {'switches':['--start-maximized']}}
          blackbox: ~
          api_driver: drupal
            drupal_root: web/
            navigation: ".navbar-header"
            navigation_collapsible: "#navbar-collapse"
            header: ".region-header"
            highlighted: ".highlighted"
            help: ".region-help"
            content: ".main-content"
            sidebar_first: ".region-sidebar-first"
            sidebar_second: ".region-sidebar-second"
            footer: ".footer"
          base_url: http://mysite.localhost:8000
  3. Initialize behat. This creates the features folder where we will put all our features files and some of our custom context if we needed. Custom context will be inside the features/bootstrap directory.

    behat --init
    +d features/bootstrap - place your context classes here
    +f features/bootstrap/FeatureContext.php - place your definitions, transformations and hooks here
  4. To check if everything is working as expected, you can try listing definitions by:

    behat -dl

    You should see something like this:

    default | Given I am an anonymous user
    default | Given I am not logged in
    default | Then I log out
    default | Given I am logged in as a user with the :role role(s)
    default | Given I am logged in as a/an :role
    default | Given I am logged in as a user with the :role role(s) and I have the following fields:
    default | Given I am logged in as :name
    default | Given I am logged in as a user with the :permissions permission(s)
    default | Then I should see (the text ):text in the :rowText row
    default | Then I should not see (the text ):text in the :rowText row
    default | Given I click :link in the :rowText row
    default | Then I (should )see the :link in the :rowText row
    default | Given the cache has been cleared
    default | Given I run cron


Setup Selenium server with Chrome driver

On this part, we will be using a standalone Selenium server.

  1. Create a directory that you can easily access. E.g.:
    mkdir ~/selenium-server
  2. Download the standalone Selenium server  and move it in to the folder you just created.
  3. Download your desired browser to use for testing. In this guide, we will be using Chrome driver. Head to to download the right Chrome driver version that fits your Chrome browser version. 
  4. Extract the zip file and move the chromedriver executable file in to the same folder we created.
  5. Run the server
    cd ~/selenium-server
    java -jar selenium-server-standalone-3.141.59.jar



Create feature and run the test

Create a new file inside features folder and name it like homepage.feature

Feature: Homepage check

Scenario: Anonymous user access
	Given I am an anonymous user
	When I visit "/"
	Then I should see the text "Featured" in the "content" region

This example feature file will access the homepage as an anonymous user and will look for the text "Featured" in the "content" region (which the css selector were specified in the behat.yml)


Now let's run the test by executing:

behat --profile local

I'm running the local profile (which we also specified the url from our behat.yml) since I'm running the test from my local.