Behat and BDD web
training (PHP)
2 days
Who is the training for?
•

Ideal for testers wanting to gain BDD and
automation testing experience

•

Some programming knowledge required but not
essential

•

Testers involved with website testing

•

Testers wanting to gain technical skills

•

PHP programmers and Drupal programmers

2

www.time2test.co.uk
High level
•

BDD overview

•

Step Definitions

•

Behat History

•

Context Class

•

PHP Versions

•

Assertions

•

Installation

•

Command Line

•

Quick Usage Reference

•

Gherkin Language

•

Features

•

Mink Web Testing


3

www.time2test.co.uk
What will you learn?
•

Behaviour Driven
Development Overview

•

Behat Overview and
configuration

•

Gherkin Language Explained

•

Syntax - Givens, Whens,
Thens, And, But

•

Test Data

•


4

Command Line usage

•

Understand Features,
Scenarios, Step
Definitions

Context Class and
Assertions

•

End to End Behat
examples

•

•

Mink for web testing
www.time2test.co.uk
schedule
Day 1

Day 2

•

php primer

•

gherkin

•

behat background

•

case studies

•

mink api


5

www.time2test.co.uk
PHP Primer
overview

Lets have a quick PHP programming recap


7

www.time2test.co.uk
syntax
PHP tags
<?php … ?>
!

Comments with #


8

www.time2test.co.uk
variables
•

All variables lead with $

Integers $int_var = 12345;

•

assignments with =
operator

Doubles $doubles = 3.42

•

•

Boolean TRUE or FALSE
constants

variables don’t need to
be declared

Null $my_var = NULL;

no need to specify the
type e.g. String or int

Strings $string = “This is
some text”;


9

www.time2test.co.uk
Constants
•

identifier that can not change

•

UPPERCASE

•

define(“NAME”, value);

•

echo constant(“NAME”);

•

Magic constants - ___LINE___, ___FILE___ e.t.c

10

www.time2test.co.uk
Operators
•

Arithmetic Operators ( +, -, *, %, ++, —)

•

Comparison Operators ( ==, !=, >, <, >=, <=)

•

Logical operators ( and, or, &&, ||, !)

•

