<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=278116885016877&amp;ev=PageView&amp;noscript=1">

, , , , ,

Oct 6, 2015 | 1 Minute Read

4 Examples Of Behat Success

Table of Contents


Behat is a testing tool for behavior driven development—once its pre-requisites are completed, the fun starts. Never before could you take on so many BDD scenarios and functionalities with such ease.

And the best part of Behat? It has built in Mink and Drupal Extensions. These contain ready to use, step by step definitions; the kind that helps you automate link and text verifications in no time.

Want proof? Take a look at these common challenges that are made easy with Behat.

The initial difficulty most quality assurance folks face is writing their step definitions. At first, I was very nervous when I had to write code for Behat, but when I figured out how to do it, I realized how easy it was.

1. Form filling

I had a form that needed to be filled out and needed to write a function that would randomly generate a unique string and fill a field. Here's how I went about it:

$length = 10;

$characters = '0123456789defghijklmnopqrstuvwxyz';

$charactersLength = strlen($characters);

for ($i = 0; $i < $length; $i++) {

   $randomString .= $characters[rand(0, $charactersLength - 1)];



2. Image upload

The second challenge was uploading an image when Behat was scheduled to run via Continuous Integration. The solution is incredibly straightforward. Create a Resource Folder under the features folder and add your images or other attachments. In the behat.yml file - add the files_path configuration. When using it in the feature file, it looks like this:

And I attach the file "image.png" to "<field_name>"

3. Test failure

Now comes the third big challenge: to detect the reason for a step failure in Behat’s execution. Often while writing and running scripts, we see that the flow and logic are correct. The real challenge is to identify the point of failure.

Here we had two options to run our scripts with Selenium, which was great as long it was done on the local machine. The issue came when running scripts on a headless browser on a CI server. Did you know that goutte, our headless browser, takes great snapshots too?


4. Populating AJAX fields

Another challenge was laying in front of what was filling AJAX fields. PhantomJS came to the rescue. This headless browser runs along with Selenium on the server. Here’s how to get it going:

STEP 1: Ensure Java is installed

STEP 2: Download Selenium Standalone Server

STEP 3: Install PhantomJS

STEP 4: Get Selenium and Phantom running

You’re now ready to run your feature files using Selenium with a headless browser PhantomJS. 

Have any Behat success stories? 

About the Author
Avni Gupta, Axelerant Alumni
About the Author

Avni Gupta, Axelerant Alumni

Back to Top