Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario...
Transcript of Behat - events.drupal.org · What is Behat? Behat does one simple thing: ** each line in a scenario...
![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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/2.jpg)
> 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/3.jpg)
Plan, Work, Miscommunicate, Panic, Put out Fires, Repeat!
aka Project Work!
![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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/11.jpg)
@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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/13.jpg)
@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://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/16.jpg)
“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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/17.jpg)
@weaverryan
Evolution of Test-Driven Development
≈ Unit Tests
≈ Functional Tests
![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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/20.jpg)
@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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/21.jpg)
@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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/23.jpg)
@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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/25.jpg)
@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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/38.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/39.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/40.jpg)
... now let’s turn them into tests!
![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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/42.jpg)
@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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/43.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/44.jpg)
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 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/46.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/47.jpg)
![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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/48.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/49.jpg)
$> 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/50.jpg)
<?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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/51.jpg)
<?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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/52.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/53.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/54.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/55.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/56.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/57.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/58.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/59.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/60.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/61.jpg)
/** * @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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/62.jpg)
/** * @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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/63.jpg)
![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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/64.jpg)
![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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/66.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/67.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/68.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/69.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/70.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/71.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/72.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/73.jpg)
// 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/74.jpg)
$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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/75.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/76.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/77.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/78.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/79.jpg)
{ “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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/80.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/81.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/82.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/83.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/84.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/85.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/86.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/87.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/88.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/89.jpg)
# 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/90.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/91.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/92.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/93.jpg)
@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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/94.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/95.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/96.jpg)
‣ 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/97.jpg)
... 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/98.jpg)
... 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/99.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/100.jpg)
@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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/101.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/102.jpg)
# 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/103.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/104.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/105.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/106.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/107.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/108.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/109.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/110.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/111.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/112.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/113.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/114.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/115.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/116.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/117.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/118.jpg)
... 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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/119.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022052518/5f07ec037e708231d41f6e1b/html5/thumbnails/120.jpg)
Ryan Weaver @weaverryan
THANK YOU!