BDD con Behat y Mink en Symfony2
-
Upload
carlos-granados -
Category
Economy & Finance
-
view
120 -
download
7
description
Transcript of BDD con Behat y Mink en Symfony2
![Page 1: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/1.jpg)
![Page 2: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/2.jpg)
![Page 3: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/3.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Carlos Granados
![Page 4: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/4.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
![Page 5: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/5.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
![Page 6: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/6.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
![Page 7: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/7.jpg)
• Desarrollo basado en funcionalidad
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
¿Qué es BDD?
• Desarrollo basado en comportamiento• Pasar tests != funcionalidad conseguida • Historias en lenguaje natural y compartido• Lenguaje definido y automatizable • Las historias dirigen nuestro desarrollo• Podemos comprobar la funcionalidad
![Page 8: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/8.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Historias (Stories)
• Características (Features)• As a [role] I want [feature] so that [benefit]• Escenarios (Scenarios) y pasos (Steps)• Precondiciones (Given …)• Acciones (When…)• Resultados (Then…)
![Page 9: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/9.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Feature: Account Holder withdraws cash
As an Account HolderI want to withdraw cash from an ATMSo that I can get money when the bank is closed Scenario 1: Account has sufficient funds
Given the account balance is 100€ And the card is valid And the machine contains enough money When the Account Holder inserts the card And the Account Holder requests 20€ Then the ATM should dispense 20€ And the account balance should be 80€ And the card should be returned Scenario 2: ...
![Page 10: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/10.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Gherkin
• El lenguaje de cucumber• Lenguaje natural y comprensible• Lenguaje específico y definido• Lenguaje automatizable• Similar a YAML• Ficheros .feature
![Page 11: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/11.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Behat
• BDD para php• Inspirado por cucumber• Herramienta de línea de comandos• Disponible en varios idiomas• Más información en http://behat.org
![Page 12: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/12.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
#features/atm.featureFeature: Account Holder withdraws cash
As an Account HolderI want to withdraw cash from an ATMSo that I can get money when the bank is closed Scenario: Account has sufficient fundsGiven the account balance is 100€ And the card is valid And the machine contains enough money When the Account Holder inserts the card And the Account Holder requests 20€ Then the ATM should dispense 20€ And the account balance should be 80€ And the card should be returned Scenario: ...
![Page 13: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/13.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
$ behatFeature: Account Holder withdraws cashAs an Account HolderI want to withdraw cash from an ATMSo that I can get money when the bank is closed Scenario 1: Account has sufficient funds #features/atm.feature:7
Given the account balance is 100€ ...1 scenario ( 1 undefined)8 steps (8 undefined)You can implement undefined steps with these code snippets:
/** * @Given /^the account balance is (\d+)€$/ */public function theAccountBalanceIs($argument1){
throw new PendingException();}...
![Page 14: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/14.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
// features/bootstrap/FeatureContext.php<?php use Behat\Behat\Context\BehatContext, Behat\Behat\Exception\PendingException; class FeatureContext extends BehatContext{ /** * @Given /^the account balance is (\d+)€$/ */ public function theAccountBalanceIs ($argument1) { throw new PendingException (); }}
![Page 15: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/15.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
// features/bootstrap/FeatureContext.php<?php use Behat\Behat\Context\BehatContext, Behat\Behat\Exception\PendingException; class FeatureContext extends BehatContext{ /** * @Given /^(?:|the )account balance is (\d+)€$/ */ public function setAccountBalance ($balance) { $user = $this->getContainer()->getUser(); $account = $user->getAccount(); $account->setBalance($balance); }}
![Page 16: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/16.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
$ behatFeature: Account Holder withdraws cashAs an Account HolderI want to withdraw cash from an ATMSo that I can get money when the bank is closed Scenario 1: Account has sufficient funds #features/atm.feature:7Given the account balance is 100€ #featureContext::setAccountBalance()...1 scenario (1 pased)8 steps (8 passed)
![Page 17: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/17.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
/** * @Then /^(?:|The )account balance should be (\d+)€$/ */public function checkAccountBalance ($balance){ $user = $this->getContainer()->getUser();
$account = $user->getAccount();
if ($account->getBalance()!=$balance) { throw new Exception( 'Actual balance is '.$account->getBalance().
'€ instead of '.$balance.'€'; ); }}
![Page 18: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/18.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
$ behatFeature: Account Holder withdraws cashAs an Account HolderI want to withdraw cash from an ATMSo that I can get money when the bank is closed Scenario 1: Account has sufficient funds #features/atm.feature:7...And the account balance should be 80€ #featureContext::checkAccountBalance()
Actual balance is 100€ instead of 80€And the card should be returned#featureContext::isCardReturned() ...1 scenario (1 pased)8 steps (6 passed, 1 skipped, 1 failed)
![Page 19: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/19.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
require_once 'PHPUnit/Autoload.php';require_once 'PHPUnit/Framework/Assert/Functions.php';
...
/** * @Then /^(?:|the )account balance should be (\d+)€$/ */public function checkAccountBalance ($balance){ $user = $this->getContainer()->getUser(); $account = $user->getAccount(); assertEquals($account->getBalance(), $balance);}
![Page 20: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/20.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
...
Scenario: Account has insufficient funds
Given the account balance is 10€ And the card is valid And the machine contains enough money When the Account Holder inserts the card And the Account Holder requests 20€ Then the ATM should not dispense any money
And the ATM should print "Insuficient funds" And the account balance should be 10€ And the card should be returned Scenario: ...
![Page 21: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/21.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Then the account balance should be 20€/** * @Then /^the account balance should be (\d+)€$/ */
Then the ATM should print "Insuficient funds" /** * @Then /^the ATM should print "([^"]*)"$/ */
![Page 22: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/22.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Scenario: ...Given the following users exist: | name | email | phone | | Aslak | [email protected] | 123 | | Joe | [email protected] | 234 | | Bryan | [email protected] | 456 |
/*** @Given /the following users exist:/*/public function insertUsers(TableNode $table){ $hash = $table->getHash(); foreach ($hash as $row) { $user = new User($row['name'], $row['email'], $row['phone']); $this->database->insert($user); }}
![Page 23: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/23.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Scenario: Eat 5 out of 12 Given there are 12 cucumbers When I eat 5 cucumbers Then I should have 7 cucumbers
Scenario: Eat 5 out of 20 Given there are 20 cucumbers When I eat 5 cucumbers Then I should have 15 cucumbers
Scenario: Eat 5 out of 5 Given there are 5 cucumbers When I eat 5 cucumbers Then I should have 0 cucumbers
![Page 24: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/24.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Scenario Outline: Eat cucumbers Given there are <start> cucumbers When I eat <eat> cucumbers Then I should have <left> cucumbers
Examples: | start | eat | left | | 12 | 5 | 7 | | 20 | 5 | 15 | | 5 | 5 | 0 |
![Page 25: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/25.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Background:Given the following users exist: | name | email | phone | | Aslak | [email protected] | 123 | | Joe | [email protected] | 234 | | Bryan | [email protected] | 456 |
Scenario:...
Scenario:...
Hooks: http://docs.behat.org/guides/3.hooks.html
![Page 26: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/26.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
/** * @Then /^there should be no money in the account$/ */public function checkEmptyAccount (){ return new Then('the account balance should be 0€');}
/** * @When /^the user eats and sleeps$/ */public function userEatsAndSleeps (){ return array( new When("the user eats"), new When("the user sleeps"), );}
![Page 27: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/27.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//deps[gherkin] git=https://github.com/Behat/Gherkin.git target=/behat/gherkin
[behat] git=https://github.com/Behat/Behat.git target=/behat/behat
[BehatBundle] git=https://github.com/Behat/BehatBundle.git target=/bundles/Behat/BehatBundle
Behat en Symfony2: BehatBundle
![Page 28: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/28.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//app/autoload.php$loader->registerNamespaces(array( // .. 'Behat\Gherkin' => __DIR__.'/../vendor/behat/gherkin/src', 'Behat\Behat' => __DIR__.'/../vendor/behat/behat/src', 'Behat\BehatBundle' => __DIR__.'/../vendor/bundles',));//app/AppKernel.phppublic function registerBundles(){ // .. if ('test' === $this->getEnvironment()) { $bundles[] = new Behat\BehatBundle\BehatBundle(); }}
![Page 29: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/29.jpg)
+Acme+DemoBundle
+...+Features
+Context-FeatureContext.php
+...
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
$ app/console -e=test behat --init @AcmeDemoBundle
![Page 30: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/30.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//Acme/DemoBundle/Features/Context/FeatureContext.php<?phpnamespace Acme\DemoBundle\Features\Context; use Behat\BehatBundle\Context\BehatContext; class FeatureContext extends BehatContext{ /** * @Given /I have a product "([^"]*)"/ */ public function insertProduct($name) { $em = $this->getContainer()->get('doctrine') ->getEntityManager(); $product = new \Acme\DemoBundle\Entity\Product(); $product->setName($name); $em->persist($product); $em->flush(); }}
![Page 31: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/31.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
$ app/console –e=test behat @AcmeDemoBundle
BDD en Symfony2: ejecutar tests
![Page 32: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/32.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Pruebas de funcionalidad web: Mink
• Librería php integrada con behat• Permite usar distintos Browser emulators• Controlar el Navegador • Recorrer la Página• Manipular la Página• Simular la interacción del Usuario• Interface común para todos los emuladores
![Page 33: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/33.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Tipos de Browser emulators:
• Emuladores Headless Browsers• Symfony Web Client• Goutte
• Emuladores Browser controllers• Selenium• Sahi
• Mixtos: Zombie.js
![Page 34: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/34.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
// iniciar driver:$driver = new \Behat\Mink\Driver\GoutteDriver();
// iniciar sesión:$session = new \Behat\Mink\Session($driver);
// arrancar sesión:$session->start();
// abrir una página en el navegador:$session->visit('http://my_project.com/some_page.php');
// obtener el código de respuesta:echo $session->getStatusCode();
// obtener el contenido de la página:echo $session->getPage()->getContent();
Controlar el Navegador
![Page 35: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/35.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
// utilizar la historia del navegador:$session->reload();$session->back();$session->forward();
// evaluar expresión Javascript:echo $session->evaluateScript( "(function(){ return 'something from browser'; })()");
// obtener los headers:print_r($session->getResponseHeaders()); // guardar cookie:$session->setCookie('cookie name', 'value'); // obtener cookie:echo $session->getCookie('cookie name');
![Page 36: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/36.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//xpath selector$handler = new \Behat\Mink\Selector\SelectorsHandler();$xpath = $handler->selectorToXpath('xpath', '//html'); //css selector$selector = new \Behat\Mink\Selector\CssSelector();$xpath = $selector->translateToXPath('#ID'); //named selectors$selector = new \Behat\Mink\Selector\NamedSelector();$xpath = $selector->translateToXPath( array('field', 'id|name|value|label')); //named selectors: link, button, content, select, checkbox//radio, file, optgroup, option, table
Recorrer la Página: selectors
![Page 37: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/37.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//obtengo la página$page = $session->getPage(); //encuentro un elemento$element = $page->find('xpath', '//body'); //encuentro todos los elementos$elementsByCss = $page->findAll('css', '.classname'); //encuentro un elemento por su Id$element = $page->findById('ID'); //encuentro elementos con named selectors$link = $page->findLink('href');$button = $page->findButton('name');$field = $page->findField('id');
Recorrer la Página: obtener elementos
![Page 38: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/38.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//obtengo un elemento$el = $page->find('css', '.something'); // obtengo el nombre del tag:echo $el->getTagName(); // compruebo si tiene un atributo:$el->hasAttribute('href'); // obtengo un atributo:echo $el->getAttribute('href'); //obtengo el texto$plainText = $el->getText(); //obtengo el html$html = $el->getHtml();
Manipular la Página: Node Elements
![Page 39: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/39.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
// marcar/desmarcar checkbox:if ($el->isChecked()) { $el->uncheck();}$el->check(); // elegir option en select:$el->selectOption('optin value'); // añadir un fichero: $el->attachFile('/path/to/file'); // obtener el valor:echo $el->getValue(); // poner un valor:$el->setValue('some val');
Manipular la Página: Form fields
![Page 40: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/40.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
// pulsar un botón:$el->press(); //simular el ratón$el->click();$el->doubleClick();$el->rightClick();$el->mouseOver();$el->focus();$el->blur(); //Hacer drag'n'drop$el1->dragTo($el2);
Simular la interacción del Usuario
![Page 41: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/41.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
// features/bootstrap/FeatureContext.php
use Behat\Mink\Behat\Context\MinkContext; class FeatureContext extends MinkContext{ /** * @Then /^I press the submit button$/ */ public function PressSubmitButton() { $page = $this->getSession()->getPage(); $button = $page->findButton('submit'); $button->press(); }}
Integración con Behat: MinkContext
![Page 42: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/42.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Given I am on "URL" When I go to "url" When I reload the page When I move backward one page When I move forward one page When I press "button" When I follow "link" When I fill in "field" with "value" When I fill in "value" for "field" When I fill in the following: When I select "option" from "select" When I additionally select "option" from "select" When I check "option" When I uncheck "option" When I attach the file "path" to "field"
Steps predefinidos: Given/When
![Page 43: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/43.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Then I should be on "page"Then the url should match "pattern"Then the response status code should be "code"Then the response status code should not be "code"Then I should see "text"Then I should not see "text"Then I should see "text" in the "element" elementThen the "element" element should contain "value"Then I should see an "element" elementThen I should not see an "element" elementThen the "field" field should contain "value"Then the "field" field should not contain "value"Then the "checkbox" checkbox should be checkedThen the "checkbox" checkbox should not be checkedThen I should see "num" "element" elementsThen print last response
Steps predefinidos: Then
![Page 44: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/44.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
# features/search.featureFeature: Search In order to see a word definition As a website user I need to be able to search for a word
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"
Scenario: Searching for a page that does NOT exist Given I am on "/wiki/Main_Page" When I fill in "search" with "Glory Driven Development" And I press "searchButton" Then I should see “No results found"
![Page 45: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/45.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
/** * @Given /^I am on the main page$/ */public function goToMainPage(){ return new Given('I am on "/wiki/Main_Page"');}
/** * @Then /^I press the search button$/ */public function pressSearchButton(){ return new Then('I press "searchButton"');}
![Page 46: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/46.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//deps[mink] git=https://github.com/Behat/Mink.git target=/behat/mink
[MinkBundle] git=https://github.com/Behat/MinkBundle.git target=/bundles/Behat/MinkBundle
Mink en Symfony2: MinkBundle
![Page 47: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/47.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//app/autoload.php$loader->registerNamespaces(array( // .. 'Behat\Mink' => __DIR__.'/../vendor/behat/mink/src', 'Behat\MinkBundle' => __DIR__.'/../vendor/bundles',));
//app/AppKernel.phppublic function registerBundles(){ // .. if ('test' === $this->getEnvironment()) { $bundles[] = new Behat\BehatBundle\BehatBundle(); $bundles[] = new Behat\MinkBundle\MinkBundle(); }}
![Page 48: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/48.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
#app/config/config_test.ymlmink: base_url: http://localhost/app_test.php browser_name: chrome goutte: ~ sahi: ~ zombie: ~
![Page 49: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/49.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//web/app_test.php if (!in_array(@$_SERVER['REMOTE_ADDR'], array( '127.0.0.1', '::1',))) { header('HTTP/1.0 403 Forbidden'); exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');} require_once __DIR__.'/../app/bootstrap.php.cache';require_once __DIR__.'/../app/AppKernel.php'; use Symfony\Component\HttpFoundation\Request; $kernel = new AppKernel('test', true);$kernel->loadClassCache();$kernel->handle(Request::createFromGlobals())->send();
![Page 50: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/50.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
namespace Acme\DemoBundle\Features\Context; use Behat\MinkBundle\Context\MinkContext; class FeatureContext extends MinkContext{ /** * @When /^I go to the user account page$/ */ public function showUserAccount() { $user = $this->getContainer()->get('security.context') ->getToken()->getUser(); $session = $this->getSession(); $session->visit('/account/'. $user->getSlug()); }}
![Page 51: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/51.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
# symfony driver (default)@mink:symfonyScenario: ...
# goutte driver@mink:goutteScenario: ...
# sahi driver@mink:sahi o @javascriptScenario: ...
# zombie.js driver@mink:zombieScenario: ...
Qué Driver usar
![Page 52: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/52.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
app/console -e=test behat -f pretty,junit --out ,. @AcmeDemoBundle
Trucos: Salida de Behat
![Page 53: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/53.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
app/console -e=test behat --rerun="re.run" @AcmeDemoBundle
Trucos: repetir Tests
![Page 54: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/54.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
BDD vs TDD
• BDD es TDD
BDD vs UnitTesting• Unit testing comprueba unidades• BDD comprueba funcionalidad
![Page 55: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/55.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
Stop Press!!! Behat 2.4
• BehatBundle y MinkBundle deprecated• Usar MinkExtension y Symfony2Extension• Más info en http://behat.org
![Page 56: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/56.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
//deps[mink] git=https://github.com/Behat/Mink.git target=/behat/mink version=v1.3.3
[gherkin] git=https://github.com/Behat/Gherkin.git target=/behat/gherkin version=v2.1.1
[behat] git=https://github.com/Behat/Behat.git target=/behat/behat version=v2.3.5
![Page 57: BDD con Behat y Mink en Symfony2](https://reader036.fdocumento.com/reader036/viewer/2022081414/54c69ae94a795911528b458b/html5/thumbnails/57.jpg)
Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados
¡¡Gracias!!
• [email protected]• @carlos_granados• http://es.linkedin.com/in/carlosgranados
¿Preguntas?