This article is for those who want to implement Behavior-Driven Development (BDD) using Behat in their Drupal projects in general. It is also helpful for those who are working on a collection of similar sites like a multi-site setup or an Acquia Cloud Site Factory setup where sites exhibit similar behavior.
Hereâs what weâll cover.
- How to configure behat.yml for multiple sites.
- How to configure and create a PHP script to feed multiple URLs to Behat.
- How to generate corresponding reports for each site the test runs on.
Create a composer.json file in the root directory and paste the extensions you want to install. Here is a reference composer.json file.
Open a terminal, navigate to the projectâs root directory and paste the following command:
Every *.feature file formally consists of a single feature. Lines with the keyword Feature: followed by three organized lines start a feature.
A feature normally includes a list of scenarios. User can write until the first scenario, which starts with Scenario: on a new line. User can apply tags to combination features and scenarios together.
Every scenario consists of a record of steps, which must begin with one of the keywords Given, When, Then, But or And. Behat employs them all equally.
Take a look at the feature file below:
Feature file 1
Feature file 2
Goutte Vs Selenium 2
Below GoutteDriver is a pure PHP library available through Composer.
$ composer require behat/mink-goutte-driver
Selenium2Driver is mostly used in JS calls. This is the beauty of using Minkâyou can get the best of these drivers just by configuring them in your YML file.
Selenium2Driver is available through Composer:
$ composer require behat/mink-selenium2-driver
If you are considering Goutte Vs Selenium 2, you can see a feature comparison in the screenshot below:
Take a look at this reference screenshot:
The reason behind this implementation
When manual testing, if a QA engineer needs to test more than 300 sites with similar scenarios, that would be a hectic task involving a lot of time and resources. To make this task easier, weâve found a solution to test multiple Drupal sites with single script execution.
There are a few reasons behind this implementation:
- Drupal Core update.
- Helpful to test more than 300 sites
- Manual QA can be a complex task in terms of the resources and number of days involved
We will list all the sites in a single file and have a script loop over it.
Create a file called âsites.csvâ and place it in the root directory. In this file, you are supposed to add the site URLs and report unique name.
BehatSite1,dev-behatsite1.pantheonsite.iobehatSite1 - HTML file name. When the report is generated, this filename will be used.
Dev-behatsite1.pantheonsite.io - Website URL name
- Feed URLs to PHP script
It read the URLs from CSV file one by one via php script.
- PHP script iterates through the list
Set the loop and pick URLs one by one and test accordingly.
- Run Behat per site and generate reports
Execute the Behat script with command âRunbehat.phpâ and it will generate the HTML reports.
Here is a reference video recording for script execution:
Extension used to generate structured HTML reports
Here is the extension: âemuse/behat-html-formatterâ
Command: composer require emuse/behat-html-formatter
This will create an HTML formatter for Behat.
Here is a reference image for reports structure:
Here is the behat.yml configuration, where you will notice that we didnât define âBase_urlâ because it will automatically be detected in behat.yml.
The simple yet powerful Script that runs our tests
Here is the âRunbehat.phpâ file which is the main source. The functions have been written for single script execution.
Command: PHP runbehat.php
You can see the code here.
With âemuse/behat-html-formatterâ, extension reports will be generated. Take a look at the example report screenshot below.