Top Banner
Behat BDD, FUNCTIONAL TESTS & SELENIUM (IN DRUPAL!) ’s
120

Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Jun 17, 2020

Download

Documents

dariahiddleston
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: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Behat

BDD, FUNCTIONAL TESTS & SELENIUM (IN DRUPAL!)

♥’s

Page 2: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

> Lead of the Symfony documentation team

> KnpLabs US - Symfony consulting, training & kumbaya

> Writer for KnpUniversity.com: PHP & Symfony screencasts packed with puns, unrelated (but entertaining) illustrations and coding challenges!> Husband of the much more talented @leannapelham

knpuniversity.com twitter.com/weaverryan

Hallo!

Page 3: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Plan, Work, Miscommunicate, Panic, Put out Fires, Repeat!

aka Project Work!

Page 4: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

How the customer explained it

http://www.projectcartoon.com

Page 5: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

How the project leader understood ithttp://www.projectcartoon.com

Page 6: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

How the programmer wrote it

http://www.projectcartoon.com

Page 7: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

What the customer really neededhttp://www.projectcartoon.com

Page 8: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

What the beta testers receivedhttp://www.projectcartoon.com

Page 9: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Computer Science?

https://www.flickr.com/photos/diueine/3604050776

Page 10: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Different roles, different languages, miscommunication

@weaverryan

One

Page 11: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

Two

Your code and business values may not align

Page 12: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

I've just dreamt up this cool new feature that we should implement!

Why? Because it's cool!

Page 13: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

Three

Over-planning, under-planning,

planning...?

Page 14: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

https://www.flickr.com/photos/tambako/4175456498

Getting down with BDD

Page 15: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Evolution of Test-Driven Development

@weaverryan

Page 16: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

“Behaviour” is a more useful word, than “test”

- Dan North *

* the santa of behavior-driven development

Page 17: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

Evolution of Test-Driven Development

≈ Unit Tests

≈ Functional Tests

Page 18: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Specification BDD

http://www.phpspec.net

@weaverryan

Page 19: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Scenario-oriented BDD (Story BDD)

Page 20: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

Let’s create a single vocabulary and process

" Project Managers

" Developers

" QA

" Business Analyst

" Product Owners

Page 21: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

1. Define business value for the features

2. Prioritize features by their business value

3. Describe them with readable scenarios

4. And only then - implement them

Calming the Chaos

Page 22: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Gherkin

Page 23: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

Gherkin ==

a structured language to describe a feature

Page 24: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Feature: {custom_title} In order to {A} As a {B} I need to {C}‣ {A} - the benefit or value of the feature

‣ {B} - the role (or person) who will benefit

‣ {C} - short feature description

| The person “writing” this feature - the “I”

Page 25: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

1. Define business value for the features

2. Prioritize features by their business value

3. Describe them with readable scenarios

4. And only then - implement them

Calming the Chaos

Page 26: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Feature: I18n In order to read news in french As a french user I need to be able to switch locale

Read news in French

@weaverryan

Page 27: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Feature: I18n In order to read news in french As a french user I need to be able to switch locale

The business value

Read news in French

@weaverryan

Page 28: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Feature: I18n In order to read news in french As a french user I need to be able to switch locale

The person who benefits +

The “author” of this feature

Read news in French

@weaverryan

Page 29: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Feature: I18n In order to read news in french As a french user I need to be able to switch locale

Description of the feature, the action the

person will take

Read news in French

@weaverryan

Page 30: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

1. Define business value for the features

2. Prioritize features by their business value

3. Describe them with readable scenarios

4. And only then - implement them

@weaverryan

Calming the Chaos

Page 31: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Prioritize...

1) Feature: News admin panel

2) Feature: I18n

3) Feature: News list API

Page 32: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Solution

1. Define business value for the features

2. Prioritize features by their business value

3. Describe them with readable scenarios

4. And only then - implement them

@weaverryan

Page 33: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Feature: News admin panel In order to maintain a list of news As a site administrator I need to be able to edit news

Scenario: Add new article Given I am on the "/admin/news" page When I click "New Article" And I fill in "Title" with "Learned BDD" And I press "Save" Then I should see "A new article was added"

Page 34: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Scenarios

Given

Defines the initial state of the system for the scenario

Scenario: Add new article Given I am on the "/admin/news" page When I click "New Article" And I fill in "Title" with "Learned BDD" And I press "Save" Then I should see "A new article was added"

