Behat: Introduction & Installation

I got the opportunity to learn and work on Behat for one of the internal projects at and I found it easy and interesting. During my learning phase, I noticed that there is not much documentation available on Behat over the internet. And hence this blog.

Hope it fills in some of the gaps in Behat’s supporting literature.

What is Behat?

Behat is a Behaviour Driven Development (BDD) tool which is used to test the behaviour of the application from end user’s point of view. It’s a popular open source tool for automation testing of business cases, using human readable scenarios to map onto the PHP framework.

Behat was inspired by Ruby’s Cucumber project. Both Behat and Cucumber are based on BDD. That’s the only similarity. Cucumber supports many languages (Ruby, Python, Java etc.) but Behat is just supported by PHP.

We use Gherkin, a language quite like English, to write various scenarios/features for testing an application. Behat can also be extended by writing custom PHP functions. These functions are written in FeatureContext.php file which is created inside the bootstrap folder.

Who Should Use Behat?

Behat was developed for Business Analysts (BA) and developers, so as to bridge the gap between business stakeholders and the development team.

It is also used by testers for testing websites. As Behat uses Gherkin language to write it’s scenarios, it becomes easy for testers to map it with the acceptance criteria of the project. This results in fulfilment of the client requirements to the best of our ability..

When to use Behat?

Behat helps completely achieve the client requirements because it works according to the Acceptance Criteria of the project. It uses English like, human understandable language to write Behat steps. That’s the best part, because it can be understood by anyone, whether it’s a project manager, a developer or any other business stakeholder.

It is best suited when:

Testing the data/content on the webpage

Testing actions (like links, buttons etc.)

Testing forms

In migration state of any website from one CMS to other

For end to end testing (i.e. flow of an application from start to finish)

For Functional, Regression, Smoke, and Sanity Testing

When not to use Behat?

Behat is a great tool with easy installation, but it lags behind in terms of documentation. Also it is not a great option if you are testing:

Dynamic data

Images

Http response of links throughout the website

Steps for Behat Ins tallation

Before we get down to the steps, we are assuming you have done the following:

Made a folder and named it ‘behat’ (or any name you wish) at any location in your system.

Opened terminal and typed cd Desktop/behat (here we are assuming that ‘behat’ folder is created on Desktop. If you create it at some other location, go to that directory through terminal using ‘cd’ command).

Typed the following commands to install and run behat (these commands are same for Windows, Mac or any other Linux based system):

1. touch composer.json

The ‘touch’ command is used to create empty files in Mac and Linux systems. For Windows, you can create composer.json manually inside the project folder (in our case it is named behat). Once the file ‘composer.json’ is created, add the code below:

The above code is in json format and hence indentation needs to be taken care of. It consists of various dependencies that Behat requires.

2. touch behat.yml

Now it's time to move onto the next step in Behat. Create a behat.yml file using command “touch behat.yml” inside the behat folder. Once the file behat.yml is created, add the following code:

3. composer install

Now it’s time to update the composer in our system. We are almost done by now. Just a step away from completing the installation process.

Execute “composer install” from your terminal. This command will read the ‘composer.json’ file from the current directory and execute it. It will install/update all the dependencies and its versions specified in composer.json file.

At this point, Behat is installed in your system. Now the steps below will help you initialize and run behat.

4. bin/behat --init

The command “bin/behat --init” initializes Behat. This is to be run only once. It will create a directory structure as shown below:

This command creates a directory (folder) called ‘features’ inside which we create our .feature files. The .feature file contains a feature, it’s overview, and scenarios. Scenario is a structure of Gherkin that consists of various steps as per the acceptance criteria.
Inside ‘features’ directory, there is a another directory present called ‘bootstrap’. Inside this directory you will find a php file called ‘FeatureContext.php’. This is the heart of your behat, as it consists of the code that is mapped with the steps written within the scenarios in .feature file(s).

5. bin/behat features/name_of_featurefile.feature

This command is used to run the feature files (here name_of_featurefile represents the name which you will give to your feature file) and features is the folder created inside the project folder.

If only a particular scenario is to be run, tags can be used. Following is an example:

> bin/behat features/name_of_featurefile.feature --tags 1

If more than one scenarios are to be run, the tags associated can be separated using comma (,). Following is an example:

> bin/behat features/name_of_featurefile.feature --tags 1, 2

Behat Components

Browser Emulators

Browser Emulators are used for testing the responsiveness of a website. These can be of two types:

Browser Controllers

Headless Browsers

When we run the command bin/behat features/name_of_featurefile.feature, it is run on headless browser. To run our features on a real time browser (like Firefox, Chrome etc.) we need browser controllers (like Selenium etc.). In some cases we might require specific drivers like chromedriver for Chrome browser.

If using selenium, following is the command:

> java -jar selenium-server-standalone-2.53.1.jar

This command will run the selenium jar file which can be downloaded from Selenium’s official website. To run a jar file, java should be installed in the system.

It can be run on a particular browser by downloading it’s driver which is easily available online. To run selenium on chrome browser, following is the command:

Terms in Behat

Gherkin is an English like language which is business readable and domain specific, that is specially designed to describe the behaviour. It is also available in other languages (like French).

Gherkin is used to write the scenario(s) in the feature file(s). It has a defined structure and is indentation sensitive. Following are the keywords used in Gherkin:

Given, When, Then, And, But

Example:

Feature

A feature gives the overview of the functionality that would be covered in the specific feature file. We write features in a text file with .feature extension. One .feature file contains a single feature, but can have multiple scenarios within that feature.

Example:

Scenario

A scenario consists of a short summary (which starts with keyword Scenario) and step definitions. The step definitions are written in Gherkin format.

Example:

behat.yml

It is a file which is written in YAML format, which consists of all configurations required for Behat. This file is loaded by default.

Example: An example of behat.yml file is given in Behat Installation Steps section above.

Composer

Composer is a tool which is used to declare various dependencies of our project and manage their installations (and/or updations). It can be called a dependency management tool, but definitely not a package manager.

We use commands like composer install, composer update etc. to install/update project libraries/dependencies through composer.

Behat Commands

Following are the generally used commands in behat:

bin/behat -h When you run this command, you get all available commands and options in behat.

bin/behat -dl- When you run this command, you get list of available step definitions that can be used in scenarios. When any custom step is written, it gets added to this list.

bin/behat --dry-run --append-snippets - When this command is run, the snippet (custom PHP function body) gets added in FeatureContext.php file inside which custom PHP function is to be written.

That’s all folks! Hope this helps you get started with Behat. You can check out one of our QA webinars on how to leverage Behat to tell stories through your code, and build the right product.