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.

Maximizing Value Via Continuous Discovery (PDF)

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>"

View Our Work

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->getSession()->getPage()->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. Take a look at a few more test scripts in our Behat re-usable assets folder.

Would a QA team help you Learn more

Have any Behat success stories? 

  • Jordi Girós Guerrero

    In order to generate random informations for filling fields I created that https://github.com/JordiGiros/MinkFieldRandomizer
    You can try and propose modifications!

    • Shweta Sharma

      Thanks Jordi. That looks like a good library to generate quite-some variety of random data. I will give it a try and also let the QA group in Axelerant know about it.