Automation in Drupal 
by Bozhidar Boshnakov
About me 
• Bozhidar Boshnakov 
• Team leader @ ProPeople 
• bboshnakov91@gmail.com 
• Skype: bo6nakov 
• Drupal.org – bboshnakov 
• linkedin.com/in/bboshnakov
Table of contents 
• TDD – Test-Driven Development 
• BDD – Behavior-Driven Development 
• Gherkin 
• Behat 
• Mink 
• Drupal extension 
• Acceptance criteria 
• Demonstrations and show cases
INTELLIGENCE 
IS 
A LIABILITY!
SMART PEOPLE 
Can make progress … 
… without process 
This is not a good thing !!!
Professionals 
• Design, plan and prepare first 
• …then do the work 
• This produces better results FASTER !!!
Process is the difference 
between 
Software 
Engineering &Programming
In order to verify application behavior 
As a software developer 
I need tests 
Preferably automated tests
Test-Driven Development 
…is an iterative design process 
• Write a test 
• Ensure the new test fails 
• Write code to satisfy the test 
• Ensure all tests pass 
• Refactor 
• Repeat
Behavior-Driven Development 
…builds upon TDD 
• Write test cases in a natural language 
– Understood by developers and business folks alike 
– Helps relate domain language of requirements to 
the code 
• Do this with user stories and scenarios 
– User stories describe a feature’s benefit in context 
– Scenarios are executable acceptance criteria
Gherkin 
Gherkin is a Business Readable, Domain 
Specific Language created especially for 
behavior descriptions. It gives you the 
ability to remove logic details from 
behavior tests.
Gherkin Syntax
Gherkin Syntax 
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
This is where 
and come in
Behat is an open source behavior-driven 
development framework for 
PHP 5.3 and 5.4.
Before you begin, ensure that you have at least PHP 5.3.1 installed. 
The simplest way to 
install Behat is through 
Composer.
Composer.json 
{ 
"require": { 
"behat/mink": "*", 
"behat/mink-goutte-driver": "*", 
"behat/behat": "*", 
"behat/mink-extension": "*", 
"drupal/drupal-extension": "*" 
}, 
"minimum-stability": "dev", 
"config": { 
"bin-dir": "bin/" 
} 
}
Composer.phar 
Then download composer.phar and run install 
command: 
$ curl http://getcomposer.org/installer | php 
$ php composer.phar install
Behat.yml 
default: 
extensions: 
BehatMinkExtensionExtension: 
base_url: http://wearepropeople.com 
goutte: ~ 
selenium2: ~ 
paths: 
features: features 
bootstrap: features/bootstrap
And the last step… 
php bin/behat --init 
And now you have your Features folder 
where you can create the magic!!!
What are the features?
Let's Have Behat Analyze Our Feature
Behat Creates the Glue 
...but the rest is up to you
Not so fast. What about Mink?
Understanding Mink 
One of the most important parts in the 
web is a browser. A browser is the window 
through which web application users 
interact with the application and other 
users.
Headless 
browser 
emulators VS Browser 
controllers
Selenium2Driver 
• Control the Browser – Session 
• Cookies and Headers management 
• HTTP Authentication 
• Traverse the Page - Selectors 
• Manipulate the Page
Mink Context Defines Steps 
...for making requests
Mink Context Defines Steps 
...for interacting with forms
Mink Context Defines Steps 
...for querying the DOM
Mink Context Defines Steps 
...for examining responses
Now… 
Let’s get away from the 
theory and show some 
action!
Tags 
Tags are a great way to organize your 
features and scenarios: 
A Scenario or Feature can have as many 
tags as you like, just separate them with 
spaces:
Tags 
How to execute the tags? 
If a tag exists on a Feature, Behat 
will assign that tag to all child 
Scenarios and Scenario Outlines 
too!
Behat.yml 
All configuration happens inside a 
single configuration file in the 
YAML format.
Imports 
…or share your feature configurations
Profiles 
$bin/behat –p google features/feature1
The Drupal extension 
• Overrides default behaviors: 
– Snippets follow Drupal coding standards 
– Mink Extension steps get synonyms for readability 
• Provides drivers to facilitate data setup 
– Blackbox 
– Drush 
– Drupal API 
• Adds support for Drupal: 
– Regions 
– Node types 
– Users and Roles 
– Taxonomy 
– Subcontexts for Contributed Modules
Provides drivers for data set up 
• Works with Drupal 6, 7, and 8 
• Connect with no privileges (local or remote) 
• Connect with Drush (local or remote) 
• Connect with the Drupal API (local only)
About data setup 
• Every scenario must be able to run 
independent of other scenarios 
• Often work with a fresh copy of the 
production database 
• Can’t always assume the database has specific 
content 
• Separate architectural elements from content 
elements 
• Insert data to get reliable checks
Drush: alias file 
http://drush.ws/examples/example.aliases.drushrc.php
Drush: behat.yml
Drupal API: behat.yml
Region map: behat.yml
Regions 
Requires a map in the behat.yml file 
• label => css selector 
• This means regions don’t have to map directly to 
Drupal theme regions
Structure > Blocks > (Theme) > Demonstrate block regions
Nodes
Users, roles and taxonomies
Relational data
Relational data
If you have any questions or you want 
access for the full installation guideline feel 
free to contact me on my email address: 
bboshnakov91@gmail.com
Special thanks to: 
• Nikolay Ignatov 
• Boyan Borisov 
• Miroslav Banov 
• Toni Kolev
Resources 
• http://dspeak.com/pnwds/6-drupalextension.html 
• http://docs.behat.org/guides/7.config.html 
• http://behat.org/ 
• http://mink.behat.org/
Thank you!!!
Automation in Drupal

