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 

    default:
      suites:
        default:
          contexts:
            - FeatureContext
            - Drupal\DrupalExtension\Context\DrupalContext
            - Drupal\DrupalExtension\Context\MinkContext
      extensions:
        Behat\MinkExtension:
          goutte: ~
          base_url: https://www.mysite.com
          javascript_session: selenium2
          browser_name: 'chrome'
          selenium2:
            wd_host: http://localhost:4444/wd/hub
            capabilities: { "browser": "chrome", "version": "*", 'chrome': {'switches':['--start-maximized']}}
        Drupal\DrupalExtension:
          blackbox: ~
          api_driver: drupal
          drupal:
            drupal_root: web/
          region_map:
            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"
    
    local:
      extensions:
        Drupal\MinkExtension:
          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 https://chromedriver.chromium.org/ 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 -Dwebdriver.chrome.driver=./chromedriver -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

@api
@javascript
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.

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.