Behaviour Driven Development
using Behat
Isn’t it obvious…???
○

What test should I write next?


○

How do I know when I’ve written the last test?


○

Am I writing the right code?
“

If you can't explain it simply, you
don't understand it well enough


!

Albert Einstein

”
Why BDD?
○

Write Human-readable stories that describe the behaviour of your application


○

Focuses the communication between the business and the developers


○

Code is self documenting


○

Code is easier to maintain
BEHAT
INSTALL
○

Composer


○

PHAR


○

GIT

INIT
○

mkdir Calculator

○

cd Calculator

○

behat --init

./features

/bootstrap

FeatureContext.php
Gherkins
The Basics
Feature - Gherkins
# features/BananaCalculator.features

# Describe the business value of this feature

Feature: Banana Calculator

As Bob the Banana merchant,

I want a calculator that can add the amount of bananas

so that I can know how many bananas I currently have
Scenario - Gherkins
# features/BananaCalculator.features

# Describe the business rules with scenarios

Scenario: Will add 2 banana amounts
Steps - Gherkins
# features/BananaCalculator.features

# Describe the steps

Given I have 3 Bananas

When I add 5 Bananas

Then I should have 8 Banana
Example
Feature: Banana Calculator

As Bob the Banana merchant,

I want a calculator that can add the amount of bananas

so that I can know how many bananas I currently have

Scenario: Will add 2 banana amounts

Given I have 3 Bananas

When I add 5 Bananas

Then I should have 8 Banana
Scenario Outline
Passing Parameters and Tagging
@BeforeSuite
Managing Sub-Context
○

Context files can get really big really fast


○

Certain steps are repeated across multiple Feature files
Mink
test in browser
Mink
In order to test, that our web application behaves correctly, we need a way to simulate this
interaction between browser and web application in our tests

Mink is an open source acceptance test framework for web applications
Goutte
○

Headless Browser


○

No JS support


○

Good for testing REST API
Selenium
○

JS Support

○

Can run multiple browser

○

Need to start Selenium RC

○

Not Headless

○

Need to load Drivers for different browsers

○

Doesn’t play nice at times

java -jar selenium-serverstandalone.jar

java -jar selenium-serverstandalone.jar Dwebdriver.chrome.driver=

"/usr/local/bin/chromedriver"
Sahi
○

JS Support (kinda)


○

Can run Multiple browsers


○

Need to setup localhost proxy
in browser


○

Not headless


○

Issues with Ajax Request
PhantomJS
○

JS Support


○

Headless browser


○

Pure Awesomeness


!
phantomjs --webdriver=8643
DEMO
BDD Anti-Patterns
just don’t
When i fill in 'input.flooble-widgets[:first]' with 'FooBar'
○

Write BDD in the domain language


○

You should never see any DOM elements
Anti-pattern
When I subscribe to the newsletter
rather than
And I set the value of 'input#newsletter-signup-checkbox' to '1'
and
When I search for articles containing the words 'rockets'
rather than
When I fill in 'input#search-terms' with 'rockets'
And I click 'button#search-button'
And I wait 10 seconds
○

This is not a requirement


○

Roll up the execution into the page element
Try this
public function search($keywords)

{

$searchForm = $this->find('css', 'form#search');



if (!$searchForm) {

throw new ElementNotFoundException($this->getSession(), 'form', 'css', 'form#search');

}



$searchForm->fillField('q', $keywords);

$searchForm->pressButton('Google Search');



return $this->getPage('Search results');

}
IN CONCLUSION
BDD & TDD = BFF
Thank you

BDD using behat