Vladimir Bunchuk
Vladimir Bunchuk

Reputation: 103

Codeception - How to get current environment configuration in Helper class

Is it possible to get current environment config in Helper Class in Codeception?

Now I'm passing it as $env variable from Cest where I use this Helper.

class FavoritesCest
    {
        public function _before(AcceptanceTester $I)
        {
            $I->loggedInIntoFrontend(LoginPage::LOGIN, LoginPage::PASSWORD, $I->getScenario()->current('env'));
        }

    ...

    }

In Cest I use $I->getScenario()->current('env'), but in Helper I can't use Actor class to get environment this way.

// Helper Class
    class Frontend extends Acceptance
    {
        public function loggedInIntoFrontend($name, $password, $env)
        { ... }
    }

Has anyone encountered this?

Upvotes: 0

Views: 1436

Answers (1)

Alexander Kuntashov
Alexander Kuntashov

Reputation: 694

You can use the following approach to get current environment in Helper class:

// Helper Class
class Frontend extends Acceptance
{
    private $currentEnv = '';

    // This hook will be called before each scenario.
    public function _before(\Codeception\TestInterface $test)
    {
        $this->currentEnv = $test->getMetadata()->getCurrent('env');
    }

    public function loggedInIntoFrontend($name, $password)
    {  
        if ($this->currentEnv == 'my-env') {
            ... 
        }
    }
}

Upvotes: 1

Related Questions