News Archive
PhpRiot Newsletter
Your Email Address:

More information

Supercharge your mobile development with BDD in the Cloud

Note: This article was originally published at Planet PHP on 5 September 2012.
Planet PHP

BDD stands for Behavior Driven Development and the idea is to test your application from end to end, like you would in a functional test but with tests that are written in human readable text. The problem of the usual functional testing is Javascript. You can easily test your API or webpage with PHPUnit but these tests will never execute any line of Javascript.A

To solve this problem, you'll have to use quite a technology stack. At first it seems a bit overwhelming but after you set the whole thing up you don't have to worry about them any more. Don't worry we'll go through each of them, step by step. Here's the set of tools we are going to use:

  • Gherkin Human readable testing language
  • Behat Turns Gherkin language into PHP Tests
  • Goutte Headless Browser that can access Webpages without JavascriptA
  • Selenium Browser Emulator that uses Drivers to control real Browsers
  • Mink Abstracts different browser emulators like Selenium or Goutte into one API
  • Testingbot Online Selenium Server that supports Desktop, Mobile and Tablet Browsers


As a first step you will write your tests in a language called GHERKIN, what sounds complicated is actually pretty easy. You just write what you want to do step by step. Kinda what you do when you write a user story:

@javascriptScenario: Searching for a page with autocompletionGiven I am on "/wiki/Main_Page"When I fill in "search" with "Behavior Driv"And I wait for the suggestion box to appearThen I should see "Behavior-driven development"


Now how is that ever going to work? It's not as magic as it looks. Behat comes with a starting vocabulary. So it will know what to do for most of these commands except for the "I wait for the suggestion box to appear' part.

Once you execute the tests it will skip this and all succeeding parts but will prompt you with something very helpful. It builds the function glue layer. As it turns out all those Gherkin sentences are actually just PHP functions where the quoted text is a parameter.

You copy paste the function wrapper into your FeatureContext.php and fill in the missing code. Imagine it like Lego: you have a basic set of methods and you combine them to become more powerful methods.A

The great thing about this is, you build your vocabulary. It almost forces you to write reusable code and so as the project goes on. Your vocabulary will grow. After a certain point your Project Owner will be able to write the Acceptance Criteria in Gherkin and you won't have to do a thing to have a test.

So the Code for our missing Lego brick looks like that:

/*** @Then /^I wait for the suggestion box to appear$/*/public function iWaitForTheSuggestionBoxToAppear(){$this-getSession()-wait(5000,"$('.suggestions-results').children().length 0"); }

Ok now we know how we get from GHERKIN to actual php but where do these tests run?


Now that we have the function wrapper inside our FeatureContext.php file, we can use the Mink API to tell Goutte or Selenium what it has to do. The advantage of Goutte method is of course speed. But this method won't give you any Javascript capabilities. That's where Selenium comes in handy.


Selenium uses drivers that directly connect with the browser. Other than testing libraries like buster.js (Unit Testing Framework), it's directly communicating with the browser and not using Javascript.

You set up your Selenium

Truncated by Planet PHP, read more at the original (another 1971 bytes)