Page 35: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

When

Describes the action taken by the person/role

Scenario: Add new article Given I am on the "/admin/news" page When I click "New Article" And I fill in "Title" with "Learned BDD" And I press "Save" Then I should see "A new article was added"

Scenarios

Page 36: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Then

Describes the observable system state after the action has been performed

Scenario: Add new article Given I am on the "/admin/news" page When I click "New Article" And I fill in "Title" with "Learned BDD" And I press "Save" Then I should see "A new article was added"

Scenarios

Page 37: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

And/But

Can be added to create multiple Given/When/Then lines

Scenario: Add new article Given I am on the "/admin/news" page When I click "New Article" And I fill in "Title" with "Learned BDD" And I press "Save" Then I should see "A new article was added"

Scenarios

Page 38: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Example #2

Scenario: List available articles Given there are 5 news articles And I am on the "/admin" page When I click "News Administration" Then I should see 5 news articles

Page 39: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Gherkin gives us a consistent

language for describing features and their scenarios

@weaverryan

Page 40: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

... now let’s turn them into tests!

Page 41: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

http://bit.ly/behatch-t

Page 42: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

What is Behat?

Behat does one simple thing:

** each line in a scenario is called a “step”

Behat “executes” your scenarios, reading each step and calling the function associated with it

It maps each step** to a PHP Callback

Page 43: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Installing Behat

Behat is just a library that can be installed easily in

any project via Composer

New to Composer? Free screencast cures it! KnpUniversity.com/screencast/composer

Page 44: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

In your project directory...

1) Download Composer

$> curl -s http://getcomposer.org/installer | php

2) Create (or update) composer.json for Behat

$> php composer.phar require --dev behat/behat

Page 45: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

{ “require-dev": { "behat/behat": "^3.1" }}

bit.ly/behat3-composer

Page 46: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

The most important product of the installation

is an executable vendor/bin/behat file

Page 47: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and
Page 48: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

To use Behat in a project you need:

1) Actual *.feature files to be executed

2) A FeatureContext.php file that holds the PHP callbacks for each step

3) (optional) A behat.yml configuration file

@weaverryan

Page 49: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

$> php vendor/bin/behat --init

Page 50: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

<?php// features/bootstrap/FeatureContext.php use Behat\Behat\Context\SnippetAcceptingContext; use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; /** * Behat context class. */ class FeatureContext implements SnippetAcceptingContext { }

Page 51: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

<?php// features/bootstrap/FeatureContext.php use Behat\Behat\Context\SnippetAcceptingContext; use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; /** * Behat context class. */ class FeatureContext implements SnippetAcceptingContext { }

Page 52: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Pretend you’re testing the “ls” program

Page 53: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

1) Describe your Feature

Feature: ls

features/ls.feature

In order to see the directory structure As a UNIX user I need to be able to list the current directory's contents

Page 54: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

2) Your First Scenario

If you have two files in a directory, and you're running the command - you should see them listed.

Page 55: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Scenario: List 2 files in a directory

Write in the natural voice of “a UNIX user”

Given I have a file named "foo" And I have a file named "bar" When I run "ls" Then I should see "foo" in the output And I should see "bar" in the output

features/ls.feature

Page 56: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

3) Run Behat

$> php vendor/bin/behat

Page 57: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Behat tries to find a

method in FeatureContext

for each step

Page 58: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Matching is done with

simple wildcards

For each step that doesn’t have a matching method, Behat prints code to copy

into FeatureContext

Page 59: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

class FeatureContext extends BehatContext{ /** @Given I have a file named :file */ public function iHaveAFileNamed($file) { throw new PendingException();

}

/** @When I run :command */ public function iRun($command) { throw new PendingException(); }

// ...}

4) Copy in the new Definitions

Quoted text maps to a method

argument

Page 60: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

5) Make the definitions do what they need to

Page 61: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

/** * @Given I have a file named :file */public function iHaveAFileNamed($file) { touch($file);}

/** * @Given I have a directory named :dir */public function iHaveADirectoryNamed($dir) { mkdir($dir);}

Page 62: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

/** * @When I run :command */public function iRun($command) { exec($command, $output); $this->output = trim(implode("\n", $output));}

/** * @Then I should see :string in the output */public function iShouldSeeInTheOutput($string) { if (strpos($this->output, $string) === false) { throw new \Exception(‘Did not see’.$string); );}

