user acceptance testing with behat, mink and phantomjs
TRANSCRIPT
User Acceptance Testing with Behat, Mink, and
PhantomJSChattanooga PHP Developers Group
December 3, 2014
User Acceptance Testing?
• Rooted in agile software development
• Used to verify that a solution “works” for the user
• Ideally created by business customers
• Expressed in a business domain language
Business Domain Language?
• Written in the “business speak” of the client’s business world, or “domain”
• Geared toward “business readable”
• Designed to bridge the communications gap between product owners (clients) and developers (us) by expressing requirements in a common language that both sides understand
What’s the benefit?
• Increases customer / developer mutual understanding of objectives
• Increases odds of the application you develop actually meeting the customer expectations
• Increases customer satisfaction
Crafting Good Acceptance Tests
• Starts with a User Story
• A User Story is the smallest piece of functionality that adds business value
• A Good User Story is a Feature Set of your project
Structure of a User Story
• As a user who…(target user role)
• I need…(business need)
• In order to….(benefit)
Good User Stories are written by the customer
User Story Example
• As a student…
• I want to purchase a parking pass...
• So I can park on campus without getting a ticket or being towed
User Stories are Prioritized
• By the client
• Based on business importance
• With feedback from the development team
• Highest agreed priority items get worked on first
Next Come Acceptance Criteria
• Written before programming begins
• Defines the specific functional aspects of the user story
• Feature set is complete when all Acceptance Criteria are met (i.e, all Acceptance tests for that Feature are passed)
Structure of Acceptance Criteria
• Given that I am (user precondition)
• When I do this (performs action)
• Then I should (see observable results)
This is called a “Scenario”
and each of the above is a “step”
What is Behat?
• A testing framework written in PHP
• Accepts tests written in a business domain language
• Executes those test on your application
• Designed to test the behavior of your application
Behat works with Mink
• Mink is an open source browser controller/emulator for web applications
• Written in PHP
• Integrates with Behat via the Mink Extension
What does the Mink Extension Provide?
• Predefined “steps” that comprise the components of Acceptance Tests
• Each “step” maps to a PHP callback that contains the code for the test
But, wait, you said...
• Tests were written in “business speak”
!
• And they are….using a structured, highly readable language called Gherkin
Gherkin
• Gherkin is a business readable, domain specific language created specifically for crafting behavior descriptions
• These behavior descriptions serve as both your product documentation and your acceptance tests
Behat does the work
• Features (User Stories and Acceptance Tests) are written in Gherkin and are composed of steps
• Steps are parsed by Behat using regular expressions, and mapped to PHP Callbacks
• PHP Callbacks execute test code that powers Mink
• Mink runs browser simulations to test applications
PhantomJS
• Headless WebKit scriptable with a JavaScript API
• Native support for DOM Handling, CSS selectors, JSON, Canvas, SVG and more
• Gets triggered in your Acceptance tests by specifying the “@javascript” tag on your test — replaces cURL calls with actual WebKit function executions
• Behat is Pluggable, through extensions
• Mink Extension is a Behat Plugin
• Extensions exist for various frameworks including:
• Drupal
• Symfony/Symfony2
• Yii
!
!
{! "require": {! "behat/behat": "2.4.*@stable",! "behat/mink": "1.5.*@stable",! "behat/mink-extension": "*",! "behat/mink-goutte-driver": "*",! "behat/mink-selenium2-driver": "*",! "behat/yii-extension": "*",! ! "drupal/drupal-extension": "*"! },! "minimum-stability": "dev",! "config": {! "bin-dir": "bin/"! }!}
composer.json
$> composer install
You’ll get
• bin/ -- contains Behat executable
• vendor/ -- contains dependencies, including Mink Extension, Drupal Extension, Yii extension and their dependencies
Next you need to define your test environment parameters
behat.ymldefault:! extensions:! Behat\MinkExtension\Extension:! goutte: ~! selenium2:! wd_host: "http://localhost:8643/wd/hub"! base_url: "http://www.supplyhog.com"! Drupal\DrupalExtension\Extension:! blackbox: ~! region_map:! content: "#content"!
Now you need to initialize your test environment
$> bin/behat --init
Now you also get...• features/ -- Directory that will house your
features (user stories and scenarios) written in Gherkin
• features/bootstrap/FeatureContext.php -- Context extension that will house any of your application specific test callbacks
• Will extend DrupalExtension Context (the Drupal Extension provides the ability to designate a “region” based on a CSS selector)
FeatureContext.php
• Change Class Extension from:
class FeatureContext extends BehatContext
• to
class FeatureContext extends Drupal\ DrupalExtension\Context\DrupalContext