我正在尝试为应用程序编写一些Behat测试,我需要分离上下文,以便我可以在不同的其他上下文中使用一些核心元素,例如登录用户步骤。
behat.yml
suites:
common:
type: symfony_bundle
bundle: CommonBundle
mink_session: symfony2
mink_javascript_session: selenium2
autoload:
'CommonContext': %paths.base%/src/xxx/CommonBundle/Features/Context
contexts: [ xxx\CommonBundle\Features\Context\CoreContext ]
user:
type: symfony_bundle
bundle: UserBundle
mink_session: symfony2
mink_javascript_session: selenium2
autoload:
'UserContext': %paths.base%/src/xxx/UserBundle/Features/Context
contexts:
- Behat\MinkExtension\Context\MinkContext
- xxx\CommonBundle\Features\Context\CoreContext
- xxx\UserBundle\Features\Context\UserContext
DefaultContext.php
namespace XXX\CommonBundle\Features\Context;
use ...
/**
* Class DefaultContext
*/
class DefaultContext extends MinkContext implements Context, KernelAwareContext
{
/**
* @param AbstractEntity $oEntity
*
* @return object
*/
protected function getRepository(AbstractEntity $oEntity)
{
return $this->getService($oEntity);
}
/**
* @return mixed
*/
protected function getEntityManager()
{
return $this->getService('doctrine')->getManager();
}
/**
* @param $id
*
* @return object
*/
protected function getService($id)
{
return $this->getContainer()->get($id);
}
CoreContext.php
namespace XXX\CommonBundle\Features\Context;
use ...
/**
* Class SubContext
*/
class CoreContext extends DefaultContext implements Context, SnippetAcceptingContext
{
/**
* @Given I visit the homepage
* @When I visit the homepage
*/
public function iVisitHomepage()
{
$this->visitPath('/');
}
/**
* @And /^I am logged in as "([^"]*)"$/
* @Then /^I am logged in as "([^"]*)"$/
*/
public function iAmLoggedInAs($username)
{
$user = $this->getContainer()->get('fos_user.user_manager')->findUserByUsername($username);
$this->visit('/login');
$this->fillField('_username', $user->getEmailCanonical());
$this->fillField('_password', self::USER_PASSWORD);
$this->pressButton('_submit');
}
UserContext.php
namespace xxx\UserBundle\Features\Context;
use ...
/**
* Defines application features from the specific context.
*/
class UserContext extends CoreContext implements Context, SnippetAcceptingContext
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}
}
register_user.feature
Feature: Register user
@javascript
Scenario: Register user
Given I am on homepage
And I go to "/register/"
And I am logged in as "foo@bar.com"
Then I should see "Terms and Conditions"
所以当我运行测试时,我得到一个错误说:
Given I am on homepage
Step "I visit the homepage" is already defined in xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
And I go to "/register/"
Step "I visit the homepage" is already defined in xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
我是否完全理解这一点,或者我在这里错过了一些设置?
答案 0 :(得分:2)
不要扩展提供步骤定义的上下文。没有办法解决这个问题。
良好的扩展为上下文提供了方便的方法,但没有步骤定义。如果是Mink扩展程序,则MinkContext
旁边还有RawMinkContext
。首先提供步骤定义,不应该扩展。另一个提供了您可能感兴趣的辅助方法。RawMinkContext
是您应该扩展的方法。或者,您也可以使用MinkAwareTrait
。
答案 1 :(得分:0)
Behat2中的Contexts以及Behat3中现在的内容发生了变化。 Behat3中的上下文更多关于"情境"您的测试可能会发生。例如:
等等。
所以我担心问题出在你在两个套件中使用的CoreContext的配置中。您可以尝试通过单独运行套件来避免这种情况,因此它不会加载两个配置(并且它不会自动加载两次相同的上下文)或者更改上下文策略以不使用自动加载,但是不同的东西就像将公共步骤逻辑封装到他们自己的类,然后从不同的上下文中使用。
希望有所帮助