Creating Better
Behat Features
Behat
➔Created by @everzet
➔BDD framework
➔Scenarios written in Gherkin:
➔Lends itself to describing UI
Given a precondition
When an action happens
Then an outcome can be tested
Given I am a logged in user
When I subscribe to a thread
Then I should receive confirmation
The Mink
➔PHP toolkit for interacting with web
pages
➔Abstracts different browser drivers
◆headless browsers
◆Selenium
➔Web page interaction
$active = $element->find(‘css’, ‘#status’)-
>hasText(‘open’);
Behat and MinkContext
➔Built in browser interaction
➔Predefined step definitions:
I fill in "#comment-form textfield" with "Hello!"
I should see "Hello!" in the "#comments" element
I fill in "rob" for "form input[name=’username’]"
etc.
STOF!
not me
Behat and MinkContext
➔Tightly coupled
➔Lack of ubiquitous language
➔Confined to MinkContext definitions
◆Moulding your scenarios to fit
MinkContext
➔Bloody difficult to maintain
Behat and MinkContext
Introducing PageObjects
➔Implementation by SensioLabs
➔Encapsulate a web page
➔Extends Mink’s DocumentElement
class
➔Removes coupling from feature files
PageObjects and Elements
➔Declare elements in the class
➔$form = $this-
>getElement(‘login.form’);
◆returns an extension of Mink’s Element
➔Interact with the elements:
◆$form->pressButton(‘Login’);
PageObjects
PageObjects within a
Context
➔Contexts extend PageObjectContext
➔Easily get a page object:
◆$page = $page->get(‘Homepage’);
➔Open a page (as though you were in
a browser)
◆$page = $page->get(‘Homepage’)-
>open();
◆->open([‘id’ => ‘12345’])
PageObjects within a
Context
My process
1. Write feature files
2. Run Behat
3. Add generated step definitions to
Context
4. Create a PageObject to represent a
page
5. Write step definitions
6. Write code
Behat in your application
➔Keep everything together
/
app/
bin/
features/
tasks.add.feature
tasks.remove.feature
bootstrap/
fixtures/
Vivait/
Context/
Page/
spec/
src/
Behat and Databases
KnpLabs - FriendlyContexts
➔Lots of lovely contexts
◆ But be careful not to reintroduce coupling!
➔AliceContext
@reset-schema @alice(groups) @alice(users)
@alice(filters)
Feature: Pinning filters
In order to easily access filtered issues
As a reporter
I should be able to pin my issues to the main menu
➔Better organised
➔Easier to
maintain
➔Less coupling
➔Nicer to read
End
➔Robin
Cawser
➔Viva IT
Ltd.
➔@robcaws
er
Some links
➔http://docs.behat.org/en/latest/
➔https://github.com/sensiolabs/BehatPageObj
ectExtension
➔https://github.com/KnpLabs/FriendlyContexts

Creating better behat features