bdd con behat y mink en symfony2

Post on 27-Jan-2015

120 Views

Category:

Economy & Finance

7 Downloads

Preview:

Click to see full reader

DESCRIPTION

Desarrollo basado en funcionalidad (BDD) en el framework Symfony2 con las herramientas Behat y Mink

TRANSCRIPT

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

Carlos Granados

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

• 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

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…)

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: ...

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

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

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: ...

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();}...

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 ();    }}

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);    }}

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)

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.'€';        );    }}

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)

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);}

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: ...

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 "([^"]*)"$/ */

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

Scenario: ...Given the following users exist: | name | email | phone | | Aslak | aslak@email.com | 123 | | Joe | joe@email.com | 234 | | Bryan | bryan@email.org | 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);    }}

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

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 |

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

Background:Given the following users exist: | name | email | phone | | Aslak | aslak@email.com | 123 | | Joe | joe@email.com | 234 | | Bryan | bryan@email.org | 456 |

Scenario:...

Scenario:...

Hooks: http://docs.behat.org/guides/3.hooks.html

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"),    );}

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

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();    }}

+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

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();    }}

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

$ app/console –e=test behat @AcmeDemoBundle

BDD en Symfony2: ejecutar tests

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

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

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

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');

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

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

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

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

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

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

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

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

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"

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"');}

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

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();    }}

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: ~

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();

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()); }}

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

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

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

app/console -e=test behat --rerun="re.run" @AcmeDemoBundle

Trucos: repetir Tests

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

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

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

Desarrollo guiado por comportamiento (BDD)con Symfony2, Behat y Mink – Carlos Granados

¡¡Gracias!!

• carlos@planetapluton.com• @carlos_granados• http://es.linkedin.com/in/carlosgranados

¿Preguntas?

top related