If you use $mainContext = $this->getMainContext(); in one of your sub context files which extends FeatureContext file, you can easily access resources of FeatureContext. However, if you want to access sub context methods from another sub context then you can use example below.


Sub context One


use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class SubContextOne extends FeatureContext
{
/** @var SubContextTwo */
private $subContextTwo;
/** @var SubContextThree */
private $subContextThree;

/**
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

$this->subContextTwo = $environment->getContext(SubContextTwo::class);
$this->subContextThree = $environment->getContext(SubContextThree::class);
}

/**
* @BeforeScenario
*/
public function loadDataFixtures()
{
// You can manually load data fixtures here

$this->subContextTwo->doSomethingNice();
$this->subContextThree->doSomethingBad();
}

/**
* If you tag one of your step definitions with @two, it would trigger this method
*
* @BeforeScenario @two
*/
public function doSomethingNice()
{
$this->subContextTwo->doSomethingNice();
}

/**
* If you tag one of your step definitions with @three, it would trigger this method
*
* @AfterScenario @three
*/
public function doSomethingBad()
{
$this->subContextThree->doSomethingBad();
}
}