Introduction

This post stands on the shoulders of this helpful post by Gnaritas which got me almost all of the way there.

I found the opposite to Gnaritas in terms of which browser driver worked. I found the Chrome browser driver kept crashing and the Firefox (Gecko) driver worked fine.

Behat is 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. It has the ability to drive a browser. You have the ability to write tests that look like this:

Scenario: Recipes page
  Given I go to "https://mywebsite/recipes"
  Then I should see "Bread"
  And I should see "Cake"
  And page should have body class "page-template-t-recipes"

There’s a shortage of instructions for getting Behat working on Windows, hence this post, hoping to fill in some gaps for someone else. That said, if you are behind a proxy, I don’t have all the answers yet. My first tip is to give Goutte a go. I was able to get Goutte working with the proxy.

If this post helps you or you have advice to offer, please post in the comments. #gratitude, and all that.


Preparation

Install MAMP if you haven’t already

Download MAMP for Windows from the MAMP website and install it. You need PHP.

If you install to C:\MAMP, add C:\MAMP\bin\php\php7.0.9 to your PATH in your environment variables.

MAMP is a local Apache/MySQL server, great for local development and testing.

Install Composer if you haven’t already

Download and install Composer, the dependency manager for PHP applications. This makes it really easy to get Behat and all the components of it. Composer uses a JSON formatted file to download multiple packages.

Install Behat

Modify the path to suit your own needs.

  1. Create a directory somewhere that’s going to be easy to get to on the command line. I used D:\Behat
  2. In D:\Behat, create a text file called composer.json and copy and paste the following content into it.
    {
    	"config": {
    		"bin-dir":"bin/"
    	}
    }
  3. Open a command prompt and run this command:
    composer require behat/behat behat/mink-extension behat/mink-selenium2-driver behat/mink-goutte-driver

    This tells composer to load Behat and its dependencies, Mink Extension which has a lot of the basic test commands to get you started, Selenium2 driver which lets Behat control a browser for testing, and Goutte driver which lets Behat use a PHP program to test.
    At first I thought I didn’t need Goutte but after struggling with the browser based tests for hours (undoubtedly proxy issues), it was refreshing for Goutte to just work even with the proxy.

  4. Add D:\Behat\bin to your PATH in your environment variables.
  5. On your command prompt, at D:\Behat, run this command which initialises your Behat install (documentation here):
    behat --init
  6. Run this command to test your behat install: behat -dl – it shows you what commands are available to use for tests – save it/print it.

Install Selenium

Download the Selenium Standalone Server jar file. Put it in D:\Behat. You’ll also need the Java runtime to run Selenium. I already had a Java runtime environment.
Download browser drivers for Chrome and Firefox and put them in your D:\Behat directory with Selenium.
Create a batch file to start Selenium. I called mine run-selenium.bat. Save it in your D:\Behat directory. Here are the contents of my run-selenium.bat. “rem” stands for remark (commented out). So you can see I have commands for Chrome and Firefox (gecko). In the file below, Chrome is commented out, so it will only run Firefox:

rem java -Dwebdriver.chrome.driver=D:\Behat\chromedriver.exe -jar selenium-server-standalone-3.4.0.jar
java -Dwebdriver.gecko.driver=D:\Behat\geckodriver.exe -jar selenium-server-standalone-3.4.0.jar

Run your batch file from the command line to start Selenium.

Configure behat.yml

This is the step that wasted a huge part of my day – finding a valid behat.yml to work with Behat 3, Selenium and Chrome. It’s also why I highly recommend you install Goutte if you haven’t already. It gives you a more foolproof way to test your setup apart from having Selenium working.

default:
    extensions:
        Behat\MinkExtension:
            default_session: goutte
            goutte: ~
            selenium2: 
              wd_host: "https://127.0.0.1:4444/wd/hub"
              capabilities: { "browserName": "firefox", "browser": "firefox", "version":  "", "platform": "WINDOWS" }
            browser_name: firefox
    suites:
        default:
            contexts:
                - FeatureContext

Continue to part 2