SlideShare a Scribd company logo
1
BDD with Behat &
Drupal
Alessio Piazza
Software Developer @SparkFabrik
(twinbit + agavee)
twitter: @alessiopiazza
I do stuff with Drupal
SPARKFABRIK
3
• Behaviour Driven Development
• a software development process that emerged
from TDD (Test Driven Development)
• Behaviour driven development specifies that
tests of any unit of software should be specified
in terms of the desired behaviour of the unit
BDD
SPARKFABRIK
WHY BDD? or TDD?
• You must test your application
• Save your self from regressions (fix one thing,
break another one)
• Save your self from misunderstanding between
you and your client
• Your tests become specification
4
SPARKFABRIK
BDD Workflow
1. Write a specification feature
2. Implement its test
3. Run test and watch it FAIL
4. Change your app to make test PASS
5. Run test and watch it PASS
6. Repat from 2-5 until all GREEN
5
SPARKFABRIK
BDD: HOW IT WORKS?
6
HOW IT WORKS?
7
Write a specification feature
8
Feature: Download Drupal 8
In order to use Drupal 8
as a user
i should be able to download it from drupal.org
Scenario: Download Drupal 8 from Drupal.org
Given i am on the homepage
When i Click “Download and Extend”
Then I should see the link “Download Drupal 8.0.0”
Scenario: …
SPARKFABRIK
SCENARIO STRUCTURE
9
Scenario: Download Drupal 8 from Drupal.org
Given I am on drupal.org homepage
When I Click “Download and Extend”
Then I should see the link “Download Drupal 8.0.0”
SPARKFABRIK
SCENARIO STRUCTURE
10
Given i am on drupal.org homepage
And i Click “Download and Extend”
Then I should see the link “Download Drupal 8.0.0”
Scenario: Download Drupal 8 from Drupal.org
Keyword Description
SPARKFABRIK
SCENARIO STRUCTURE
11
Given i am on drupal.org homepage
And i Click “Download and Extend”
Then I should see the link “Download Drupal 8.0.0”
Scenario: Download Drupal 8 from Drupal.org
Keyword Description
Steps
Given I am on drupal.org homepage
When I Click “Download and Extend”
Then I should see the link “Download …”
SPARKFABRIK
STEPS: Keywords
12
Given -> To put the system in a known state
Given I am an anonymous user
SPARKFABRIK
Given
STEPS: Keywords
13
When -> To describe the key action to performs
When I click on the link
SPARKFABRIK
Given
STEPS: Keywords
14
When
Then -> To to observe outcomes
Then I should see the text “Hello”
SPARKFABRIK
Given
STEPS: Keywords
15
When
Then
And / But -> To make our scenario more readable
SPARKFABRIK
STEPS: Keywords
16
Scenario: Download Drupal 8 from Drupal.org
Given I am on drupal.org homepage
Given I am an anonymous user
When I Click “Download and Extend”
When I Click “Download Drupal 8.0.0”
Then I should see the link “Drupal Core 8.0.0”
SPARKFABRIK
STEPS: Keywords
17
Scenario: Download Drupal 8 from Drupal.org
Given I am on drupal.org homepage
And I am an anonymous user
When I Click “Download and Extend”
And I Click “Download Drupal 8.0.0”
Then I should see the link “Drupal Core 8.0.0”
SPARKFABRIK
GHERKIN
• It is a Business Readable, Domain Specific
Language
• Lets you describe software’s behaviour without
detailing how that behaviour is implemented.
• Gherkin is the language that BEHAT
understands.
18
SPARKFABRIK
BEHAT
docs.behat.org/en/v3.0/
Open source Behavior Driven Development
framework for PHP (5.3+)
Inspired from Cucumber (Ruby)
www.cucumber.io
From version 3 BEHAT it’s an official
Cucumber implementation in PHP
Let us write tests based on our scenarios
19
SPARKFABRIK
HOW BEHAT READS
GHERKIN?
20
Given I am an anonymous user
SPARKFABRIK
HOW BEHAT READS
GHERKIN?
21
/**
* @Given I am an anonymous user
*/
public function assertAnonymousUser() {
// Verify the user is logged out.
if ($this->loggedIn()) {
$this->logout();
}
}
Given I am an anonymous user
SPARKFABRIK
HOW BEHAT READS
GHERKIN?
22
Then I should not see the text :text
SPARKFABRIK
HOW BEHAT READS
GHERKIN?
23
/**
* @Then I should not see the text :text
*/
public function assertNotTextVisible($text) {
// Use the Mink Extension step definition.
$this->assertPageNotContainsText($text);
}
Then I should not see the text :text
SPARKFABRIK
STEP RESULTS
24
/**
* @Then I should not see the text :text
*/
public function assertNotTextVisible($text) {
// This step will fail.
throw new Exception(‘Test failed’);
}
• A step FAILS when an Exception is thrown
SPARKFABRIK
STEP RESULTS
25
/**
* @Then I will trigger a success
*/
public function triggerSuccess() {
// This step will pass.
print(‘Hello DrupalDay’);
}
• A step PASS when no Exception are raised
SPARKFABRIK
OTHER STEP RESULTS
26
• SUCCESSFUL
• FAILED
• PENDING
• UNDEFINED
• SKIPPED
• AMBIGUOUS
• REDUNDANT
SPARKFABRIK
WHERE ARE THESE
STEPS?
27
• STEPS are defined inside plain PHP Object
classes, called CONTEXTS
• MinkContext and DrupalContext give use a lot of
pre-defined steps ready to be used
• Custom steps can be defined inside
FeatureContext class
SPARKFABRIK
Sum Up
28
Give use the syntax
to write features and scenarios
Our framework that reads our
scenarios and run tests
Classes containing our step
definitions
GHERKIN
BEHAT
CONTEXTS
SPARKFABRIK
How we integrate Behat and
Drupal?
29
Behat Drupal Extension
An integration layer between
Drupal and Behat
https://www.drupal.org/project/drupalextension
https://github.com/jhedstrom/drupalextension
SPARKFABRIK
From your project root..
composer require drupal/drupal-extension
30
- Installing drupal/drupal-driver (v1.1.4)
Downloading: 100%
> DrupalCoreComposerComposer::vendorTestCodeCleanup
- Installing symfony/filesystem (v3.0.0)
Downloading: 100%
> DrupalCoreComposerComposer::vendorTestCodeCleanup
- Installing symfony/config (v2.8.0)
Downloading: 100%
> DrupalCoreComposerComposer::vendorTestCodeCleanup
- Installing behat/transliterator (v1.1.0)
Loading from cache
…
…
> DrupalCoreComposerComposer::vendorTestCodeCleanup
- Installing drupal/drupal-extension (v3.1.3)
Downloading: 100% SPARKFABRIK
Create file behat.yml
31
1 default:
2 suites:
3 default:
4 contexts:
5 - FeatureContext
6 - DrupalDrupalExtensionContextDrupalContext
7 - DrupalDrupalExtensionContextMinkContext
8 extensions:
9 BehatMinkExtension:
10 goutte: ~
11 base_url: http://d8.drupalday2015.sparkfabrik.loc # Replace with your site's URL
12 DrupalDrupalExtension:
13 blackbox: ~
see https://github.com/jhedstrom/drupalextension
SPARKFABRIK
Then --init your test suite
32
vendor/bin/behat --init
+d features - place your *.feature files here
+d features/bootstrap - place your context classes here
+f features/bootstrap/FeatureContext.php - place your
definitions, transformations and hooks here
SPARKFABRIK
Demo!
33
SPARKFABRIK
BROWSER INTERACTION
• With BDD we write clear, understandable
scenario
• Our scenario often describe an interaction with
the browser
34
WE NEED A BROWSER EMULATOR
SPARKFABRIK
BROWSER EMULATORS
• Headless browser emulators (Goutte)
• Browser controllers (Selenium2, SAHI)
• They do basically the same thing: control or
emulate browsers but they do them in different
ways
• They comes with their pro and cons
35
SPARKFABRIK
MINK!
http://mink.behat.org/en/latest/
• MINK removes API differences between different browser emulators
• MINK provides different drivers for every browser emulator
• MINK gives you an easy way to
1. control the browser
2. traverse pages
3. manipulate page elements
4. interact with page elements
36
SPARKFABRIK
37
/**
* @When /^(?:|I )move forward one page$/
*/
public function forward() {
$this->getSession()->forward();
}
MINK!
http://mink.behat.org/en/latest/
When I move forward one page
SPARKFABRIK
Wrap up
38
GHERKIN
BEHAT
CONTEXTS
MINK
Give use the syntax
to write features and scenarios
Our framework that reads our
scenarios and run the tests
Classes containing our step
definitions
Describe browser interaction
without worrying about the browser
SPARKFABRIK
39

