0

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

Roma Kap
  • 517
  • 1
  • 8
  • 23

1 Answers1

1

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
Igor Lantushenko
  • 1,771
  • 10
  • 19
  • Thank you :) I solved this problem. It were muliple problems. My Project-SetUp was not correct. I have a quastion. When i say: browser_name: "firefox" ==> Selenium2-Server will use firefox Browser. But when i set capabilities on "browser" : "chrome" and some version, Selenium2-Server uses still Firefox. What is the use of capabilities??? – Roma Kap Jul 28 '16 at 12:56
  • Good question, I'm not a big expert in the area of capabilities. But as I know this arguments (`browser`, `version`) should be passed to the selenium driver. But also `browser_name` has bigger power than `browser`. In other words if `browser_name` not empty than `browser` will be ignored. Here is [Selenium2Factory](https://github.com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium2Factory.php#L41) which get `browser_name` parameter as `browser` – Igor Lantushenko Jul 28 '16 at 13:05
  • hhmm..... i have read, that Selenium2-Server takes firefox-browser per default, when "browser_name: 'chrome' " is missing. When i say: "browser_name: 'firefox' " and wihtin capabilities: { "browser": "chrome" } (or invert), Selenium2-Server takes still firefox ^^ either capabilities are useless or its something wrong.... – Roma Kap Jul 28 '16 at 13:15
  • Yeah, if `browser_name` is not set than `firefox` will be default, but when I want to run tests in chrome I just set `browser_name: chrome` and it works. And by the way of my answer was useful for you you can accept it or vote :) – Igor Lantushenko Jul 28 '16 at 13:32