Behatch / contexts

Behat extension with most custom helper steps
Other
393 stars 203 forks source link

How to access RestContext from my custom context? #272

Closed tsantos84 closed 4 years ago

tsantos84 commented 5 years ago

I need to write a custom step on my custom context to add a header in the request. Is it possible? I've followed the official way to do so without success.

http://behat.org/en/latest/cookbooks/accessing_contexts_from_each_other.html

The error says that I can't call the private getContext method from InitializedSymfonyExtensionEnvironment class.

My code:

/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope)
{
    $environment = $scope->getEnvironment();
    $this->restContext = $environment->getContext(RestContext::class);
}

Any idea?

OskarStark commented 5 years ago

You can try it by using gatherContexts() method

I am using it like this: https://github.com/OskarStark/SonataAdminBehatContext/blob/master/src/Context/SonataAdminContext.php

tsantos84 commented 5 years ago

I've tried this snippet but I still getting the calling private method exception.

OskarStark commented 5 years ago

Can you share your behat.yaml please?

PWalkow commented 4 years ago

@tsantos84 have you tried something like that?

use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class YourContext implements Context {

(...)

    /**
     * @BeforeScenario
     */
    public function gatherContext(BeforeScenarioScope $beforeSuiteScope)
    {
        $env = $beforeSuiteScope->getEnvironment();
        $this->restContext = $env->getContext(RestContext::class);
    }
tsantos84 commented 4 years ago

Hi guys. Sorry for the very long reply. I think my initial issue were with some symfony/behat/behatch version. I've just make a fresh install and now I can access Behatch context from my custom one.