integration testing with behat drupal
TRANSCRIPT
Integration Testing with Behat
Oscar MeridaMarch 2016
Why Automated Testing
● Old school “Click around to see if anything broke”● Should want a process that is:
– Repeatable– Can be automated– Not Prone to Human Error
Forms of Testing
● Unit Testing / TDD● Integration Tests● UI Testing● Performance or Load Testing
Behat / Mink
● Behat is a Behavior Driven Developement framework to help communicate how an application should behave.
● Mink is an extension to allow website testing by simulating interacting with it through a browser– Goutte: makes request, parses content, no JS– Selenium: controls a real browser, more setup.
Installation
● In your project root with Composer:composer require behat/behatcomposer require behat/minkcomposer require behat/mink-selection2-drivercomposer require behat/mink-selenium2-drivercomposer require behat/mink-extension
● Or in your composer.json file: "require": { "behat/behat": "^3.0", "behat/mink": "^1.7", "behat/mink-selenium2-driver": "^1.3", "behat/mink-extension": "^2.1" },
Verify Installation
● Behat and it's dependencies will download to your 'vendor/' folder
● Can check that it works with:vendo/bin/behat -V
Drupal Integration
● The Behat Drupal Exentions integrates Behat, Mink, and Drupal.
● Provides out-of-the-box tests that are Drupal aware.
● Install with:composer require drupal/drupal-extension
Configuration
● Be default looks for a “behat.yml” file.● First part – configures contexts
default:
suites:
default:
contexts:
- Drupal\DrupalExtension\Context\DrupalContext
- Drupal\DrupalExtension\Context\MarkupContext
- Drupal\DrupalExtension\Context\MessageContext
- FeatureContext
Configuring extensions
● Additional configuration for Selenium integration
extensions:
Behat\MinkExtension:
goutte: ~
javascript_session: selenium2
selenium2:
wd_host: http://local.dev:4444/wd/hub
base_url: http://local.dev
Configuring DrupalExtension
● Tell it about your site's regions
Drupal\DrupalExtension:
blackbox: ~
region_map:
breadcrumb: '#breadcrumb'
branding: '#region-branding'
branding_second: '#region-branding-second'
content: '#region-content'
content_zone: '#zone-content'
Writing a Feature
● A “Feature” is a set of tests in a plain text file● Use “vendor/bin/behat --init” to create a features
directory.● You can have more than one feature file:
– “blog.feature”– “members.feature”– “homepage.feature”
Global Elements feature
● Checks that certain blocks are present
Feature: Global Elements
Scenario: Homepage Contact Us Link
Given I am on the homepage
Then I should see the link "Contact Us" in the "branding_second" region
Then I should see the "Search" button in the "branding_second" region
Then I should see the "div#block-system-main-menu" element in the "menu" region