Codeception 获取测试所在的环境 运行
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
访问该信息。正如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.
所以在你的情况下它应该是这样的:
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);
}
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
访问该信息。正如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.
所以在你的情况下它应该是这样的:
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);
}