Automated Testing for WordPress, Part 2: Setting up the Environment

In part 1, I set the context for doing automated “behavior-based” testing of a WordPress application we developed and the available tools we discovered. In this post, I’ll talk about installing those tools (Behat, Selenium) and setting up the environment to start testing.

Installing Behat and its Dependencies

I found it best to install Behat using Composer, PHP's main package management tool. Although there is a stand-alone "phar" ("PHP archive") version, Behat has a lot of dependencies, so using Composer was definitely the way to go. Also, using Composer makes it really easy to add other libraries or drivers you might want to use, or to remove ones you've decided not to use.

Once you've a installed Composer (and assuming you've put it on your system path), run the following in the root directory of your project to install Behat, the Mink extension, and the Selenium2 browser driver:

composer require behat/behat behat/mink-extension behat/mink-selenium2-driver

Tip: If you have an existing project using Composer to which you want to add Behat tests, you'll probably want to run the above with the --dev flag, so that Behat and friends will be listed as development dependencies of your project.

Composer will download the specified packages and all of their dependencies into the "vendor" folder under your project root (creating it if it doesn't exist). It will also create a composer.json file for you (or update your existing one) listing the packages you required with minimum version constraints, and create/update a composer.lock file with the exact versions of everything it downloaded. (It may take a while to install. You'll end up installing about 30 packages with thousands of source files under your "vendor" directory once this is all done.)

Composer will also let you know if there are any required extensions that are missing from your PHP environment (e.g. mbstring, curl) or if you need a different version of PHP altogether.

Tip: You can start with the following minimal composer.json in your project root directory to put the executable directory at your project root instead of under "vendor":

{
  "config": {
    "bin-dir":"bin/"
  }
}

That way, you can run Behat with bin/behat instead of vendor/bin/behat.

Setting up Selenium

To set up Selenium:

You can put the browser driver files in the same directory as Selenium to make them discoverable. (There's also some environment variable you can define for the paths, but it's easiest to put them in the same directory.)

Configuring Behat

Behat's configuration directives are specified using YAML in the behat.yml file in the root directory. A minimal behat.yml file looks something like this:

default:
  extensions:
      Behat\MinkExtension:
          base_url: http://test.example.com
          sessions:	            
            selenium:
              selenium2: ~	          
          browser_name: 'chrome'      
  suites:
    default:
      contexts:
        - FeatureContext:
            parameters:
              parameter1name: parameter1value
              parameter2name: parameter2value

Here we've set up a single default profile using the Mink extension, for which we've defined the base_url of the site to test and a single named session ("selenium") using the Selenium2 driver. Note that I've specified "chrome" for the browser_name parameter since I couldn't find a working driver for Firefox, its default browser.

Our profile also has a single default test suite with a single context class called FeatureContext for which we've defined some parameters to be passed to its constructor as key/value pairs.

You can read more about configuring Behat here, although we'll learn a lot more about how Behat works in the next part of the series.

! Gotcha: The syntax for the behat.yml file has changed somewhat from Behat 2 to Behat 3, the current version, so examples you find on the web for Behat 2 might not work in Behat 3. The version 2 documentation is more detailed than that for the latest version, so you can get an idea of what the various fields mean, even if the syntax is different.

Next: Writing and Running the Tests.