0

I just want to access the screen_shots_path parameter from FeatureContext.php file but writing $this->getMinkParameter('screen_shots_path'); doesn't work?

Anyone know how to do it?

Thanks in advance

I checked this one but the class extends BehatContext and mine extends MinkContext so I giot confused how to apply it mine.

sport/behat.yml

default:
    context:
        class: 'FeatureContext'
    extensions:
        Behat\Symfony2Extension\Extension:
            mink_driver: true
            kernel:
                env: test
                debug: true
        Behat\MinkExtension\Extension:
            base_url: 'http://localhost/local/sport/web/app_test.php/'
            files_path: 'dummy/'
            screen_shots_path: 'build/behat/'
            browser_name: 'chrome'
            goutte: ~
            selenium2: ~
    paths:
        features: 'src/Football/TeamBundle/Features'
        bootstrap: %behat.paths.features%/Context

sport/src/Football/TeamBundle/Features/Context/FeatureContext.php

namespace Football\TeamBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Mink\Driver\Selenium2Driver;

class FeatureContext extends MinkContext
{
    /**
     * Take screen-shot when step fails.
     * Works only with Selenium2Driver.
     *
     * @AfterStep
     * @param $event
     * @throws \Behat\Mink\Exception\UnsupportedDriverActionException
     */
    public function takeScreenshotAfterFailedStep($event)
    {
        if (4 === $event->getResult()) {
            $driver = $this->getSession()->getDriver();

            if (! ($driver instanceof Selenium2Driver)) {
                throw new UnsupportedDriverActionException(
                    'Taking screen-shots is not supported by %s, use Selenium2Driver instead.',
                    $driver
                );

                return;
            }

            #$directory = 'build/behat';
            $directory = $this->getMinkParameter('screen_shots_path');

            if (! is_dir($directory)) {
                mkdir($directory, 0777, true);
            }

            $filename = sprintf(
                '%s_%s_%s.%s',
                $this->getMinkParameter('browser_name'),
                date('Y-m-d') . '_' . date('H:i:s'),
                uniqid('', true),
                'png'
            );

            file_put_contents($directory . '/' . $filename, $driver->getScreenshot());
        }
    }
}
Community
  • 1
  • 1
BentCoder
  • 12,257
  • 22
  • 93
  • 165

1 Answers1

1

I know you've tagged it as a Symfony question, there might be something on that side that affects it, but from the code it doesn't seem to be, so the problem is probably in the following.

Assuming you are using Mink Extension 1.x and not 2.x, screen_shots_path parameter is not on the list of the supported ones. In fact 2.x doesn't support it either, but it would throw an exception right away when it finds something illegal in the config. Perhaps 1.x doesn't do that. You can see the supported parameters here.

The most likely reason, screen_shots_path simply gets ignored when the config is normalised and hence getMinkParameter('screen_shots_path') doesn't return anything. I bet if you try the same with files_path you'll see dummy/.

If you want to keep the configuration in your behat.yml your best chances would be to pass them directly to the context, see documentation.

# behat.yml
default:
    context:
        class: FeatureContext
        parameters:
            screen_shots_path: 'build/behat/'

This will be passed to the constructor where you can initialise a local parameter. Alternatively you can use the static parameter and make it accessible through other contexts.

class FeatureContext extends MinkContext
{
    protected $screenShotsPath;

    public function __construct($parameters) 
    {
        $this->screenShotsPath = isset($parameters['screen_shots_path']) ? $parameters['screen_shots_path'] : 'some/default/path';
    }

    public function takeScreenshotAfterFailedStep($event)
    {
        $directory = $this->screenShotsPath;
    }
}
Ian Bytchek
  • 8,804
  • 6
  • 46
  • 72
  • I'll try it when I get back home but from my understanding is this what I should do in my FeatureContext? : `public function __construct(array $parameters) { $this-directory = $this->getMinkParameter('screen_shots_path'); }` – BentCoder Sep 11 '14 at 10:38
  • Lovely. I'll check it tonight and let you know. Thanks for now. – BentCoder Sep 11 '14 at 10:57
  • I sense a british accent :D – Ian Bytchek Sep 11 '14 at 11:35
  • 1
    Excellent. Worked like a charm. Looks like I can stuff parameters with anything I want. This will be very useful. Thank you very much again. – BentCoder Sep 11 '14 at 20:35
  • Sorry Ian, I have one more issue which is [defined here](http://stackoverflow.com/questions/25796825/bin-behat-footballteambundle-is-fine-but-bin-phing-is-not-for-running-featureco). Would you please have a look at it as well. Thanks – BentCoder Sep 11 '14 at 20:58