Page 63: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and
Page 64: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and
Page 65: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

See the full FeatureContext class:

http://bit.ly/behat-ls-feature

Page 66: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Scenario Step

Definition

Given I have a file named “foo”

pattern

public function iHaveAFileNamed($file) {

do work

Pass/Fail: Each step is a “test”, which passes *unless* an exception is thrown

touch($file);

@Given I have a file named :file

What Behat *does*

Page 67: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Creating files and directories in FeatureContext is nice...

Page 68: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

but wouldn’t it be really cool to command a browser, fill out forms and check the output?

Page 69: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Mink

https://www.flickr.com/photos/15016964@N02/5696367600

Page 70: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Mink!

http://mink.behat.org/

‣ A standalone library to use PHP to command a “browser”

‣ One easy API that can be used to command Selenium, Goutte, PhantomJS, etc

@weaverryan

Page 71: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

A sample of Mink

Page 72: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

use Behat\Mink\Driver\GoutteDriver;use Behat\Mink\Session;

// change *only* this line to run// in Selenium, etc$driver = new GoutteDriver();$session = new Session($driver);

Page 73: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

// visit a page$session->visit('http://behat.org');

echo 'URL : '.$session->getCurrentUrl();

echo 'Status: '.$session->getStatusCode();

Page 74: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

$page = $session->getPage();

// drill down into the page$ele = $page->find('css', 'li:nth-child(4) a');

echo 'Link text is: '.$ele->getText();echo 'href is: '.$ele->getAttribute('href');

// click the link// (you can also fill out forms)$ele->click();

Page 75: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Mink inside FeatureContext =>

Dangerous Combo for Functional Testing

Page 76: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

http://mink.behat.org/

Behat Mink

Integration

MinkExtension

‣ An “Extension” is like a Behat plugin

‣ The MinkExtension makes using Mink inside Behat a matter of configuration

@weaverryan

Page 77: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Install Mink & MinkExtension

‣ Update composer.json to include: > Mink

> MinkExtension > Goutte and Selenium2 Drivers for Mink

@weaverryan

Page 78: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

composer require --dev \ behat/mink-extension \ behat/mink-goutte-driver \ behat/mink-selenium2-driver

Page 79: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

{ “require-dev": { "behat/behat": "^3.1", "behat/mink-extension": "^2.2", "behat/mink-goutte-driver": "^1.2", "behat/mink-selenium2-driver": "^1.3" }}

http://bit.ly/behat-mink-composer

Page 80: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Goal:

To easily use Mink inside FeatureContext

Page 81: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Bootstrap MinkExtension# behat.yml default: extensions: Behat\MinkExtension: goutte: ~ selenium2: ~ # The base URL you're testing base_url: http://en.wikipedia.org/

@weaverryan

Page 82: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Extend MinkContext

use Behat\MinkExtension\Context\RawMinkContext; /** * Behat context class. */ class FeatureContext extends RawMinkContext

@weaverryan

Page 83: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Access to a Mink Sessionclass FeatureContext extends RawMinkContext{ public function doSomething() { $session = $this->getSession(); $session->visit('http://behat.org'); }

// ...}

Our custom definitions can now command a browser!

Page 84: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

More! Add MinkContext# behat.yml default: extensions: # ... suites: default: contexts: - FeatureContext - Behat\MinkExtension\Context\MinkContext

Behat now parses definitions from *our* class *and* this MinkContext class

Page 85: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

We inherit a pile of great definitions

the -dl option prints all current definitions

Before adding MinkContext:

Page 86: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

After adding MinkContext:

Page 87: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

In other words: We can write some tests for our app without writing any

PHP code

@weaverryan

Page 88: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Suppose we’re testing Wikipedia.org

Page 89: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

# features/wikipedia.featureFeature: Search In order to see a word definition As a website user I need to be able to search for a word

These 4 definitions all come packaged with MinkContext

Scenario: Searching for a page that does exist Given I am on "/wiki/Main_Page" When I fill in "search" with "Behavior Driven Development" And I press "searchButton" Then I should see "agile software development"

Page 90: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Celebration!

Page 91: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Behat in your application

Page 92: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Behat in your App

https://www.flickr.com/photos/yoanngd/10669976224

Page 93: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

Getting “under the hood”

‣ Black-box testing: the site lives out on the web

‣ Because of this, we can’t: a) access/clear/prepare the database b) use any code in our application

Page 94: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

When testing: you should guarantee the starting

condition of your environment

Page 95: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

How can we add nodes, add users, and configure

permissions from inside Behat?

Page 96: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

‣ Install Behat & Mink

‣ ??? Gain access to Drupal functionality from inside FeatureContext

‣ Create nodes, users, etc so that you’re testing against a predictable dataset

@weaverryan

Behat & Drupal

‣PROFIT!

Page 97: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

... there’s a library made by the Drupal community ...

Fortunately...

Page 98: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

... which I did not help with ...

Page 99: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

DrupalExtension!

http://bit.ly/drupal-extension

A plugin (extension) for Behat and Drupal

jhedstrom

Page 100: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

@weaverryan

DrupalExtension

2) Build nodes, add users, manage permissions inside Behat

