doujie9252 2014-09-10 20:17
浏览 30
已采纳

访问FeaturesContext中的behat.yml变量

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());
        }
    }
}
  • 写回答

1条回答 默认 最新

  • douci6541 2014-09-11 10:10
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?