Chopping The Head Off Of Your Behat Test Monster With Phantom Js

Recently, I posted about how to set up Behat with Sahi on OSX to automate your web application testing including javascript. Today, we will chop the head off this operation by swapping out a spawned browser instance with the open source, headless browser: Phantomjs.

This post was helped along quite a bit by first, this article by Ryan Grenz, but ultimately this one by Shane.

1. Get a basic test project running.

This post relies heavily on the work completed in the last one. My suggestion is that you work through that one quickly, as I'll assume:

  • You have the prerequisites for PHP and Sahi testing.
  • You have installed Sahi and know how to start the server.
  • You have a sample project configured to use Sahi.
  • You can run all the tests in the sample project and they pass.

2. Install PhantomJS.

You can grab the headless browser over at their website or, if you are running Homebrew as you should, just run:

brew install phantomjs

This will install PhantomJS, which you can verify afterwards by typing:

phantomjs -v

Which will display a version number.

3. Create a PhantomJS access file.

Next, we need to create a file that will tie Sahi to PhantomJS. This file will be used when we are configuring Sahi to use PhantomJS as an alternative browser. You can put it wherever you like, but it probably makes sense to place it somewhere close to the PhantomJS installation. I put mine here:


And the contents of the file should be:

if (phantom.args.length === 0) {
    console.log('Usage: phantom-sahi.js <Sahi Playback Start URL>');
} else {
    var address = phantom.args[0];
    console.log('Loading ' + address);
    var page = new WebPage();, function(status) {
        if (status === 'success') {
            var title = page.evaluate(function() {
                return document.title;
            console.log('Page title is ' + title);
        } else {
            console.log('FAIL to load the address');

4. Add the PhantomJS Browser to Sahi.

The browsers for Sahi are defined in a browser_types.xml file, which if you followed the previous Sahi installation, would be located at /usr/local/share/sahi/userdata/config/browser_types.xml edit that file, adding the following to the bottom, inside the main tag:

    <options>--debug=yes --proxy=localhost:9999 /usr/local/Cellar/phantomjs/phantom-sahi.js</options>

This adds a new browser type, phantomjs, using the same icon as Safari, points to your Homebrew installation of PhantomJS and sets basic options, as well as points to the script that we made earlier.

You can verify that you have added this additional browser by running the script at /usr/local/share/sahi/userdata/bin/, which will open the Sahi dashboard allowing you to see the additional browser:

Sahi browser launch dashboard.

5. Modify behat.yml.

Using the sample source code from last time, you need to make one modification to the existing behat.yml file, changing the browser you are using from "Firefox" to "PhantomJS". Your updated file should look like this:

    bootstrap:  '%behat.paths.features%/bootstrap'
      javascript_session: sahi
      browser_name: phantomjs
      goutte: ~
      sahi: ~

6. Make sure it all works!

Make sure you start Sahi first, but running:


From the root of the sample project should execute all the tests, and they should pass, but this time a browser instance will not spawn! Rather, all the testing goes on in the background in the headless PhantomJS browser.

If you enjoyed reading this or learned something, please consider sharing via , , or . Thanks!

If you enjoyed this article, you might like others related to the Testing interest.