3) Operating within Regions

4) Hooks to load more sentences/definitions from contrib modules

1) Even more built-in sentences/definitions

Page 101: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Background: Given I am logged in as a user with the "administrator" role

Scenario: Edit Node Given I am viewing a "page" with the title "Cool beans!" When I click "Edit" in the "Body" region And I fill in the following: | Body | Ipsumm | And I press "Save" Then I should see "Ipsumm" in the "Body" region

# features/node_manage.feature

Page 102: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

# features/node_manage.featureBackground: Given I am logged in as a user with the "administrator" role

Scenario: Edit Node Given I am viewing a “page" with the title "Cool beans!" When I click "Edit" in the "Body" region And I fill in the following: | Body | Ipsumm | And I press "Save" Then I should see "Ipsumm" in the "Body" region

Creates a user and adds a role to it

Page 103: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Background: Given I am logged in as a user with the "administrator" role

Scenario: Edit Node Given I am viewing a "page" with the title "Cool beans!" When I click "Edit" in the "Body" region And I fill in the following: | Body | Ipsumm | And I press "Save" Then I should see "Ipsumm" in the "Body" region

Creates a “page” node in the database

# features/node_manage.feature

Page 104: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Background: Given I am logged in as a user with the "administrator" role

Scenario: Edit Node Given I am viewing a "page" with the title "Cool beans!" When I click "Edit" in the "Body" region And I fill in the following: | Body | Ipsumm | And I press "Save" Then I should see "Ipsumm" in the "Body" region

Looks for the text in a CSS region you’ve defined as “Body”

# features/node_manage.feature

Page 105: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

And it’s alive!

Page 106: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

The 3 Modes of the DrupalExtension

1) blackbox: test an external server, no access to the database

2) drupal: Bootstraps Drupal’s code and calls functions

3) drush: Interacts with Drupal via drush

Page 107: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

What if my page/test rely on JavaScript

Page 108: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Behat/Mink does not support testing pages that

use JavaScript

Page 109: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

jk!

Page 110: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Add @javascript

# ...

@javascriptScenario: Edit Node Given I am viewing a "page" with the title "Cool beans!" When I click "Edit" in the "Body" region And I fill in the following: | Body | Ipsumm | And I press "Save" Then I should see "Ipsumm" in the "Body" region

Page 111: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Add @javascript

# ...

@javascriptScenario: Edit Node Given I am viewing a "page" with the title "Cool beans!" When I click "Edit" in the "Body" region And I fill in the following: | Body | Ipsumm | And I press "Save" Then I should see "Ipsumm" in the "Body" region

Yep, that’s all you do!

Page 112: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Download and start Selenium

$> wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar

$> java -jar selenium-server-standalone-2.53.0.jar

Page 113: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Re-run the tests

Page 114: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Yes, add only 1 line of code to run a test in Selenium

Page 115: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Bonus!

Mink directly via PHPUnit?

See BrowserTestBase

**and (the new) JavascriptTestBase

@weaverryan

Page 116: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

It’s Simple!

https://www.flickr.com/photos/dtelegraph/5907116936

Page 117: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

1) Install Behat

http://knpuniversity.com/screencast/behat

Page 118: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

... and learn more about what you can do with Mink: http://mink.behat.org/

2) Write features for your app!

Page 119: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

3) high-five your teammates

https://www.flickr.com/photos/nickwebb/3904325807

Page 120: Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario is called a “step” Behat “executes” your scenarios, reading each step and

Ryan Weaver @weaverryan

THANK YOU!