How to run Behat test automation for Drupal on Lando using Acquia BLT

Submitted by abhaisasidharan on Wed, 05/06/2020 - 19:05
Lando

Lando is a very useful tool especially for Drupal developers, because of the extensive support for Drupal. Apart from the fact that when you google Lando, it's usually Lando Calrissian that shows up in the results, there's nothing to complain about Lando.

Firstly, we need to install Lando. For those who use Linux, just download the package from here, and use either dpkg, yum, pacman or dnf to install Lando, depending on what package manager you use. Mac users just have to download the .dmg file and double click it. For Windows users, here is an article on how to install Ubuntu alongside your Windows operating system. Run lando version to see if Lando is successfully installed. Lando is well documented and you can see the installation and troubleshooting information here.

Now that Lando is successfully installed, let's install Drupal. To install Drupal all your have to do is run composer create-project drupal/recommended-project project_folder. Your composer.json file and .lando.yml file should be in this "project_folder" and your docroot will be in this folder. Docroot is where your index.php resides. In this case since you are using the drupal/recommended-project, your docroot will be "web". I usually use "docroot" as my docroot as you can see in the config below. You can use docroot, web, webroot etc. To each his own. Just make sure your .lando.yml file has the same docroot name. Now you have to configure .lando.yml file. Lando recipes are available for Drupal 7 and 8. All you have to do is run lando init and Lando will prompt you with options. It will look something like this: 

lando init
? From where should we get your app's codebase? current working directory
? What recipe do you want to use? drupal8
? Where is your webroot relative to the init destination? docroot
? What do you want to call this app? my-lando-app

  _  __                       _          
 / |/ /__ _    __  _    _____( )_______  
/    / _ \ |/|/ / | |/|/ / -_)// __/ -_)
/_/|_/\___/__,__/  |__,__/\__/ /_/  \__/  
                                         
 _________  ____  __ _______  _______  _      ______________ __  ___________  ______
/ ___/ __ \/ __ \/ //_/  _/ |/ / ___/ | | /| / /  _/_  __/ // / / __/  _/ _ \/ __/ /                                                                                                         
/ /__/ /_/ / /_/ / ,< _/ //    / (_ /  | |/ |/ // /  / / / _  / / _/_/ // , _/ _//_/                                                                                                          
\___/\____/\____/_/|_/___/_/|_/\___/   |__/|__/___/ /_/ /_//_/ /_/ /___/_/|_/___(_)                                                                                                           
                                                                                                                                                                                             
Your app has been initialized!

Go to the directory where your app was initialized and run lando start to get rolling.
Check the LOCATION printed below if you are unsure where to go.

Oh... and here are some vitals:

NAME      my-lando-app                                
LOCATION  /your/current/location                    
RECIPE    drupal8                                     
DOCS      https://docs.lando.dev/config/drupal8.html

Your .lando.yml config file should be quite simple and look like this:

name: my-lando-app
recipe: drupal8
config:
  webroot: docroot

If you want a more advanced config file where you have more control over the other aspects like database, you can see here or you can take a peek at my config file. You will probably need some of it to run Behat tests like google chrome. 

name: behattest
recipe: drupal8
config:
  webroot: docroot
  php: '7.4'
  via: nginx
  drush: ^9
  xdebug: true
services:
  database:
    type: mariadb:10.1
  appserver:
    run_as_root:
      - apt-get -y update && apt-get -y install software-properties-common
      - curl -sL https://deb.nodesource.com/setup_10.x | bash -
      - apt-get install -y nodejs
      - npm install gulp-cli gulp -g
      - wget https://dl-ssl.google.com/linux/linux_signing_key.pub && apt-key add linux_signing_key.pub
      - add-apt-repository "deb http://dl.google.com/linux/chrome/deb/ stable main"
      - apt-get -y update && apt-get -y install google-chrome-stable
  mailhog:
    type: mailhog
    hogfrom:
      - appserver
proxy:
  mailhog:
    - mail.behattest.lndo.site
  appserver_nginx:
    - behattest.lndo.site
tooling:
  robo:
    service: appserver
    description: Runs <robo> commands
    cmd: robo
  phpunit:
    service: appserver
    cmd: vendor/bin/phpunit
  npm:
    service: appserver
  node:
    service: appserver
  gulp:
    service: appserver
  blt:
    service: appserver
    cmd: /app/vendor/bin/blt
env_file:
  - .env

Once you have the .lando.yml with the required configs, all you have to do it run lando start. You should be able to access your site at http://behattest.lndo.site after the command has successfully completed. Once that is done we need to install Acquia BLT, Behat and it's dependencies. From here on, we do everything within Lando. 

BLT has a lot of commands to run specific tests and initialize tests. 

lando composer require acquia/blt
lando blt tests:behat:init:config

This will generate blt.yml and local.blt.yml file. Make the necessary changes to both to fit your project. This is what my local.blt.yml looks like.

project:
  machine_name: behattest
  prefix: BTT
  human_name: 'Behat Test'
  profile:
    name: minimal
  local:
    protocol: http
    hostname: behattest.lndo.site
deploy:
  tag_source: true
drush:
  aliases:
    remote: '${project.machine_name}.dev'
    local: self
    ci: self
  default_alias: '${drush.aliases.local}'
cm:
  strategy: core-only
  core:
    # The parent directory for configuration directories, relative to the docroot.
    path: ../config/default
    key: sync
    install_from_config: true
    dirs:
      # Corresponding value is defined in config.settings.php.
      sync:
        path: ${cm.core.path}
git:
  default_branch: develop
  remotes:
    cloud: 'git@github.com:behattest/behattest.git'

This will also generate the necessary feature files for you to run a simple test with the command lando blt tests:behat:run.

This article is basic and introductory, I do want to get more into setting up tests on Acquia and taking screenshots and handling failure on production, etc. I will be covering that in upcoming articles I hope. Live long and prosper!