Web Development

Installing Behat/Mink/Goutte on Slackware Linux

Using the latest and the greatest Slackware 14.0, below are the simple steps to install the Behat/Mink/Goutte stack for BDD stuff. Currently, my PHP version is 5.4.13 and my PHPUnit version is 3.7.19. This should be enough to get us started with the installation.

Install via Composer

First, I decided to put everything on /opt/bdd so I created first that directory, owned by my ordinary user (non-root). Next, create a composer.json file at /opt/bdd/composer.json:

{
    "require": {
        "behat/behat": "2.4.*@stable",
        "behat/mink": "1.4.*@stable",
        "behat/mink-extension": "*",
        "behat/mink-goutte-driver": "*",
        "behat/mink-selenium2-driver": "*",
        "behat/mink-zombie-driver": "*",
        "symfony/http-kernel": "2.2.*@stable"
    },
    "minimum-stability": "dev",
    "config": {
        "bin-dir": "bin/"
    }
}

Then, download the installer script and run install.

cd /opt/bdd
curl http://getcomposer.org/installer | php
php composer.phar install

Next is to make behat easily accessible on the system. Do below as root.

ln -s /opt/bdd/vendor/behat/behat/bin/behat /usr/bin/behat

Checking if behat is installed correctly

As long as we’ve installed everything correctly, we should be able to run behat by now. To check if installation is working, run behat or behat --version.

Setting up a test case

Now, we are ready to run our first test. Create a dummy project and create some basic test case. Below is my final directory structure for a sample behat test case.

lysender@darkstar-lenovo-32:~/projects/test-behat$ tree
.
|-- behat.yml
`-- features
    |-- bootstrap
    |   `-- FeatureContext.php
    `-- header.feature

2 directories, 3 files

To create the above structure, we do the following:

cd ~
mkdir -p projects/test-behat
cd projects/test-behat
behat --init
touch behat.yml
touch features/header.feature

When we run behat --init, we initializes the directory so we could work with behat feature files on it. it creates features directory and a default features/bootstrap/FeatureContext.php file. Lets’ have our configuration ready. Below is the content of our configuration behat.yml.

default:
    extensions:
        Behat\MinkExtension\Extension:
            base_url: http://www.lysender.com/
            goutte: ~

It tells behat about our site’s base url, in which I used my sites home page. Then I tell behat to use goutte headless browser emulator to be used when interacting with the site. Although, goutte is the default.

Next, we need to setup features/bootstrap/FeatureContext.php to initialize the libraries and behat context we are running. We should point to our behat/mink stack autoloader so it will autoload all classes that we ever need.

<?php

require_once '/opt/bdd/vendor/autoload.php';

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

use Behat\MinkExtension\Context\MinkContext;

//
// Require 3rd-party libraries here:
//
//   require_once 'PHPUnit/Autoload.php';
//   require_once 'PHPUnit/Framework/Assert/Functions.php';
//

/**
 * Features context.
 */
class FeatureContext extends MinkContext
{
    /**
     * Initializes context.
     * Every scenario gets it's own context object.
     *
     * @param array $parameters context parameters (set them up through behat.yml)
     */
    public function __construct(array $parameters)
    {
        // Initialize your context here
    }
}

The file features/FeatureContext.php was automatically created when we run behat --init with few default initializations. We’ve added our own so we can jump straight to testing using goutte driver and mink context. Now that we have our feature context class set up, we are now ready to write some feature test. Below is the content of our first feature features/header.feature.

Feature: Site basic header

Scenario: Site header should be present
  Given I am on "/"
  Then I should see "Lysender" in the ".head-block h1 a" element
  And I should see "Web developer by day, product manager by night" in the ".head-block p" element

The feature simply states that we should have a header text and a sub header text on specified elements (it can use a CSS selector or a name/id string). Below is what we got when we run the test case.

lysender@darkstar-lenovo-32:~/projects/test-behat$ behat
Feature: Site basic header

  Scenario: Site header should be present
    Given I am on "/"
    Then I should see "Lysender" in the ".head-block h1 a" element
    And I should see "Web developer by day, product manager by night" in the ".head-block p" element

1 scenario (1 passed)
3 steps (3 passed)
0m0.694s
lysender@darkstar-lenovo-32:~/projects/test-behat$

What’s next?

Learn more about BDD (Behavioral Driven Development), behat and mink on behat site and its online documentation.

Enjoy and share.

1 thought on “Installing Behat/Mink/Goutte on Slackware Linux”

Leave a reply

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