Top Banner
User Acceptance Testing with Behat, Mink, and PhantomJS Chattanooga PHP Developers Group December 3, 2014
29
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: User Acceptance Testing with Behat, Mink and PhantomJS

User Acceptance Testing with Behat, Mink, and

PhantomJSChattanooga PHP Developers Group

December 3, 2014

Page 2: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 3: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 4: User Acceptance Testing with Behat, Mink and PhantomJS

Why is this important?

Page 5: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 6: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 7: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 8: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 9: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 10: User Acceptance Testing with Behat, Mink and PhantomJS

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)

Page 11: User Acceptance Testing with Behat, Mink and PhantomJS

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”

Page 12: User Acceptance Testing with Behat, Mink and PhantomJS

Feature

• A “Feature” is comprised of a User Story and it’s supporting Acceptance Criteria

Page 13: User Acceptance Testing with Behat, Mink and PhantomJS

As a PHP Developer, how do I get there?

...You start with Behat

Page 14: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 15: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 16: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 17: User Acceptance Testing with Behat, Mink and PhantomJS

But, wait, you said...

• Tests were written in “business speak”

!

• And they are….using a structured, highly readable language called Gherkin

Page 18: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 19: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 20: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 21: User Acceptance Testing with Behat, Mink and PhantomJS

• Behat is Pluggable, through extensions

• Mink Extension is a Behat Plugin

• Extensions exist for various frameworks including:

• Drupal

• Symfony/Symfony2

• Yii

Page 22: User Acceptance Testing with Behat, Mink and PhantomJS

Let’s Play

Page 23: User Acceptance Testing with Behat, Mink and PhantomJS

!

!

{! "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

Page 24: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 25: User Acceptance Testing with Behat, Mink and PhantomJS

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

Page 26: User Acceptance Testing with Behat, Mink and PhantomJS

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)

Page 27: User Acceptance Testing with Behat, Mink and PhantomJS

FeatureContext.php

• Change Class Extension from:

class FeatureContext extends BehatContext

• to

class FeatureContext extends Drupal\ DrupalExtension\Context\DrupalContext

Page 28: User Acceptance Testing with Behat, Mink and PhantomJS

Predefined Steps

$bin/behat -di !

or !

$bin/behat -dl

Page 29: User Acceptance Testing with Behat, Mink and PhantomJS

Write your Feature

• Saved in the features/ directory in a file called <something>.feature

• Can have multiple .feature files in this directory

• All will get executed unless otherwise specified