Testing Drupal with Behat, Mink, & Selenium

Links

Recommended Reading:

Advanced Reading:

Resources:

  • Behatch (Repo full of helpful step definitions, including breakpoints and screen shots.)

Setting Up

Installation:

  1. Follow the steps to install the Drupal Extension (steps 1 - 2): https://drupal.org/project/drupalextension
    1. This will install Behat, Mink, Mink Extension, and the Drupal integration all for you.
    2. You should install it somewhere you can use it for all your projects, such as your home directory.
  2. Add an alias to the bin/behat executable within your Behat install directory (e.g. "alias behat='~/behat/bin/behat'"), or add it to your PATH.
  3. Create a testing directory in your project and run "behat --init" there.
  4. Edit the features/bootstrap/FeatureContext.php file as described in step 10 on the Drupal Extension project page.
  5. Create a behat.yml file with the following, changing values where necessary:

    default:
      paths:
        features: 'features'
      extensions:
        Behat\MinkExtension\Extension:
          goutte: ~
          selenium2: ~
          base_url: http://yoursite.kala # The web-accessible URL you're testing against.
        Drupal\DrupalExtension\Extension:
          blackbox: ~
          drush:
            root: /var/www/yoursite # The absolute path to your project's webroot.
    

     

  6. Create your feature tests in .feature files in the features directory.
  7. Run the tests by running "behat" in the testing directory.

Testing with Selenium and a Browser (Site Running on Virtual Machine):

  1. For any test scenarios you want to run in the browser, prefix them with @javascript
  2. In your behat.yml file, change selenium2: ~ to the following, changing the browser name and version to the one you want to use.

    selenium2:
      capabilities: {"browser": "firefox", "version": "21"}
    
  3. Download the latest Selenium Server to both your host computer and your virtual machine: http://docs.seleniumhq.org/download
    1. On Kalabox, you may need to download Java before being able to run Selenium, "sudo apt-get install default-jre" should do the trick.
  4. On the virtual machine, start Selenium: java -jar selenium-server-standalone-2.33.0.jar -role hub
    1. Make sure to update the version number if yours is different.
  5. On your host computer, start Selenium: java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://1.3.3.7:4444/grid/register -browser browserName=firefox,version=21,maxInstances=1

    1. Make sure to update the version number if yours is different.
    2. Change the IP address, browserName, and version if they are different from what you want. The command above registers the host with a hub running on Kalabox for Firefox version 21 (I haven't been able to get it to work with Chrome yet).
  6. Execute your tests with Behat.

For more info, see: http://michaelheap.com/behat-selenium2-webdriver-with-minkextension/

 

Adding PHPUnit for Assertions:

  1. Add the following line to the "require" section of your Behat install's composer.json file: "phpunit/phpunit": "*"
  2. Update your Behat install to retrieve PHPUnit: php composer.phar update
  3. Add the following line near the top of your project's features/bootstrap/FeatureContext.php (or uncomment it if it's already there): require_once 'PHPUnit/Framework/Assert/Functions.php';

See the PHPUnit documentation for the various assertions it offers: http://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.assertions

Testing Drupal

It can sometimes be helpful to view the definitions of steps provided by the Drupal Extension. You can read their definitions in the following file within your Behat installation:

vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/DrupalContext.php

Additionally, definitions for steps provided by Mink can be found in the file:

vendor/behat/mink-extension/src/Behat/MinkExtension/Context/MinkContext.php

If you have a scenario that needs to access Drupal internals (e.g. to delete a test user account), be sure to prefix the scenario with @api

Useful Drupal Steps:

  • Given I am logged in as a user with the "role" role (creates user with a given role and logs in as it)