Путаница между BehatContext и MinkContext

Я пытаюсь сделать BDD в своем проекте Symfony 2.3 и, похоже, борюсь с некоторыми несоответствиями.

В зависимости от того, использую ли я BehatContext или MinkContext в качестве базового класса для класса FeatureContext, я получаю разные результаты.

Если я использую:

class FeatureContext extends BehatContext 

всё хорошо. однако, если я использую:

class FeatureContext extends MinkContext

Я получаю ошибки, из-за которых кажется, что MinkContext больше не любит мое регулярное выражение, которое система генерирует сама. Не могли бы вы помочь мне понять:

FFFFFFF

(::) failed steps (::)

01. Ambiguous match of "I am on homepage":
    to `/^I am on homepage$/` from Main\ReferralCaptureBundle\Features\Context\FeatureContext::iAmOnHomepage()
    to `/^(?:|I )am on (?:|the )homepage$/` from Behat\MinkExtension\Context\MinkContext::iAmOnHomepage()
    In step `Given I am on homepage'.
    From scenario `Successful registration when user provides all the required info'. # src/Main/ReferralCaptureBundle/Features/registration.feature:12
    Of feature `registration'.                                                        # src/Main/ReferralCaptureBundle/Features/registration.feature

02. Ambiguous match of "I follow "sign up"":
    to `/^I follow "([^"]*)"$/` from Main\ReferralCaptureBundle\Features\Context\FeatureContext::iFollow()
    to `/^(?:|I )follow "(?P<link>(?:[^"]|\\")*)"$/` from Behat\MinkExtension\Context\MinkContext::clickLink()
    In step `And I follow "sign up"'.
    From scenario `Successful registration when user provides all the required info'. # src/Main/ReferralCaptureBundle/Features/registration.feature:12
    Of feature `registration'.                                                        # src/Main/ReferralCaptureBundle/Features/registration.feature

03. Ambiguous match of "I fill in "username" with "[email protected]"":
    to `/^I fill in "([^"]*)" with "([^"]*)"$/` from Main\ReferralCaptureBundle\Features\Context\FeatureContext::iFillInWith()
    to `/^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/` from Behat\MinkExtension\Context\MinkContext::fillField()
    In step `When I fill in "username" with "[email protected]"'.
    From scenario `Successful registration when user provides all the required info'. # src/Main/ReferralCaptureBundle/Features/registration.feature:12
    Of feature `registration'.                                                        # src/Main/ReferralCaptureBundle/Features/registration.feature

04. Ambiguous match of "I fill in "password" with "password123"":
    to `/^I fill in "([^"]*)" with "([^"]*)"$/` from Main\ReferralCaptureBundle\Features\Context\FeatureContext::iFillInWith()
    to `/^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/` from Behat\MinkExtension\Context\MinkContext::fillField()
    In step `And I fill in "password" with "password123"'.
    From scenario `Successful registration when user provides all the required info'. # src/Main/ReferralCaptureBundle/Features/registration.feature:12
    Of feature `registration'.                                                        # src/Main/ReferralCaptureBundle/Features/registration.feature

05. Ambiguous match of "I press "register"":
    to `/^I press "([^"]*)"$/` from Main\ReferralCaptureBundle\Features\Context\FeatureContext::iPress()
    to `/^(?:|I )press "(?P<button>(?:[^"]|\\")*)"$/` from Behat\MinkExtension\Context\MinkContext::pressButton()
    In step `And I press "register"'.
    From scenario `Successful registration when user provides all the required info'. # src/Main/ReferralCaptureBundle/Features/registration.feature:12
    Of feature `registration'.                                                        # src/Main/ReferralCaptureBundle/Features/registration.feature

06. Ambiguous match of "I should see "You have successfully registered"":
    to `/^I should see "([^"]*)"$/` from Main\ReferralCaptureBundle\Features\Context\FeatureContext::iShouldSee()
    to `/^(?:|I )should see "(?P<text>(?:[^"]|\\")*)"$/` from Behat\MinkExtension\Context\MinkContext::assertPageContainsText()
    In step `Then I should see "You have successfully registered"'.
    From scenario `Successful registration when user provides all the required info'. # src/Main/ReferralCaptureBundle/Features/registration.feature:12
    Of feature `registration'.                                                        # src/Main/ReferralCaptureBundle/Features/registration.feature

07. Ambiguous match of "I should be on homepage":
    to `/^I should be on homepage$/` from Main\ReferralCaptureBundle\Features\Context\FeatureContext::iShouldBeOnHomepage()
    to `/^(?:|I )should be on (?:|the )homepage$/` from Behat\MinkExtension\Context\MinkContext::assertHomepage()
    In step `And I should be on homepage'.
    From scenario `Successful registration when user provides all the required info'. # src/Main/ReferralCaptureBundle/Features/registration.feature:12
    Of feature `registration'.                                                        # src/Main/ReferralCaptureBundle/Features/registration.feature