Automation in Drupal

  • 1.
    Automation in Drupal by Bozhidar Boshnakov
  • 2.
    About me •Bozhidar Boshnakov • Team leader @ ProPeople • [email protected] • Skype: bo6nakov • Drupal.org – bboshnakov • linkedin.com/in/bboshnakov
  • 3.
    Table of contents • TDD – Test-Driven Development • BDD – Behavior-Driven Development • Gherkin • Behat • Mink • Drupal extension • Acceptance criteria • Demonstrations and show cases
  • 4.
  • 5.
    SMART PEOPLE Canmake progress … … without process This is not a good thing !!!
  • 6.
    Professionals • Design,plan and prepare first • …then do the work • This produces better results FASTER !!!
  • 7.
    Process is thedifference between Software Engineering &Programming
  • 8.
    In order toverify application behavior As a software developer I need tests Preferably automated tests
  • 9.
    Test-Driven Development …isan iterative design process • Write a test • Ensure the new test fails • Write code to satisfy the test • Ensure all tests pass • Refactor • Repeat
  • 10.
    Behavior-Driven Development …buildsupon TDD • Write test cases in a natural language – Understood by developers and business folks alike – Helps relate domain language of requirements to the code • Do this with user stories and scenarios – User stories describe a feature’s benefit in context – Scenarios are executable acceptance criteria
  • 11.
    Gherkin Gherkin isa Business Readable, Domain Specific Language created especially for behavior descriptions. It gives you the ability to remove logic details from behavior tests.
  • 12.
  • 13.
    Gherkin Syntax 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
  • 15.
    This is where and come in
  • 16.
    Behat is anopen source behavior-driven development framework for PHP 5.3 and 5.4.
  • 17.
    Before you begin,ensure that you have at least PHP 5.3.1 installed. The simplest way to install Behat is through Composer.
  • 18.
    Composer.json { "require":{ "behat/mink": "*", "behat/mink-goutte-driver": "*", "behat/behat": "*", "behat/mink-extension": "*", "drupal/drupal-extension": "*" }, "minimum-stability": "dev", "config": { "bin-dir": "bin/" } }
  • 19.
    Composer.phar Then downloadcomposer.phar and run install command: $ curl http://getcomposer.org/installer | php $ php composer.phar install
  • 20.
    Behat.yml default: extensions: BehatMinkExtensionExtension: base_url: http://wearepropeople.com goutte: ~ selenium2: ~ paths: features: features bootstrap: features/bootstrap
  • 21.
    And the laststep… php bin/behat --init And now you have your Features folder where you can create the magic!!!
  • 22.
    What are thefeatures?
  • 23.
    Let's Have BehatAnalyze Our Feature
  • 24.
    Behat Creates theGlue ...but the rest is up to you
  • 25.
    Not so fast.What about Mink?
  • 26.
    Understanding Mink Oneof the most important parts in the web is a browser. A browser is the window through which web application users interact with the application and other users.
  • 27.
    Headless browser emulatorsVS Browser controllers
  • 28.
    Selenium2Driver • Controlthe Browser – Session • Cookies and Headers management • HTTP Authentication • Traverse the Page - Selectors • Manipulate the Page
  • 29.
    Mink Context DefinesSteps ...for making requests
  • 30.
    Mink Context DefinesSteps ...for interacting with forms
  • 31.
    Mink Context DefinesSteps ...for querying the DOM
  • 32.
    Mink Context DefinesSteps ...for examining responses
  • 33.
    Now… Let’s getaway from the theory and show some action!
  • 34.
    Tags Tags area great way to organize your features and scenarios: A Scenario or Feature can have as many tags as you like, just separate them with spaces:
  • 35.
    Tags How toexecute the tags? If a tag exists on a Feature, Behat will assign that tag to all child Scenarios and Scenario Outlines too!
  • 36.
    Behat.yml All configurationhappens inside a single configuration file in the YAML format.
  • 37.
    Imports …or shareyour feature configurations
  • 38.
    Profiles $bin/behat –pgoogle features/feature1
  • 39.
    The Drupal extension • Overrides default behaviors: – Snippets follow Drupal coding standards – Mink Extension steps get synonyms for readability • Provides drivers to facilitate data setup – Blackbox – Drush – Drupal API • Adds support for Drupal: – Regions – Node types – Users and Roles – Taxonomy – Subcontexts for Contributed Modules
  • 40.
    Provides drivers fordata set up • Works with Drupal 6, 7, and 8 • Connect with no privileges (local or remote) • Connect with Drush (local or remote) • Connect with the Drupal API (local only)
  • 41.
    About data setup • Every scenario must be able to run independent of other scenarios • Often work with a fresh copy of the production database • Can’t always assume the database has specific content • Separate architectural elements from content elements • Insert data to get reliable checks
  • 42.
    Drush: alias file http://drush.ws/examples/example.aliases.drushrc.php
  • 43.
  • 44.
  • 45.
  • 46.
    Regions Requires amap in the behat.yml file • label => css selector • This means regions don’t have to map directly to Drupal theme regions
  • 47.
    Structure > Blocks> (Theme) > Demonstrate block regions
  • 48.
  • 49.
    Users, roles andtaxonomies
  • 50.
  • 51.
  • 53.
    If you haveany questions or you want access for the full installation guideline feel free to contact me on my email address: [email protected]
  • 54.
    Special thanks to: • Nikolay Ignatov • Boyan Borisov • Miroslav Banov • Toni Kolev
  • 55.
    Resources • http://dspeak.com/pnwds/6-drupalextension.html • http://docs.behat.org/guides/7.config.html • http://behat.org/ • http://mink.behat.org/
  • 56.