More Related Content

PDF
PPTX
Python/Flask Presentation
PPTX
KEY
Eclipse IAM, Maven Integration For Eclipse
PDF
Instrumentación de entrega continua con Gitlab
KEY
Enterprise Build And Test In The Cloud
KEY
Enterprise Build And Test In The Cloud
PPT
Q4E and Eclipse IAM, Maven integration for Eclipse
Python/Flask Presentation
Eclipse IAM, Maven Integration For Eclipse
Instrumentación de entrega continua con Gitlab
Enterprise Build And Test In The Cloud
Enterprise Build And Test In The Cloud
Q4E and Eclipse IAM, Maven integration for Eclipse

What's hot (20)

PDF
Magento 2 Capistrano Deploy
PDF
Spring IO '15 - Developing microservices, Spring Boot or Grails?
PDF
From devOps to front end Ops, test first
PDF
Deploying a Pylons app to Google App Engine
PDF
When Web meet Native App
PPT
Auto Build
PPTX
10 Laravel packages everyone should know
PDF
Intro to Laravel 4
PDF
Writing a Jenkins / Hudson plugin
PDF
10 less-known Laravel Packages: May 2016
ODP
5 Reasons Why Maven Sux
PPTX
Laravel Beginners Tutorial 1
PPTX
Jenkins Plugin Development With Gradle And Groovy
PPTX
Grails Spring Boot
PDF
Laravel Code Generators and Packages
PDF
Cocoa pods
PPT
Demystifying Maven
ODP
Presentation laravel 5 4
PPTX
Gulp and bower Implementation
PPTX
Session on Selenium 4 : What’s coming our way? by Hitesh Prajapati
Magento 2 Capistrano Deploy
Spring IO '15 - Developing microservices, Spring Boot or Grails?
From devOps to front end Ops, test first
Deploying a Pylons app to Google App Engine
When Web meet Native App
Auto Build
10 Laravel packages everyone should know
Intro to Laravel 4
Writing a Jenkins / Hudson plugin
10 less-known Laravel Packages: May 2016
5 Reasons Why Maven Sux
Laravel Beginners Tutorial 1
Jenkins Plugin Development With Gradle And Groovy
Grails Spring Boot
Laravel Code Generators and Packages
Cocoa pods
Demystifying Maven
Presentation laravel 5 4
Gulp and bower Implementation
Session on Selenium 4 : What’s coming our way? by Hitesh Prajapati
Ad

