How to communicate between contexts in behat 3?
Asked Answered
S

1

7

I can't use getMainContext() and getSubcontext($alias) in version 3 any more. What is the way to communicate between context in version 3. Are context traits the only way?

 # behat.yml
 default:
   suites:
        guest_features:
              paths:    [ %paths.base%/features/web ]
              filters:  { role: guest }
              contexts: [ GuestContext ]

        user_features:
            paths:    [ %paths.base%/features/web ]
            filters:  { role: member }
            contexts: [ MemberContext ]

        groupled_features:
            paths:    [ %paths.base%/features/web ]
            filters:  { role: grouplead}
            contexts: [ GroupleadContext ]

        admin_features:
            paths:    [ %paths.base%/features/web ]
            filters:  { role: admin }
            contexts: [ AdminContext ]

 extensions:
    Behat\MinkExtension:
        base_url: http://ollo.com
        browser_name: firefox
        selenium2:
                capabilities: { "browser": "firefox", "version": "24"}
        goutte: ~
Sec answered 16/4, 2015 at 12:26 Comment(0)
S
16

You can use scenario hooks, as explained in the "Accessing contexts from each other" documentation page:

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class MemberContext implements Context
{
    /** @var GuestContext */
    private $guestContext;


    /** @BeforeScenario */
    public function before(BeforeScenarioScope $scope)
    {
        // Get the environment
        $environment = $scope->getEnvironment();
        // $environment is an instance of
        //        Behat\Behat\Context\Environment\InitializedContextEnvironment


        // Get all the contexts you need in this context
        $this->guestContest = $environment->getContext('GuestContext');
        // $this->guestContest is the instance of GuestContext
     }
}
Session answered 16/4, 2015 at 12:59 Comment(2)
This is working link: behat.org/en/latest/cookbooks/… - for me it working out of the box, so this answer should be acceptedBittern
@drupality the links in the answer used to work at the time the answer was posted but it seems in the meantime the documentation layout and server changed. Thank you for the comment. I updated the answer with the new locations.Session

© 2022 - 2024 — McMap. All rights reserved.