ro ko ro ko - 20 days ago 8
PHP Question

Codeception get environment on which tests are running

I have acceptance.suite.yml which looks like this.

class_name: AcceptanceTester
modules:
enabled:
- \Helper\Acceptance
- WebDriver:
url: https://staging.needhelp.com
env:
firefox:
modules:
config:
qa_user: qauser1@gmail.com
WebDriver:
browser: 'firefox'
capabilities:
platform: Windows 7

chrome:
modules:
config:
qa_user: qauser2@gmail.com
WebDriver:
browser: 'chrome'
capabilities:
platform: Windows 8.1


And I run the test case as so:

$ codecept run acceptance UserCest.php --env firefox --env chrome


Now, I was wondering if there is a way to get env within test itself during run time.

class UserCest extends BaseAcceptance
{



public function login(AcceptanceTester $I)
{
$I->amOnPage("/");
$I->see('Sign In');
$env = $I->getConfig('env');
//something like this ?? which would return 'firefox' for the instance it is running as environment firefox.

$I->fillField($this->usernameField, $this->username);
$I->fillField($this->passwordField, $this->password);
}

Answer

You should be able to access that info via scenario. As it is said in the docs:

You can access \Codeception\Scenario in Cept and Cest formats. In Cept $scenario variable is availble by default, while in Cests you should receive it through dependency injection.

So in your case it should look something like this:

public function login(AcceptanceTester $I, \Codeception\Scenario $scenario)
{
    $I->amOnPage("/");
    $I->see('Sign In');

    if ($scenario->current('browser') == 'firefox') {
        //code to handle firefox
    }

    $I->fillField($this->usernameField, $this->username);
    $I->fillField($this->passwordField, $this->password);
}
Comments