如何在behat 3中的上下文之间进行通信?

时间:2015-04-16 12:26:15

标签: php bdd behat

我不能再在版本3中使用getMainContext()和getSubcontext($ alias)了。在版本3中上下文之间进行通信的方式是什么。上下文特征是唯一的方法吗?

 # 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: ~

1 个答案:

答案 0 :(得分:13)

您可以使用scenario hooks,如"Accessing contexts from each other"文档页面中所述:

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
     }
}