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
- 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
Creating a file called behat.yml inside the root directory of your project
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
To check if everything is working as expected, you can try listing definitions by:
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.
- Create a directory that you can easily access. E.g.:
- Download the standalone Selenium server and move it in to the folder you just created.
- 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.
- Extract the zip file and move the chromedriver executable file in to the same folder we created.
- 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
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.