co欺骗获取运行测试的环境


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

我运行测试用例如下:

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

现在,我想知道是否有一种方法可以在运行时在测试本身中获得环境。

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

您应该能够通过scenario访问该信息。正如文档中所说:

您可以访问Cept和Cest格式的'Codeception'Scenario。在cest中,$scenario变量是默认可用的,而在cest中,你应该通过依赖注入来接收它。

在你的例子中应该是这样的:

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