Search code examples
phpbehatminkselenium-grid2

Behat3 MinkExtension could not be enabled


i want to achieve take a screenshot by using Behat, Mink and SeleniumGrid

But i get this errors:

Given I go to "mySite.org/private" # FeatureContext::visit() Mink instance has not been set on Mink context class. Have you enabled the Mink Extension? (RuntimeException) │ ╳ Mink instance has not been set on Mink context class. Have you enabled the Mink Extension? (RuntimeException) │ └─ @AfterStep # Feat

My behat.yml:

default:
extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
        base_url: http://integration.fvs.dev.intern.etecture.de/private-clients
        browser_name: 'firefox'
        selenium2:
            wd_host: http://hub.selenium.intern.etecture.de:4444/wd/hub
            capabilities: { "browser": "firefox", "version": "14"}
        goutte: ~
        sessions:
            goutte:
                goutte: ~
            selenium2:
                selenium2: ~
            symfony2:
                symfony2: ~
suites:
    backend:
        type: symfony_bundle
        mink_session: selenium2
        contexts:
            - app\features\bootstrap\FeatureContext:
                screen_shot_path: app/screenshot

my FeatureContext.php

class FeatureContext extends MinkContext
{
    private $screenShotPath;

    public function __construct()
    {
        $this->screenShotPath = "/app/screenshot";
    }

    /**
     * Take screen-shot when step fails. Works only with Selenium2Driver.
     *
     * @AfterStep
     * @param AfterStepScope $scope
     */
    public function takeScreenshotAfterFailedStep(AfterStepScope $scope)
    {
        if (99 === $scope->getTestResult()->getResultCode()) {
            $driver = $this->getSession()->getDriver();

            if (! $driver instanceof Selenium2Driver) {
                return;
            }

            if (! is_dir($this->screenShotPath)) {
                mkdir($this->screenShotPath, 0777, true);
            }

            $filename = sprintf(
                '%s_%s_%s.%s',
                $this->getMinkParameter('browser_name'),
                date('Ymd') . '-' . date('His'),
                uniqid('', true),
                'png'
            );

            $this->saveScreenshot($filename, $this->screenShotPath);
        }
    }

    /**
     * @Then /^I should see the css selector "([^"]*)"$/
     * @Then /^I should see the CSS selector "([^"]*)"$/
     */
    public function iShouldSeeTheCssSelector($css_selector) {
        $element = $this->getSession()->getPage()->find("css", $css_selector);
        if (empty($element)) {
            throw new \Exception(sprintf("The page '%s' does not contain the css selector '%s'", $this->getSession()->getCurrentUrl(), $css_selector));
        }
    }

    /**
     * @Then /^I should not see the css selector "([^"]*)"$/
     * @Then /^I should not see the CSS selector "([^"]*)"$/
     */
    public function iShouldNotSeeAElementWithCssSelector($css_selector) {
        $element = $this->getSession()->getPage()->find("css", $css_selector);

        if (empty($element)) {
            throw new \Exception(sprintf("The page '%s' contains the css selector '%s'", $this->getSession()->getCurrentUrl(), $css_selector));
        }
    }
}

Has anyone ideas? Thx


Solution

  • Probably looking into your examples the problem is in your behat.yml configuration file. default is a name of your profile and extensions key is a child of that profile. So each line below default profile should has addition 4 spaces.

    default:
        extensions:
            Behat\Symfony2Extension: ~
            Behat\MinkExtension:
            base_url: http://integration.fvs.dev.intern.etecture.de/private-clients
            browser_name: 'firefox'
            selenium2:
                wd_host: http://hub.selenium.intern.etecture.de:4444/wd/hub
                capabilities: { "browser": "firefox", "version": "14"}
            goutte: ~
            sessions:
                goutte:
                    goutte: ~
                selenium2:
                    selenium2: ~
                symfony2:
                    symfony2: ~
        suites:
            backend:
                type: symfony_bundle
                mink_session: selenium2
                contexts:
                    - app\features\bootstrap\FeatureContext:
                        screen_shot_path: app/screenshot