Assignment operators ( =, +=, -=, *=, /=, %=


11

www.time2test.co.uk
Decisions

•

If… Else

•

elseIf

•

Switch


12

www.time2test.co.uk
Loops
for (initial; condition; increment) { code to be executed; }
while ( condition) { code to be executed; }
do { code to be executed; } while ( condition);
foreach (array as value) { code to be exe; }
break
continue

13

www.time2test.co.uk
arrays

•

numeric arrays - numeric index

•

associative array - strings used as index

•

multidimensional array - array of arrays


14

www.time2test.co.uk
strings
•

single quotes versus double quotes

•

string concatenation with a .

•

strlen($mystring) function

•

strpos($mystring, $searchstring) function


15

www.time2test.co.uk
File Input/Output
•

fopen() - open a file

•

filesize() - file length

•

fread() - read a file

•

fclose() - close a file


16

www.time2test.co.uk
functions
•

functions

•

functions with parameters

•

functions with return values

•

functions with default parameters


17

www.time2test.co.uk
regular expressions

•

are a sequence of pattern of characters

•

provide pattern matching

•

POSIX and PERL style matching


18

www.time2test.co.uk
Exceptions Handling

•

try

•

throw

•

catch


19

www.time2test.co.uk
Debugging
•

missing semicolons

•

not enough equal signs

•

misspelled variable names

•

missing dollar signs

•

troubling quotes

•

missing parentheses and curly brackets

•

array index

20

www.time2test.co.uk
date

•

time() - returns seconds from 1 jan 1970

•

getdate() - returns an associative array

•

date() - returns formatted string


21

www.time2test.co.uk
Object Orientated
Programming
•

class

•

object

•

member variables

•

member functions

•

inheritance

•

constructors

22

www.time2test.co.uk
class - $this

•

$this - is a special variable and it refers to the same
object i.e itself


23

www.time2test.co.uk
objects
•

using new keyword

•

$travel = new Books;

•

call member functions

•

$travel->setTitle(“travel to london”);

•

$travel->setPrice(100);

24

www.time2test.co.uk
constructors

•

special functions which are automatically called
whenever an object is created/ initialised

•

__construct() to define a constructor with
arguments


25

www.time2test.co.uk
public, private and
protected members
•

public members are accessible insside, outside
and in another the class to which is declared

•

private members are limited to the class its
declared

•

protected members are limited to the class its
declared and extended classes only


26

www.time2test.co.uk
interfaces

•

common function names to implementors who then
develop code


27

www.time2test.co.uk
constants

•

declared constants can not change


28

www.time2test.co.uk
abstract classes

•

abstract classes can not be instantiated , only
inherited


29

www.time2test.co.uk
static

•

static members or methods are accessible without
needing an instantiation of the class


30

www.time2test.co.uk
final

•

final methods can not be overdided by child
classes


31

www.time2test.co.uk
parent constructors

•

sub classes can extend the parent constructors.


32

www.time2test.co.uk
Environment
overview
•

Mac

•

Windows

•

Unix

•

PHP 5 installed

•

PHP IDE

34

www.time2test.co.uk
PHP IDEs

•

Integrated Development environments

•

many available -free and paid

•

Windows or Mac or Linux


35

www.time2test.co.uk
Background
What is BDD?
•

human readable stories

•

define business outcomes and drill into features

•

testing framework

•

Extends TDD - test driven development

•

Write a test that fails then watch it pass

37

www.time2test.co.uk
Why BDD?
•

Deliver what you client wants

•

Better communications and better collaboration

•

Extends the principles of TDD ( Test Data Driven)
testing.


38

www.time2test.co.uk
Behat History

•

Behat is the PHP version of Cucumber

•

created by Konstantin Kudryashov (@everzet)


39

www.time2test.co.uk
What does Behat do?
Scenario Step

Given I have a file named “foo”

regex

Given /^I have a file named“([^”$/

Definition

public function iHaveAFileNamed($file{

Do some work

touch($file)

Pass and Fal at each step unless an exception is
thrown

40

www.time2test.co.uk
Good BDD
•

practice

•

get into the zone for creating features and
scenarios

•

for web testing - understand the mink api


41

www.time2test.co.uk
Quick Overview
overview

•

lets jump straight into an end to end example using
Behat and Mink


43

www.time2test.co.uk
composer

•

dependency manager for PHP external libraries


44

www.time2test.co.uk
dependencies

•

download the dependencies using a
composer.json file

•

$php composer.phar install


45

www.time2test.co.uk
behat —help

•

$>php bin/behat —help


46

www.time2test.co.uk
behat.yml

•

create this file at root level

•

settings file

•

pronounce ( ya-mul ?)


47

www.time2test.co.uk
initialise project
•

$>php bin/behat —init

•

This will create some directories

•

+d features - place your *.feature files here

•

+d features/bootstrap - place bootstrap scripts and static
files here

•

Also will create the file FeatureContext.php

•

+f features/bootstrap/FeatureContext.php - place your
feature related code here

48

www.time2test.co.uk
FeatureContext.php

•

Extend context from MinkContext instead of the
BehatContext


49

www.time2test.co.uk
Feature
Feature: Search
In order to find an article
As a website user
I need to be able to search for am article


50

www.time2test.co.uk
Execute Feature
!

$bin/behat features/search.feature:
•

Magic happens and test results shown

•

Good for headless browser without javascript


51

www.time2test.co.uk
Selenium
•

Download selenium server from seleniumhq

•

run selenium standalone server

•

java -jar selenium-server-standalone-2.38.0.jar

•

Tag your scenario with @javascript annotation


52

www.time2test.co.uk
javascript test

Run in a browser that supports javascript
@javascript


53

www.time2test.co.uk
javascript execute
!

•

$bin/behat features/search.feature:

•

The Firefox browser is invoked and the test
execution is visible


54

www.time2test.co.uk
implement a new step
execution will conveniently come back with a list of
undefined steps

•
/**

* @Given /^I wait for (d+) seconds$/
*/
public function iWaitForSeconds($arg1)
{
throw new PendingException();
}


55

www.time2test.co.uk
new step definition code
// copy & paste at features/bootstrap/FeatureContext.php

!
/**
* @Given /^I wait for (d+) seconds$/
*/
public function iWaitForSeconds($seconds)
{
$this->getSession()->wait($seconds*1000);
}


56

www.time2test.co.uk
Configuration

•

behat.yml


57

www.time2test.co.uk
Define a Feature
•

4 line description

•

a line describes the feature

•

three following lines describe the benefit, the role
and feature itself


58

www.time2test.co.uk
Define a Scenario
•

many scenarios per feature

•

three line syntax

•

Given - describe the initial state

•

When - action the user takes

•

And Then - what the user sees after the action

59

www.time2test.co.uk
Keywords
•

Given

•

And

•

When

•

Then


60

www.time2test.co.uk
Execution

•

$> behat


61

www.time2test.co.uk
Writing Step Definitions

•

behat matches each statement of a scenario to a
list of regular expression steps

•

Behat helps by creating an outline for undefined
steps


62

www.time2test.co.uk
Regular expressions

•

Behat and Mink rely on regex

•

here is a quick masterclass on regex


63

www.time2test.co.uk
multi lines

•

triple quotation syntax (“””)


64

www.time2test.co.uk
Directory Structure
•

behat —init

•

features

•

features/bootstrap/ *.php files

•

features/bootstrap/featureContext.php


65

www.time2test.co.uk
Further Details
Features Explained

•

features are in a format called Gherkin

•

each feature file consists of a single feature

•

each feature will consist of a list of scenarios


67

www.time2test.co.uk
Step Definitions
•

written in php

•

consists of a keyword, regular expression and a
callback

•

the regex is a method argument


68

www.time2test.co.uk
Context Class

•

behat creates a context object for each scenario


69

www.time2test.co.uk
Assertions with PHPUnit
//
require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/
Functions.php';
//
•

assertEquals($numberOfRows, count($elements));

70

www.time2test.co.uk
Command Line Usage
>>behat —h - help options
>>behat —v version
>>behat —dl - view the step definitions
>>behat —init - setup a features dir and
featurecontext.php file


71

www.time2test.co.uk
Gherkin Language
Overview

•

common language for behaviour driven testing


73

www.time2test.co.uk
Syntax

•

line orientated language

•

parser divides input into features, scenarios and
steps.


74

www.time2test.co.uk
Features

•

each feature file consist of a single feature

•

keyword feature: and three indented lines


75

www.time2test.co.uk
Scenarios

•

each scenario begins with keyword Scenario:
followed by an optional scenario title


76

www.time2test.co.uk
scenario outlines

•

template placeholders for easy multi input


77

www.time2test.co.uk
backgrounds

•

run before each scenario

•

background: kewword


78

www.time2test.co.uk
Steps

•

features consist of steps: given, when, then


79

www.time2test.co.uk
Given

•

known state before the user or system interacts
with the application under test


80

www.time2test.co.uk
When

•

user action performed on the system under test


81

www.time2test.co.uk
Then

•

observe the outcome and related to the feature
description


82

www.time2test.co.uk
And, But

•

Use and and but for elegant and natural reading

•

not needed but good practice


83

www.time2test.co.uk
Test Data

•

Tables used for injecting data - different from
scenario outlines

•

PyStrings - used for larger text input across many
lines


84

www.time2test.co.uk
tags

•

used to organise features and scenarios


85

www.time2test.co.uk
Mink - Web Testing
Overview

•

Mink is an Extension to Behat

•

Think of a plugin


87

www.time2test.co.uk
What is Mink?
•

Standalone library to use PHP to command a
browser

•

API to send commands to Selenium, Goutte,
ZombieJS and more

•

The extension allows for BDD tests to be created
without actually writing any PHP code


88

www.time2test.co.uk
Installation
•

Update composer.json to include

•

Mink

•

MinkExtension

•

Goutter adn Selenium2 drivers for Mink

•

Then update the vendor libraries ising

•

$>php composer.phar update

89

www.time2test.co.uk
MinkExtension

•

behat.yml is the behat config file

•

http://docs.behat.org/guides/7.config.html


90

www.time2test.co.uk
MinkContext Extension
•

Gives us access to the Mink Session to allow us to
send commands to a browser

•

Inheritance of a pre-existing definitions

•

Before extending : we have 4 definitions

•

After extending : there are lots of definitions

•

Try it: $>php bin/behat -dl

91

www.time2test.co.uk
wikipedia example

•

lets look at a wikipedia example


92

www.time2test.co.uk
First Example - Mink
headless

lets look at a headless mink example


93

www.time2test.co.uk
example - Mink with
Selenium

•

lets look at a javascript browser example


94

www.time2test.co.uk
Mink api
Overview

•

http://mink.behat.org/api/index.html

•

Lets look at some common Mink API


96

www.time2test.co.uk
visit()

•

$this->visit('/');


97

www.time2test.co.uk
fillField

•

fillField('email', $email);


98

www.time2test.co.uk
pressButton()

•

pressButton('Login');


99

www.time2test.co.uk
getSession()

•

$this->getSession();


100

www.time2test.co.uk
getPage()

•

getPage();


101

www.time2test.co.uk
findAll

•

css elements example

•

findAll(‘css’,’css_value_goes_here’);


102

www.time2test.co.uk
findButton()

•

findButton(‘html_link_name’);


103

www.time2test.co.uk
findButton()->click()

•

having found the button element , you wish to click

•

findButton(‘html-link’)->click();


104

www.time2test.co.uk
locatePath()

•

based on current session, now goto the defined
path

•

visit($this->locatePath(‘/user’));


105

www.time2test.co.uk
getStatusCode()

•

return the HTTP status code from the current
session

•

$session->getStatusCode();


106

www.time2test.co.uk
getCurrentUrl()

•

->getCurrentUrl();


107

www.time2test.co.uk
Next Steps
Practice
•

Try out the different mink web emulators

•

Sublime extension

•

phantom.js

•

Symfony

•

Drupal extension

109

www.time2test.co.uk
case studies
Wordpress casestudy

•

Wordpress Behat Mink Context Example


111

www.time2test.co.uk
case study 2

•

Lets look at an example - behat with mink


112

www.time2test.co.uk
case study 3

•

Lets look at an example - behat ,mink , website


113

www.time2test.co.uk
case study 4

•

google search


114

www.time2test.co.uk
case study 5


115

www.time2test.co.uk
case study 6

•

features master class


116

www.time2test.co.uk
case study 7

•

ls example on unix


117

www.time2test.co.uk
case study 8

•

open scholar project

•

good established behat mink framework


118

www.time2test.co.uk
case study 9

•

Behat + Mink demo github


119

www.time2test.co.uk
Conclusions
goals and objectives
•

Review your goals.

•

Have we met your expectations?

•

Email us with your feedback


121

www.time2test.co.uk
Thank you

•

From the Time2test Team


122

www.time2test.co.uk

Behat bdd training (php) course slides pdf