Viewers also liked (20)

PDF
DDAY2014 - Rete civica in Drupal: un bene comune digitale da costruire assiem...
PDF
Mantenere una distribuzione Drupal attraverso test coverage: Paddle case study
PDF
Docker for developers
PDF
Invisiblefarm condivide l'esperienza DrupalGIS
PDF
Sviluppo Code-driven e riusabilità del codice: CMI e Features per D8
PDF
La semantica per automatizzare una redazione web: l'esperienza di Innolabplus.eu
PDF
Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...
PDF
La piattaforma web di CNA: Istanze Drupal replicabili integrate con Alfresco ...
PDF
DrupalGap: crea una app Android (ed iOS) con Drupal, Drupalgap ed Apache Cordova
PDF
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
PDF
Come progettare e realizzare una distribuzione in Drupal 8
PDF
Drupal per la PA
PDF
Da X a Drupal 8, migra tutto e vivi sereno
PDF
Tooling per il tema in Drupal 8
PDF
Once you go cloud you never go down
PDF
Drupal per la PA dell'Emilia-Romagna
PDF
Your Entity, Your Code
PDF
[drupalday2017] - DevOps: strumenti di automazione per Drupal8
PDF
[drupalday2017] - Devel - D8 release party
PDF
[drupalday2017] - Async navigation with a lightweight ES6 framework
DDAY2014 - Rete civica in Drupal: un bene comune digitale da costruire assiem...
Mantenere una distribuzione Drupal attraverso test coverage: Paddle case study
Docker for developers
Invisiblefarm condivide l'esperienza DrupalGIS
Sviluppo Code-driven e riusabilità del codice: CMI e Features per D8
La semantica per automatizzare una redazione web: l'esperienza di Innolabplus.eu
Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...
La piattaforma web di CNA: Istanze Drupal replicabili integrate con Alfresco ...
DrupalGap: crea una app Android (ed iOS) con Drupal, Drupalgap ed Apache Cordova
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
Come progettare e realizzare una distribuzione in Drupal 8
Drupal per la PA
Da X a Drupal 8, migra tutto e vivi sereno
Tooling per il tema in Drupal 8
Once you go cloud you never go down
Drupal per la PA dell'Emilia-Romagna
Your Entity, Your Code
[drupalday2017] - DevOps: strumenti di automazione per Drupal8
[drupalday2017] - Devel - D8 release party
[drupalday2017] - Async navigation with a lightweight ES6 framework
Ad