1 scenario (1 failed)
7 steps (7 failed)

FeatureContext.php

<?php

namespace Main\ReferralCaptureBundle\Features\Context;

use Main\ReferralCaptureBundle\Features\Context\FeatureContext;

use Symfony\Component\HttpKernel\KernelInterface;
use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Behat\MinkExtension\Context\MinkContext;
use Behat\MinkExtension\Context\RawMinkContext;

use Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

use Goutte\Client;

//
// Require 3rd-party libraries here:
//
   require_once 'PHPUnit/Autoload.php';
   require_once 'PHPUnit/Framework/Assert/Functions.php';
//

/**
 * Feature context.
 */
class FeatureContext extends RawMinkContext //WHAT TO USE HERE!!!!!!
                  implements KernelAwareInterface
{
    private $kernel;
    private $parameters;

    /**
     * Initializes context with parameters from behat.yml.
     *
     * @param array $parameters
     */
    public function __construct(array $parameters)
    {
        $this->parameters = $parameters;
   //     $this->useContext('mink', new MinkContext);
    }

    /**
     * Sets HttpKernel instance.
     * This method will be automatically called by Symfony2Extension ContextInitializer.
     *
     * @param KernelInterface $kernel
     */
    public function setKernel(KernelInterface $kernel)
    {
        $this->kernel = $kernel;
    }

//
// Place your definition and hook methods here:
//
//    /**
//     * @Given /^I have done something with "([^"]*)"$/
//     */
//    public function iHaveDoneSomethingWith($argument)
//    {
//        $container = $this->kernel->getContainer();
//        $container->get('some_service')->doSomethingWith($argument);
//    }
//    
//    
//    
//

    /**
     * @Given /^I am on homepage$/
     */
    public function iAmOnHomepage()
    {
        $client = new Client();
        $crawler = $client->request('GET', 'http://local.referral.com/');

        $link = $crawler->selectLink('I am a Physician')->link();


       if (!count($link)>0)
       {
          throw new Exception("Home Page Not Loaded:\n");   

       }
    }

    /**
     * @Given /^I follow "([^"]*)"$/
     */
    public function iFollow($arg1)
    {
        throw new PendingException();
    }

    /**
     * @When /^I fill in "([^"]*)" with "([^"]*)"$/
     */
    public function iFillInWith($arg1, $arg2)
    {
        throw new PendingException();
    }

    /**
     * @Given /^I press "([^"]*)"$/
     */
    public function iPress($arg1)
    {
        throw new PendingException();
    }

    /**
     * @Then /^I should see "([^"]*)"$/
     */
    public function iShouldSee($arg1)
    {
        throw new PendingException();
    }

    /**
     * @Given /^I should be on homepage$/
     */
    public function iShouldBeOnHomepage()
    {
        throw new PendingException();
    }


}

behat.yml

default:
  formatter:
    name: progress
  extensions:
    Behat\Symfony2Extension\Extension:
      mink_driver: true
      kernel:
        env: test
        debug: true
    Behat\MinkExtension\Extension:
      goutte: ~
      base_url: 'http://local.mysite.com'
      default_session: symfony2

person milof    schedule 13.08.2013    source источник


Ответы (1)


Расширьте RawMinkContext, если вам нужен доступ к сеансу Mink (для автоматизации браузера).

Не используйте Goutte напрямую.

BehatContext — это базовый контекст, который будет использоваться по умолчанию для большинства ваших собственных контекстов, когда вам не нужен Mink.

MinkContext — это специальный контекст, дающий вам доступ к сеансу Mink (такой же, как RawMinkContext). Тем не менее, он также содержит некоторые основные определения шагов. Вот почему вы никогда не должны расширять его, а использовать как подконтекст. Вы сможете расширить его только один раз, так как определения шагов не могут быть продублированы.

Вопрос очень похож на ваш другой вопрос (посмотрите там пример использования Mink): Что не так с моим FeatureContext?

Использование Behat не означает, что вы следуете BDD. Чтобы узнать больше об этом, прочитайте следующие книги:

Чтобы прочитать об инструментах:

person Jakub Zalas    schedule 14.08.2013