Search code examples
phpsymfonyseleniumbehatmink

Behat not call extended mink methods


I was quoth myself "I must learn how to bdd?" And i was try Symfony2/Behat/Mink combination.

Well, i was writing code as writing in tutorials. But every behat versions in documents/tutorials lower than version 3. I want learn behat3. I'm going to behat official page, and reading.

Everything ok, but somehow, behat cli app can't call extended mink class methods. I will put my codes to below. How i run behat3 app with mink's methods.

features/bootstrap/FeatureContext.php

<?php

use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

/**
 * Defines application features from the specific context.
 */
class FeatureContext  extends \Behat\MinkExtension\Context\MinkContext implements Context, SnippetAcceptingContext
{

}

features/home.feature

Feature: Home
  I want to see homepage
  As a anonym user
  Scenario:
    Given |I am on |the homepage
    Then |I should see text matching "Sektör"

behat.yml

default:
  extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
      sessions:
        default:
          symfony2: ~
        selenium2:
          selenium2: ~
      base_url: http://site.dev

composer.json

    "behat/behat": "~3.0",
    "behat/mink-extension": "~2.0@dev",
    "behat/mink-goutte-driver": "~1.0",
    "behat/mink-selenium2-driver": "~1.1",
    "behat/symfony2-extension": "*"

Result:

Feature: Home
  I want to see homepage
  As a anonym user

  Scenario:                                   # features/home.feature:4
    Given |I am on |the homepage
    Then |I should see text matching "Sektör"

1 scenario (1 undefined)
2 steps (2 undefined)
0m0.12s (25.80Mb)

--- FeatureContext has missing steps. Define them with these snippets:

    /**
     * @Given |I am on |the homepage
     */
    public function iAmOnTheHomepage()
    {
        throw new PendingException();
    }

    /**
     * @Then |I should see text matching :arg1
     */
    public function iShouldSeeTextMatching($arg1)
    {
        throw new PendingException();
    }

Solution

  • This will you kick start with version 3. Found whole example here and there are more examples in here, inc. version 2.

    composer.json

    {
        "require-dev": {
            "behat/behat" : "3.0.15",
            "behat/symfony2-extension" : "2.0.0",
            "behat/mink": "1.6.1",
            "behat/mink-extension": "2.0.1",
            "behat/mink-browserkit-driver": "1.2.0",
            "behat/mink-goutte-driver": "1.1.0",
            "behat/mink-selenium2-driver": "1.2.0"
        }
    }
    

    behat.yml

    default:
        extensions:
            Behat\Symfony2Extension: ~
            Behat\MinkExtension:
                base_url: http://football.local/app_test.php
                browser_name: firefox
                sessions:
                    goutte: # fast, CLI, browser, no javascript support
                        goutte: ~
                    selenium2: # fast, CLI, opens up a browser
                        selenium2: ~
                    symfony2: # very fast, CLI, no browser
                        symfony2: ~
        suites:
            backend:
                type: symfony_bundle
                bundle: ApplicationBackendBundle
                mink_session: symfony2
                contexts:
                    - Application\BackendBundle\Features\Context\FeatureContext:
                        param1: hello
                        param2: world
    

    FeatureContext.php

    namespace Application\BackendBundle\Features\Context;
    
    use Behat\MinkExtension\Context\MinkContext;
    use Behat\Symfony2Extension\Context\KernelAwareContext;
    use Symfony\Component\HttpKernel\KernelInterface;
    
    class FeatureContext extends MinkContext implements KernelAwareContext
    {
        private $kernel;
        private $param1;
        private $param2;
    
        public function __construct($param1, $param2)
        {
            $this->param1 = $param1;
            $this->param2 = $param2;
        }
    
        public function setKernel(KernelInterface $kernelInterface)
        {
            $this->kernel = $kernelInterface;
        }
    
        /**
         * @Given /^I can access service container$/
         */
        public function iCanAccessServiceContainer()
        {
            $container = $this->kernel->getContainer();
            echo $container->getParameter('secret');
        }
    }
    

    Example feature

    # Backend
    Feature: Dummy feature
    
      Scenario: Home url
        Given I am on "/backend"
        Then I should see "Welcome to Application backend!"
        And I can access service container
    
    # Frontend
    Feature: Dummy feature
    
      Scenario: Home url
        Given I am on "/"
        Then I should see "Welcome to Application frontend!"
        And I can access service container
    

    Run

    bin/behat --suite=backend

    Feature: Dummy feature
    
      Scenario: Home url
        Given I am on "/backend"
        Then I should see "Welcome to Application backend!"
        And I can access service container
          │ ThisTokenIsNotSoSecretChangeIt
    
    1 scenario (1 passed)
    3 steps (3 passed)
    0m0.98s (28.17Mb)