Similar to Behaviour Driven Development con Behat & Drupal (20)

ZIP
Voiture tech talk
PDF
Virtual Environment and Web development using Django
PDF
Deploying Symfony | symfony.cat
PDF
‘Hello, world!’ application how to dockerize golang application
PPTX
Ultimate Survival - React-Native edition
PDF
Write your first WordPress plugin
PPTX
Bugzilla Installation Process
PPTX
A Presentation of Dash Enterprise and Its Interface.pptx
PDF
20130528 solution linux_frousseau_nopain_webdev
PDF
Spring Live Sample Chapter
PDF
Hands on Docker - Launch your own LEMP or LAMP stack
ZIP
Mojolicious
PDF
Learning Docker with Thomas
PDF
Drupal Day 2012 - Automating Drupal Development: Make!les, Features and Beyond
PDF
Docker for Ruby Developers
PDF
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
PPTX
Openshift Presentation ppt compare with VM
PDF
Обход проверки безопасности в магазинах мобильных приложений при помощи платф...
PDF
Development Setup of B-Translator
PDF
DCEU 18: Dockerfile Best Practices
Voiture tech talk
Virtual Environment and Web development using Django
Deploying Symfony | symfony.cat
‘Hello, world!’ application how to dockerize golang application
Ultimate Survival - React-Native edition
Write your first WordPress plugin
Bugzilla Installation Process
A Presentation of Dash Enterprise and Its Interface.pptx
20130528 solution linux_frousseau_nopain_webdev
Spring Live Sample Chapter
Hands on Docker - Launch your own LEMP or LAMP stack
Mojolicious
Learning Docker with Thomas
Drupal Day 2012 - Automating Drupal Development: Make!les, Features and Beyond
Docker for Ruby Developers
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
Openshift Presentation ppt compare with VM
Обход проверки безопасности в магазинах мобильных приложений при помощи платф...
Development Setup of B-Translator
DCEU 18: Dockerfile Best Practices

More from DrupalDay (17)

