Stafox Stafox - 4 days ago 7
YAML Question

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

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.

Answer

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.

Comments