如何将来自parameters.yml的Symfony参数注入到Behat 3配置中? [英] How to inject Symfony param from parameters.yml into Behat 3 configuration?

查看:84
本文介绍了如何将来自parameters.yml的Symfony参数注入到Behat 3配置中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将base_url设置为Behat\MinkExtension.

这是我的 app/config/parameters.yml 的一部分:

parameters:
    # ...
    behat_base_url: http://my-app.local/app_test.php
    # ...

这是我的 behat.yml 的一部分:

frontend:
    gherkin:
        filters:
            tags: "@frontend"
    suites:
        javascript:
            mink_session: default
            mink_javascript_session: selenium2
            contexts:
                - FeatureContext: ~

    extensions:
        Behat\Symfony2Extension: ~
        Behat\MinkExtension:
            base_url: http://my-app.local/app_test.php
            sessions:
                default:
                    symfony2: ~
                selenium2:
                    selenium2:
                        browser: chrome
                        capabilities:
                            extra_capabilities:
                                chromeOptions:
                                    args: ["--start-maximized"]

我可以做这样的事情(以提供对parameters.behat_base_url的引用)

Can I do something like this (to provide reference to parameters.behat_base_url)

imports:
    - app/config/parameters.yml

# ...
extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
        base_url: *parameters.behat_base_url # reference to value from parameters.yml
# ...

或者可能存在另一种权利和真实"方式,可以从parameters.yml或其他位置设置base_url.因此,任何开发人员都将拥有自己的测试环境配置.

Or maybe exist another right and "true" way how I can set base_url from parameters.yml or another place. So, any developer will have his own configuration for test env.

您能否告诉您如何使用其他测试环境开发人员的配置解决问题?

Could you tell how you solve the issue with different test environment developer's configuration?

谢谢.

推荐答案

所以.我找到了两种解决方案.

So. I found two solutions.

第一个:使用BEHAT_PARAMS进行配置.例如:

The first: use BEHAT_PARAMS to configure. for example:

在命令行中运行.

export BEHAT_PARAMS='{"extensions":{"Behat\\MinkExtension":{"base_url":"http://my-app.local/app_test.php"}}}'

注意:在这种情况下,它指定默认参数,因此您不应在behat.yml中定义这些参数(以防它们具有更高的优先级并被覆盖).您可以在类似的文档中阅读有关它的内容

Note: in that case it specify default params, so you should NOT define those parameters in behat.yml (becase they have higher priority and will be overwritten). You can read about it in behat docs

但是这种情况不适合我.我不希望开发人员每次都配置环境.

But this case is not for me. I don't want developer each time configure the environment.

第二个:

我在FeatureContext中指定了以下方法:

I specified following method in my FeatureContext:

/** @BeforeScenario */
public function beforeScenario(\Behat\Behat\Hook\Scope\BeforeScenarioScope $scenario)
{
    if ($scenario->getSuite()->getName() == 'javascript') {
        $this->setMinkParameter('base_url', $this->getContainer()->getParameter('behat_base_url'));
    }
}

因此,在每种情况之前,我都要检查套件是否为javascript并设置为base_url.我使用容器从parameters.yml获得的值. 也许这是个肮脏"的把戏,但是对我来说很好.

So, before each scenario I check that the suite is javascript and set base_url. The value I get from parameters.yml using container. Maybe it's a little bit "dirty" trick, but it works fine for me.

从内核获取容器.要注入内核,您应该实现Behat\Symfony2Extension\Context\KernelAwareContext并添加方法setKernel():

Get container you can from kernel. To inject kernel you should implement Behat\Symfony2Extension\Context\KernelAwareContext and add method setKernel():

public function setKernel(KernelInterface $kernel)
{
    $this->kernel = $kernel;
}

所以,我选择第二种方式.

So, I choose the second way.

如果有人知道更优雅的解决方案,请告诉我.

If somebody know more elegant solution let me know.

这篇关于如何将来自parameters.yml的Symfony参数注入到Behat 3配置中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