How to inject Symfony param from parameters.yml into Behat 3 configuration?

柔情痞子 提交于 2020-01-04 06:46:48

问题


I need to set the base_url for Behat\MinkExtension.

Here is a part of my app/config/parameters.yml:

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

Here is a part of my 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"]

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
# ...

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?

Thanks.


回答1:


So. I found two solutions.

The first: use BEHAT_PARAMS to configure. for example:

Run in command line.

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

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.

The second:

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'));
    }
}

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.

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.



来源:https://stackoverflow.com/questions/40798687/how-to-inject-symfony-param-from-parameters-yml-into-behat-3-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!