Eğer behat ile sub context dosyalar yaratıp, diğer context dosyaların kaynaklarına ulaşmak isterseniz, aşağıdaki örneği kullanabilirsiniz. Herşeyi tek bir FeatureContext dosyasında tutmak yerine, farklı context classlar yaratıp ortak kaynakları kullanabiliriz.


FeatureContext


namespace Application\FrontendBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class FeatureContext extends MinkContext implements ContainerAwareInterface
{
const SAY_HELLO = 'Hello';

private $container;

public function __construct()
{
$this->useContext('you_context', new YouContext());
$this->useContext('me_context', new MeContext());
}

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

public function sayHello()
{
return self::SAY_HELLO;
}
}

YouContext


namespace Application\FrontendBundle\Features\Context;

use Behat\Behat\Context\BehatContext;

class YouContext extends BehatContext
{
/**
* @Then /^I can access YouContext step definition$/
*/
public function iCanAccessYouContextStepDefinition()
{
return true;
}
}

MeContext


namespace Application\FrontendBundle\Features\Context;

use Behat\Behat\Context\BehatContext;
use Behat\Behat\Exception\BehaviorException;

class MeContext extends BehatContext
{
/**
* @Then /^I can access MeContext step definition$/
*/
public function iCanAccessMeContextStepDefinition()
{
return true;
}

/**
* @Then /^I can access FeatureContext resources from MeContext$/
*/
public function iCanAccessFeatureContextResourcesFromMeContext()
{
/** @var FeatureContext $mainContext */
$mainContext = $this->getMainContext();

if ($mainContext->sayHello() != $mainContext::SAY_HELLO) {
throw new BehaviorException('Cannot access FeatureContext resources.');
}
}
}

Behat feature


Feature: .......

Scenario: I can access step definitions of other context files
Given I can access YouContext step definition
And I can access MeContext step definition
And I can access FeatureContext resources from MeContext

Sonuç


$ bin/behat --profile=frontend
Feature: ....

Scenario: I can access step definitions of other context files
Given I can access YouContext step definition
And I can access MeContext step definition
And I can access FeatureContext resources from MeContext

1 scenarios (1 passed)
3 steps (3 passed)
0m0.304s