Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion between BehatContext and MinkContext

I am trying to do BDD in my Symfony 2.3 project and appear to be struggling with some inconsistencies.

Depending on whether I use BehatContext or MinkContext as my base class for the FeatureContext class, I am getting different results.

If I use:

class FeatureContext extends BehatContext 

everything is fine. however if I use:

class FeatureContext extends MinkContext

I get errors which make it look like the MinkContext is not liking my regex no more which the system generate itself. Can you please help me understand:

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
like image 576
milof Avatar asked Dec 15 '22 07:12

milof


1 Answers

Extend the RawMinkContext if you need to access the Mink Session (for browser automation).

Don't use Goutte directly.

BehatContext is a basic context which would be a default choice for most of your own contexts, when you don't need Mink.

MinkContext is a specialized context giving you the access to the Mink session (same as the RawMinkContext). However, it also contains some basic step definitions. That's why you should never extend it, but rather use it as a subcontext. You'd be only able to extend it once, since the step definitions cannot be duplicated.

The question is very similar to your other question (look there for an example of Mink usage): What is wrong with my FeatureContext?

Using Behat doesn't mean you're following BDD. To learn more about it, read the following books:

  • Bridging the communication gap
  • Specification by example
  • The RSpec book

To read about the tools:

  • Mink: http://mink.behat.org/
  • MinkExtension: http://extensions.behat.org/mink/index.html
  • MinkContext class: https://github.com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/Context/MinkContext.php
  • RawMinkContextClass: https://github.com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/Context/RawMinkContext.php
like image 113
Jakub Zalas Avatar answered Mar 07 '23 19:03

Jakub Zalas