Install Behat on Windows in 30 minutes

Automated Testing – Behat on Windows – In 30 minutes.

As an alternative you could try our all-in-one installer that will complete the below steps for you.

For those of you the have not encountered it, Behat is a framework for automated testing and behaviour driven development (BDD). As part of this installation, we will also add the Mink extension to Behat that will provide a library of the most common predefined test steps and automation via a browser (e.g. Google Chrome).

Why Behat? For starters, it’s free and has a tiny footprint.  It can also run on any operating system. Behat uses cucumber like syntax (readable English) to structure its tests making them definable and readable for a non-developer.  There are also several add-ons for specific web applications (e.g. Drupal, WordPress, Moodle, etc.). Most importantly it speeds up the development and modification of test packs.

When I first started to use Behat, I could not find a definitive guide to installing Behat within a Windows environment.  Although installing it is not difficult, I thought I would produce a  quick guide to getting it implemented in 30 minutes.

What we will be installing:

  • PHP
  • Composer
  • An ANSI Command Terminal
  • Behat
  • Mink extensions for Behat
  • Mink Headless Browser for testing no JavaScript functionality
  • Mink Selenium adaptor for running tests in a browser.
  • Chrome Selenium Drive and configuration.

Once completed you will have the basis of an automated test environment.

Step 1 – Installing PHP

If you already have a PHP installed on the machine (e.g. as part of WAMP, etc) you should just ensure that PHP is included in the environment path.  See Adding PHP to the PATH in Windows.

If you need to install PHP on the computer there are two main options:

Step 2 – Installing Composer

Installing composer is a relatively straightforward process.  Just run the composer installer (composer-setup.exe) from this site (https://getcomposer.org/doc/00-intro.md#installation-windows).

Step 3 – Installing Git

If you already have a Git installed on the machine you should just ensure that PHP is included in the environment path.  See Adding Git to the PATH in Windows.

Installing Git is a relatively straightforward process.  Just run the Git installer from this site (https://git-scm.com/download/win).

Step 4 – Installing NodeJS

Installing NodeJSis a relatively straightforward process.  Just run the NodeJSinstaller from this site (https://nodejs.org/en/).

Step 5 – Installing an ANSI Terminal.

Although not necessary because you can use the windows command prompt, installing a better command line tool with support for ANSI colours will make the running of tests and the output much more readable.  The screenshots below show the difference:

I choose to use ConEmu (https://conemu.github.io/) because it is free and feature rich.  Simply download and run the installer.

Step 6 – Installing Behat and its plugins

It is advised that you select a folder where all your test scripts will be placed.  This will be your project folder. This folder will contain individual Behat folders that will correspond to the systems and/or environments you will be testing.

Installing the plugins is very easy, we simply need to create a new project folder and then create a composer.json file with the following content:

 {

     "require": {

           "behat/behat" :"2.4.*@stable",

           "behat/mink":"1.5.*@stable",

           "behat/mink-goutte-driver":"*",

           "behat/mink-extension":"*",

           "behat/mink-selenium2-driver":"*"

     }

}

Note:  The Behat and Mink versions will change over time.

I’ll provide a link to a sample project at the bottom of this article.

From the command prompt (using the terminal software in step 3 or a command console), navigate to the project folder and type the command:

 > Composer install

Composer will now install all the components required.  You may see a warning but you should not see any errors.

Step 7 – Configuring Behat and Mink

To configure Behat and Mink, we edit (or create) a behat.yml file in the root of the project folder with the following content:

default:

  paths:

    features: features

    bootstrap: %behat.paths.features%/bootstrap

  extensions:

    Behat\MinkExtension\Extension:

      browser_name: 'chrome'

      goutte: ~

      selenium2:

        wd_host: "http://127.0.0.1:14400"

Note:  The above configures the default file locations and configures Google Chrome (via the selenium Chrome Driver) as the browser that should be used where JavaScript is required for testing (more about this in another article).

One important note is that we have set up the Chrome Selenium Driver to be run via port 14400.  If this clashes on your system with some other service, it can be changed.

I’ll provide a link to a sample project at the bottom of this article.

We now need to initialise Behat by executing the following command from the project folder:

> vendor\bin\behat --init

This will create the project file structure for Behat.

To make executing tests easier, you could create a batch file (behat.bat) in the root of your project folder that will execute the Behat test application.  Although not necessary, it will save some typing and is good practice for executing multiple test profiles/scenarios (I’ll discuss this in a later article).

For the moment, the content of the batch file should be a single command:

> vendor\bin\behat

Step 8 – Running our first test (Headless)

After we have defined our first test, we should now be able to run it.  To define our first test, edit the file \features\test.feature in a text editor and replace the content with the following:

Feature: Drupal.org search

     In order to find modules on Drupal.org

     As a Drupal user

     I need to be able to use Drupal.org search




Scenario: Searching for "behat"

     Given I go to "https://www.drupal.org/"

     And I press "edit-submit"

     When I fill in "edit-search-block-form--2" with "behat"

     And I press "edit-submit"

     Then I should see "Behat Drupal Extension"

We can now run the command buy running our behat.bat batch file in our project folder.  If everything is configured correctly, the results should look something like the following:

Note:  At the point of publishing, the test passes without issue but this may change (and the test fail) if the Drupal home/search functions change.

Step 9 – Running the test in Chrome

Firstly, you will need to install Google Chrome on the computer.

You will then need to download the Chrome Selenium Driver (x86 or 64bit) and place it in a folder for your selenium drivers (e.g. c:\programs\selenium).

The chrome driver needs to be told which port to check for requests form Behat (14400).  The easiest way to configure this is to create a batch file (chrome.bat) in the same folder as your driver.

The contents of the batch file should be a single command line:

 chromedriver --port=14400

You can now start selenium (Chrome) by double clicking on chrome.bat in explorer or create a shortcut to it on your desktop.

Before you can run a Behat test in Chrome, the chrome driver must be started (use chrome.bat) and running in the background.

We now just need to tell Behat that the test scenario requires JavaScript. To do this we add the tag “@JavaScript” above the “Scenario:” line in the feature file (\features\test.feature):

Feature: Drupal.org search

     In order to find modules on Drupal.org

     As a Drupal user

     I need to be able to use Drupal.org search




@JavaScript

Scenario: Searching for "behat"

     Given I go to "https://www.drupal.org/"

     And I press "edit-submit"

     When I fill in "edit-search-block-form--2" with "behat"

     And I press "edit-submit"

     Then I should see "Behat Drupal Extension"

When we execute behat.bat again, it will open a copy of Chrome, execute the test and should report the same test results as in step 4.

If that is successful, you have set up your first project for Behat and executed a test in a headless browser and Google Chrome.

I’ll provide a link to a template project file structure and configuration in another article.

I hope you have found this article useful.  I’ll publish other articles expanding on this in the future.

 

 

Comments (1)

  1. Reply

    Greate pieces. Keep posting such kind of info on your site.
    Im really impressed by your blog.
    Hi there, You’ve performed a great job.
    I will definitely digg it and individually suggest to my friends.

Leave a comment

Your e-mail address will not be published. Required fields are marked *