The specific context in the attribute

In general, Behat recommends using the FeatureContext class. In addition, you can specify any other PHP classes in the features/bootstrap directory, and they are loaded, but in alphabetical order, no matter what dependencies exist.

Given that there is a property and a FeatureContext class:

 features/bootstrap/FeatureContext.php features/bootstrap/MyLovelyTrait.php 

What is the best way to load it properly? Obviously, MyLovelyTrait is used in FeatureContext:

 class FeatureContext extends BehatContext { use MyLovelyTrait; } 

And this does not succeed, because M > F , in the alphabet.

I use composer autoload with pleasure, but I don’t want the require_once autoload.php file at the top of the BehatContext.php file. Is there a way to specify this in the behat.yml configuration? In addition, any other answer regarding loading Behat context file classes will be appreciated.

+6
source share
3 answers

I am not 100% sure that this answers your question, but I get the impression that you are trying to use multiple context files? If so, you do not need to use the use statement inside the FeatureContext.php constructor method, we use the line:

 $this -> useContext('Subcontext', new Subcontext($parameters)); 

In this case, the other context you want to use is called "Subcontext".

+1
source

A good reason not to useContext ('Subcontext') can be found in Changelog's upcoming version 3 of Behat:

 3.0.0beta1 / 2013-08-13 ... * Subcontexts removed in favor of context pools 
+1
source

I cracked it while working with a grain of behavior - all of my traits start with an β€œA.” Examples:

 // FeatureContext.php is at features/bootstrap/FeatureContext.php <?php use Behat\Behat\Context\ClosuredContextInterface, Behat\Behat\Context\TranslatedContextInterface, Behat\Behat\Context\BehatContext, Behat\Behat\Exception\PendingException; use Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\TableNode; class FeatureContext extends BehatContext { use AWebDriverContextTrait; } 

and

 // AWebDriverContextTrait is at features/bootstrap/AWebDriverContextTrait.php <?php trait AWebDriverContextTrait { /** * @Given /^I am on "([^"]+)"/ */ public function iAmOnSite($url) { $this->driver = new \Behat\Mink\Driver\Selenium2Driver( 'firefox', '' ); $this->session = new \Behat\Mink\Session($this->driver); $this->session->start(); $this->session->visit($url); } private $driver; private $session; } 
0
source

All Articles