Skip to content
Field note

4 Examples Of Behat Success

Introduction

October 6, 2015·2 min read·Avni Gupta, Axelerant Alumni/ Field correspondent
4 Examples Of Behat Success

Introduction

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)];

}

$this->getSession()->getPage()->fillField("field_subdomain_name[und][0][value]",$randomString);

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?

$this-&gt;getSession()-&gt;getPage()-&gt;getContent());

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?

Companion brief

Bring this dispatch into a working session - one page in, scoping memo out.

Brief Foyer
Pressure-test "Field note" with a partner

If this dispatch landed, bring it into a room with us.

The argument in "4 Examples Of Behat Success" is ours - but every situation breaks the argument differently. Send the situation in one page and a named partner will reply inside one business day with a scoping memo: where the argument holds for you, where it bends, and what we'd actually do in the first two weeks.

One-pager · Field note

By submitting this form you acknowledge that you have read Axelerant's Privacy Policy and agree to its terms.

Replied to by a named partner within one business day.