Behat context in a trait
Asked Answered
M

3

6

The basic Behat use case recommends using FeatureContext class. Also, you can specify any other PHP classes in the features/bootstrap directory and they are loaded, but in the alphabetical order, no matter what the dependencies are.

Given there is a trait 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 within the FeatureContext:

class FeatureContext extends BehatContext {
    use MyLovelyTrait;
}

And that fails because M > F, in the alphabet.

I will be happy to use composer autoloading, but I don't want to require_once the autoload.php file in the top of BehatContext.php file. Is there a way to specify this in behat.yml configuration? Also, any other best practice answer regarding class-loading of Behat context files will be appreciated.

Myrta answered 5/3, 2014 at 13:42 Comment(1)
What about features/bootstrap/Autoload.php as F > A and that autoloader then can load MyLovelyTrait.php if needed? Perhaps not very creative. Otherwise create a ticket and provide an updated test for github.com/Behat/Behat/blob/3.0/features/traits.feature as according to that feature, it should be supported already "out of the box" :)Alaska
S
1

I'm not 100% sure this is answering your question but I am under the impression that you are trying to use multiple context files? If so you don't need the use statement instead within the FeatureContext.php construct method we use the line:

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

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

Swint answered 17/3, 2014 at 10:57 Comment(2)
Hi, what's the benefit of using subcontexts, compared to traits? I see traits and composer autoloading are standard thing, subcotext is sort of "proprietary trait" specific to Behat.Myrta
I found this comment on behat documentation: "PHP does not yet support horizontal reusability in its core feature set. While this functionality, called traits, is on the roadmap for PHP 5.4, Behat provides subcontexts as a stop-gap solution to achieve horizontal reusability until this functionality is available in a stable PHP release." Which makes me think since traits are available we should not use subcontext, or should we?Dastard
S
1

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

3.0.0beta1 / 2013-08-13
...
  * Subcontexts removed in favor of context pools
Sevenfold answered 20/3, 2014 at 7:29 Comment(1)
That's a valuable information, but not directly answering my question. What is the preferred way to create and load context pools in Behat 3.0? Are traits an option?Myrta
D
0

I hacked around it by working with the grain of behat -- all my traits start with "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;
}
Dialogue answered 20/4, 2014 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.