Automated Testing for WordPress, Part 1

We’ve been using WordPress for some time as a general website development platform, but until now haven’t done too much in the way of automated testing. When we decided to re-implement some code we had written to generate reports on one website, we wanted to be certain the new implementation would return the same data as the old implementation. Since the website had quite a few reports with lots of parameters for each one, trying to test manually wasn’t feasible. The tests had to be automated. Also, the report implementation featured UI components using JavaScript and AJAX, so we needed a testing framework that would allow us to verify not only the report data, but also the high-level UI functionality, i.e. to simulate what an actual user would do.

For this task, we discovered Behat, a PHP “behavior-driven development” (BDD) testing framework. Behavior-driven development is similar to test-driven development, except that the tests are of high-level user interactions as opposed to individual units of code. (In our case, we had already developed the application, so we weren’t truly doing test or behavior “driven” development.)

What we found most useful about Behat for our purposes was its ability to automate browser interactions. Behat achieves this using Mink, a PHP browser emulation library. Using Mink, we were able to automate collection of report data using the same GUI interactions as a human user, and then replay those interactions in Behat to verify both the report output and the UI functionality.

Browser Drivers

As we said, Behat emulates browser behavior using Mink through its “Mink Extension”. Mink accomplishes this using various “browser drivers”. Browser drivers can be “headless”, meaning they don’t have a GUI, or can launch an actual browser using a “browser controller”, or can even use on-line browser emulation tools like BrowserStack. The crucial thing is that Mink provides a uniform API for all of them, so the same code can be used with multiple drivers.

There are a number of Mink browser drivers available:

  • Goutte: If you don’t need JavaScript, you can use one of Mink’s headless browser drivers like the “Goutte” driver, which is implemented entirely in PHP so requires no external binaries and saves you the overhead of having to launch a GUI browser.
  • Selenium: If you need JavaScript support, you can use the “Selenium2” driver, which uses Selenium, a Java application, to launch an actual GUI browser, which in turn requires a native binary driver to control each browser you want to run.
  • Headless JavaScript Drivers: While Mink’s “headless” browser drivers generally don’t support JavaScript, there are a couple that do:

Since our reports required JavaScript, we had to choose from among the JavaScript-enabled drivers. I liked the idea of using a headless driver, since I figured it would be faster to run, and could be run in an environment where a GUI browser isn’t available (i.e. on a server). I tried using the Zombie driver, but wasn’t able to get it working on Windows. The Phantom JS driver worked ok, but it didn’t seem to be much faster than Selenium, so in the end I opted for Selenium.

! Gotcha: The latest Windows version of the Selenium Gecko driver for Firefox (0.14.0, as of this writing) seems to be broken. The Google Chrome driver works fine.

Next: Setting up the environment.

Leave a Reply

Your email address will not be published. Required fields are marked *