doujie9252 2014-09-10 20:17 采纳率: 0%
浏览 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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化