PDF
[drupalday 2017] - Accessibilità Web: Finalità, metodologie e strumenti.
PDF
[drupalday2017] - Cloud e integrazione per la PA: la sfida dell'Open Source t...
PDF
[drupalday2017] - Drupal come frontend che consuma servizi: HTTP Client Manager
PDF
[drupalday2017] - Drupal & Patternlab: un nuovo approccio al theming
PDF
[drupalday2017] - Decoupled frontend con Drupal 8 e OpenUI 5
PDF
[drupalday2017] - Open Data con Drupal nella PA: considerazioni su licensing ...
PDF
[drupalday2017] - Behat per Drupal: test automatici e molto di più
PDF
[drupalday2017] - Drupal 4 Stakeholders
PDF
[drupalday2017] - DRUPAL per la PA: il modello della Trasparenza di Sapienza
PDF
[drupalday2017] - Venezia & Drupal. Venezia è Drupal!
PDF
[drupalday2017] - Quando l’informazione è un servizio
PDF
[drupalday2017] - Cosa significa convertire un modulo da D7 a D8
PDF
[drupalday2017 - KEYNOTE] - Saving the world one Open Source project at a time
PDF
[drupalday2017] - Speed-up your Drupal instance!
PDF
[drupalday2017] - DevOps: strumenti di automazione per Drupal8
PDF
[drupalday2017] - REST in pieces
PDF
[drupalday2017] - Contenuti educativi digitali aperti, creare contenuti e dis...
[drupalday 2017] - Accessibilità Web: Finalità, metodologie e strumenti.
[drupalday2017] - Cloud e integrazione per la PA: la sfida dell'Open Source t...
[drupalday2017] - Drupal come frontend che consuma servizi: HTTP Client Manager
[drupalday2017] - Drupal & Patternlab: un nuovo approccio al theming
[drupalday2017] - Decoupled frontend con Drupal 8 e OpenUI 5
[drupalday2017] - Open Data con Drupal nella PA: considerazioni su licensing ...
[drupalday2017] - Behat per Drupal: test automatici e molto di più
[drupalday2017] - Drupal 4 Stakeholders
[drupalday2017] - DRUPAL per la PA: il modello della Trasparenza di Sapienza
[drupalday2017] - Venezia & Drupal. Venezia è Drupal!
[drupalday2017] - Quando l’informazione è un servizio
[drupalday2017] - Cosa significa convertire un modulo da D7 a D8
[drupalday2017 - KEYNOTE] - Saving the world one Open Source project at a time
[drupalday2017] - Speed-up your Drupal instance!
[drupalday2017] - DevOps: strumenti di automazione per Drupal8
[drupalday2017] - REST in pieces
[drupalday2017] - Contenuti educativi digitali aperti, creare contenuti e dis...

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Spectroscopy.pptx food analysis technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation theory and applications.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
Teaching material agriculture food technology
PDF
Approach and Philosophy of On baking technology
PDF
KodekX | Application Modernization Development
DOCX
The AUB Centre for AI in Media Proposal.docx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectroscopy.pptx food analysis technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Programs and apps: productivity, graphics, security and other tools
Big Data Technologies - Introduction.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Cloud computing and distributed systems.
Encapsulation theory and applications.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Teaching material agriculture food technology
Approach and Philosophy of On baking technology
KodekX | Application Modernization Development
The AUB Centre for AI in Media Proposal.docx