Editor's Notes

  • #5 Most of the programmers are smart and it is very important when building software. What I mean…
  • #6 This is not good for big projects
  • #7 This is not good for big projects
  • #8 This is not good for big projects
  • #10 TDD Defined: - Decide what the code will do Write a test that will pass if the code does that thing Run the test, see it fail Write the code Run the test see it pass TDD Provides: Design and plan before you code Document your design Proof that code implements design Encourages design of testable code
  • #11 “BDD is about implementing an application by describing its behavior from the perspective of its stakeholders.” – Dan North The original developer of BDD (Dan North) came up with the notion of BDD because he was dissatisfied with the lack of any specification within TDD of what should be tested and how. BDD – Second generation of agile methodology User stories Domain-Driven Development Extreme Programmin TDD Acceptance Driven Test Planning Continuous integration
  • #14 Сега ще покажа прост пример как да опишем тези неща.
  • #17 И сега ще си поговорим как точно да сложим бехат на нашите локални машини.
  • #18 Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
  • #20 This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line, amongst other things.
  • #21 Sometimes, Behat’s default configuration is not enough. Some day you’ll need some extended tools to configure your feature suite. For that day, Behat has a very powerful configuration system based on YAML configuration files and profiles.
  • #23 Every *.feature file conventionally consists of a single feature. Lines starting with the keyword Feature: (or its localized equivalent) followed by three indented lines starts a feature. A feature usually contains a list of scenarios. You can write whatever you want up until the first scenario, which starts withScenario: (or localized equivalent) on a new line. You can use tags to group features and scenarios together, independent of your file and directory structure. Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And (or localized one). Behat treats them all the same, but you shouldn’t. Here is an example:
  • #27 There’s huge amount of browser emulators out there, like Goutte, Selenium, Sahi and others. They all do the same job, but do it very differently. They behave differently and have very different API’s. But, what’s more important - there’s actually 2 completely different types of browser emulators out there: Headless browser emulators Browser controllers
  • #28 First type browsers are simple pure HTTP specification implementations, likeGoutte. Those browser emulators send a real HTTP requests against an application and parse the response content. They are very simple to run and configure, because this type of emulators can be written in any available programming language and can be run through console on servers without GUI. Headless emulators have both, advantages and disadvantages. Advantages are simplicity, speed and ability to run it without the need in real browser. But this type of browsers have one big disadvantage - they have no JS/AJAX support. So, you can’t test your rich GUI web applications with headless browsers. Second browser emulators type are browser controllers. Those emulators aims to control the real browser. That’s right, a program to control another program. Browser controllers simulate user interactions on browser and are able to retrieve actual information from current browser page. Selenium and Sahi are two most famous browser controllers. The main advantage of browser controllers usage is the support for JS/AJAX interactions on page. Disadvantage is that such browser emulators require installed browser, extra configuration are usually much slower than headless counterparts.
  • #29 There’s huge amount of browser emulators out there, like Goutte, Selenium, Sahi and others. They all do the same job, but do it very differently. They behave differently and have very different API’s. But, what’s more important - there’s actually 2 completely different types of browser emulators out there: Headless browser emulators Browser controllers
  • #37 Behat tries to load behat.yml or config/behat.yml by default, or you can tell Behat where your config file is with the --config option: Paths Formatter Colors Context Imports Environment Variable
  • #38 The imports block allows you to share your feature suite configuration between projects and their test suites. All files from the imports block will be loaded by Behat and merged into yourbehat.yml config.
  • #39 Profiles Extensions
  • #40 Profiles Extensions
  • #41 Profiles Extensions
  • #42 Profiles Extensions
  • #43 Examples
  • #53 Profiles Extensions
  • #54 Profiles Extensions
  • #55 Profiles Extensions
  • #56 The RegexpMapper changes filenames according to a pattern defined by a regular expression. This is the most powerful mapper and you should be able to use it for every possible application.
  • #57 The RegexpMapper changes filenames according to a pattern defined by a regular expression. This is the most powerful mapper and you should be able to use it for every possible application.