Behaviour Driven Development con Behat & Drupal

  • 1. 1
  • 2. BDD with Behat & Drupal Alessio Piazza Software Developer @SparkFabrik (twinbit + agavee) twitter: @alessiopiazza I do stuff with Drupal SPARKFABRIK
  • 3. 3 • Behaviour Driven Development • a software development process that emerged from TDD (Test Driven Development) • Behaviour driven development specifies that tests of any unit of software should be specified in terms of the desired behaviour of the unit BDD SPARKFABRIK
  • 4. WHY BDD? or TDD? • You must test your application • Save your self from regressions (fix one thing, break another one) • Save your self from misunderstanding between you and your client • Your tests become specification 4 SPARKFABRIK
  • 5. BDD Workflow 1. Write a specification feature 2. Implement its test 3. Run test and watch it FAIL 4. Change your app to make test PASS 5. Run test and watch it PASS 6. Repat from 2-5 until all GREEN 5 SPARKFABRIK
  • 6. BDD: HOW IT WORKS? 6
  • 8. Write a specification feature 8 Feature: Download Drupal 8 In order to use Drupal 8 as a user i should be able to download it from drupal.org Scenario: Download Drupal 8 from Drupal.org Given i am on the homepage When i Click “Download and Extend” Then I should see the link “Download Drupal 8.0.0” Scenario: … SPARKFABRIK
  • 9. SCENARIO STRUCTURE 9 Scenario: Download Drupal 8 from Drupal.org Given I am on drupal.org homepage When I Click “Download and Extend” Then I should see the link “Download Drupal 8.0.0” SPARKFABRIK
  • 10. SCENARIO STRUCTURE 10 Given i am on drupal.org homepage And i Click “Download and Extend” Then I should see the link “Download Drupal 8.0.0” Scenario: Download Drupal 8 from Drupal.org Keyword Description SPARKFABRIK
  • 11. SCENARIO STRUCTURE 11 Given i am on drupal.org homepage And i Click “Download and Extend” Then I should see the link “Download Drupal 8.0.0” Scenario: Download Drupal 8 from Drupal.org Keyword Description Steps Given I am on drupal.org homepage When I Click “Download and Extend” Then I should see the link “Download …” SPARKFABRIK
  • 12. STEPS: Keywords 12 Given -> To put the system in a known state Given I am an anonymous user SPARKFABRIK
  • 13. Given STEPS: Keywords 13 When -> To describe the key action to performs When I click on the link SPARKFABRIK
  • 14. Given STEPS: Keywords 14 When Then -> To to observe outcomes Then I should see the text “Hello” SPARKFABRIK
  • 15. Given STEPS: Keywords 15 When Then And / But -> To make our scenario more readable SPARKFABRIK
  • 16. STEPS: Keywords 16 Scenario: Download Drupal 8 from Drupal.org Given I am on drupal.org homepage Given I am an anonymous user When I Click “Download and Extend” When I Click “Download Drupal 8.0.0” Then I should see the link “Drupal Core 8.0.0” SPARKFABRIK
  • 17. STEPS: Keywords 17 Scenario: Download Drupal 8 from Drupal.org Given I am on drupal.org homepage And I am an anonymous user When I Click “Download and Extend” And I Click “Download Drupal 8.0.0” Then I should see the link “Drupal Core 8.0.0” SPARKFABRIK
  • 18. GHERKIN • It is a Business Readable, Domain Specific Language • Lets you describe software’s behaviour without detailing how that behaviour is implemented. • Gherkin is the language that BEHAT understands. 18 SPARKFABRIK
  • 19. BEHAT docs.behat.org/en/v3.0/ Open source Behavior Driven Development framework for PHP (5.3+) Inspired from Cucumber (Ruby) www.cucumber.io From version 3 BEHAT it’s an official Cucumber implementation in PHP Let us write tests based on our scenarios 19 SPARKFABRIK
  • 20. HOW BEHAT READS GHERKIN? 20 Given I am an anonymous user SPARKFABRIK
  • 21. HOW BEHAT READS GHERKIN? 21 /** * @Given I am an anonymous user */ public function assertAnonymousUser() { // Verify the user is logged out. if ($this->loggedIn()) { $this->logout(); } } Given I am an anonymous user SPARKFABRIK
  • 22. HOW BEHAT READS GHERKIN? 22 Then I should not see the text :text SPARKFABRIK
  • 23. HOW BEHAT READS GHERKIN? 23 /** * @Then I should not see the text :text */ public function assertNotTextVisible($text) { // Use the Mink Extension step definition. $this->assertPageNotContainsText($text); } Then I should not see the text :text SPARKFABRIK
  • 24. STEP RESULTS 24 /** * @Then I should not see the text :text */ public function assertNotTextVisible($text) { // This step will fail. throw new Exception(‘Test failed’); } • A step FAILS when an Exception is thrown SPARKFABRIK
  • 25. STEP RESULTS 25 /** * @Then I will trigger a success */ public function triggerSuccess() { // This step will pass. print(‘Hello DrupalDay’); } • A step PASS when no Exception are raised SPARKFABRIK
  • 26. OTHER STEP RESULTS 26 • SUCCESSFUL • FAILED • PENDING • UNDEFINED • SKIPPED • AMBIGUOUS • REDUNDANT SPARKFABRIK
  • 27. WHERE ARE THESE STEPS? 27 • STEPS are defined inside plain PHP Object classes, called CONTEXTS • MinkContext and DrupalContext give use a lot of pre-defined steps ready to be used • Custom steps can be defined inside FeatureContext class SPARKFABRIK
  • 28. Sum Up 28 Give use the syntax to write features and scenarios Our framework that reads our scenarios and run tests Classes containing our step definitions GHERKIN BEHAT CONTEXTS SPARKFABRIK
  • 29. How we integrate Behat and Drupal? 29 Behat Drupal Extension An integration layer between Drupal and Behat https://www.drupal.org/project/drupalextension https://github.com/jhedstrom/drupalextension SPARKFABRIK
  • 30. From your project root.. composer require drupal/drupal-extension 30 - Installing drupal/drupal-driver (v1.1.4) Downloading: 100% > DrupalCoreComposerComposer::vendorTestCodeCleanup - Installing symfony/filesystem (v3.0.0) Downloading: 100% > DrupalCoreComposerComposer::vendorTestCodeCleanup - Installing symfony/config (v2.8.0) Downloading: 100% > DrupalCoreComposerComposer::vendorTestCodeCleanup - Installing behat/transliterator (v1.1.0) Loading from cache … … > DrupalCoreComposerComposer::vendorTestCodeCleanup - Installing drupal/drupal-extension (v3.1.3) Downloading: 100% SPARKFABRIK
  • 31. Create file behat.yml 31 1 default: 2 suites: 3 default: 4 contexts: 5 - FeatureContext 6 - DrupalDrupalExtensionContextDrupalContext 7 - DrupalDrupalExtensionContextMinkContext 8 extensions: 9 BehatMinkExtension: 10 goutte: ~ 11 base_url: http://d8.drupalday2015.sparkfabrik.loc # Replace with your site's URL 12 DrupalDrupalExtension: 13 blackbox: ~ see https://github.com/jhedstrom/drupalextension SPARKFABRIK
  • 32. Then --init your test suite 32 vendor/bin/behat --init +d features - place your *.feature files here +d features/bootstrap - place your context classes here +f features/bootstrap/FeatureContext.php - place your definitions, transformations and hooks here SPARKFABRIK
  • 34. BROWSER INTERACTION • With BDD we write clear, understandable scenario • Our scenario often describe an interaction with the browser 34 WE NEED A BROWSER EMULATOR SPARKFABRIK
  • 35. BROWSER EMULATORS • Headless browser emulators (Goutte) • Browser controllers (Selenium2, SAHI) • They do basically the same thing: control or emulate browsers but they do them in different ways • They comes with their pro and cons 35 SPARKFABRIK
  • 36. MINK! http://mink.behat.org/en/latest/ • MINK removes API differences between different browser emulators • MINK provides different drivers for every browser emulator • MINK gives you an easy way to 1. control the browser 2. traverse pages 3. manipulate page elements 4. interact with page elements 36 SPARKFABRIK
  • 37. 37 /** * @When /^(?:|I )move forward one page$/ */ public function forward() { $this->getSession()->forward(); } MINK! http://mink.behat.org/en/latest/ When I move forward one page SPARKFABRIK
  • 38. Wrap up 38 GHERKIN BEHAT CONTEXTS MINK Give use the syntax to write features and scenarios Our framework that reads our scenarios and run the tests Classes containing our step definitions Describe browser interaction without worrying about the browser SPARKFABRIK
  • 39. 39