moved default session configuration and active sessions teardown into initializer
This commit is contained in:
@@ -2,8 +2,12 @@
|
|||||||
|
|
||||||
namespace Behat\MinkExtension\Context\Initializer;
|
namespace Behat\MinkExtension\Context\Initializer;
|
||||||
|
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
|
||||||
use Behat\Behat\Context\Initializer\InitializerInterface,
|
use Behat\Behat\Context\Initializer\InitializerInterface,
|
||||||
Behat\Behat\Context\ContextInterface;
|
Behat\Behat\Context\ContextInterface,
|
||||||
|
Behat\Behat\Event\ScenarioEvent,
|
||||||
|
Behat\Behat\Event\OutlineEvent;
|
||||||
|
|
||||||
use Behat\Mink\Mink;
|
use Behat\Mink\Mink;
|
||||||
|
|
||||||
@@ -23,7 +27,7 @@ use Behat\MinkExtension\Context\MinkAwareContextInterface;
|
|||||||
*
|
*
|
||||||
* @author Konstantin Kudryashov <ever.zet@gmail.com>
|
* @author Konstantin Kudryashov <ever.zet@gmail.com>
|
||||||
*/
|
*/
|
||||||
class MinkAwareInitializer implements InitializerInterface
|
class MinkAwareInitializer implements InitializerInterface, EventSubscriberInterface
|
||||||
{
|
{
|
||||||
private $mink;
|
private $mink;
|
||||||
private $parameters;
|
private $parameters;
|
||||||
@@ -40,6 +44,32 @@ class MinkAwareInitializer implements InitializerInterface
|
|||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of event names this subscriber wants to listen to.
|
||||||
|
*
|
||||||
|
* The array keys are event names and the value can be:
|
||||||
|
*
|
||||||
|
* * The method name to call (priority defaults to 0)
|
||||||
|
* * An array composed of the method name to call and the priority
|
||||||
|
* * An array of arrays composed of the method names to call and respective
|
||||||
|
* priorities, or 0 if unset
|
||||||
|
*
|
||||||
|
* For instance:
|
||||||
|
*
|
||||||
|
* * array('eventName' => 'methodName')
|
||||||
|
* * array('eventName' => array('methodName', $priority))
|
||||||
|
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
|
||||||
|
*
|
||||||
|
* @return array The event names to listen to
|
||||||
|
*/
|
||||||
|
static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'beforeScenario' => 'prepareDefaultMinkSession',
|
||||||
|
'afterSuite' => 'tearDownMinkSessions'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if initializer supports provided context.
|
* Checks if initializer supports provided context.
|
||||||
*
|
*
|
||||||
@@ -75,4 +105,44 @@ class MinkAwareInitializer implements InitializerInterface
|
|||||||
$context->setMink($this->mink);
|
$context->setMink($this->mink);
|
||||||
$context->setMinkParameters($this->parameters);
|
$context->setMinkParameters($this->parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures default Mink session before each scenario.
|
||||||
|
* Configuration is based on scenario tags.
|
||||||
|
*
|
||||||
|
* `@javascript` tagged scenarios will get `javascript_session` as default session
|
||||||
|
* `@mink:CUSTOM_NAME tagged scenarios will get `CUSTOM_NAME` as default session
|
||||||
|
* Other scenarios get `default_session` as default session
|
||||||
|
*
|
||||||
|
* @param ScenarioEvent|OutlineEvent $event
|
||||||
|
*/
|
||||||
|
public function prepareDefaultMinkSession($event)
|
||||||
|
{
|
||||||
|
$scenario = $event instanceof ScenarioEvent ? $event->getScenario() : $event->getOutline();
|
||||||
|
$session = $this->parameters['default_session'];
|
||||||
|
|
||||||
|
foreach ($scenario->getTags() as $tag) {
|
||||||
|
if ('javascript' === $tag) {
|
||||||
|
$session = $this->parameters['javascript_session'];
|
||||||
|
} elseif (preg_match('/^mink\:(.+)/', $tag, $matches)) {
|
||||||
|
$session = $matches[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($scenario->hasTag('insulated')) {
|
||||||
|
$this->mink->stopSessions();
|
||||||
|
} else {
|
||||||
|
$this->mink->resetSessions();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->mink->setDefaultSessionName($session);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops all started Mink sessions.
|
||||||
|
*/
|
||||||
|
public function tearDownMinkSessions()
|
||||||
|
{
|
||||||
|
$this->mink->stopSessions();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,31 +23,6 @@ use Behat\Behat\Context\TranslatedContextInterface,
|
|||||||
*/
|
*/
|
||||||
class MinkContext extends RawMinkContext implements TranslatedContextInterface
|
class MinkContext extends RawMinkContext implements TranslatedContextInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @BeforeScenario
|
|
||||||
*/
|
|
||||||
public function prepareMinkSessions($event)
|
|
||||||
{
|
|
||||||
$scenario = $event instanceof ScenarioEvent ? $event->getScenario() : $event->getOutline();
|
|
||||||
$session = $this->getMinkParameter('default_session');
|
|
||||||
|
|
||||||
foreach ($scenario->getTags() as $tag) {
|
|
||||||
if ('javascript' === $tag) {
|
|
||||||
$session = $this->getMinkParameter('javascript_session');
|
|
||||||
} elseif (preg_match('/^mink\:(.+)/', $tag, $matches)) {
|
|
||||||
$session = $matches[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($scenario->hasTag('insulated')) {
|
|
||||||
$this->getMink()->stopSessions();
|
|
||||||
} else {
|
|
||||||
$this->getMink()->resetSessions();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->getMink()->setDefaultSessionName($session);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens specified page.
|
* Opens specified page.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ namespace Behat\MinkExtension\Context;
|
|||||||
|
|
||||||
use Behat\Gherkin\Node\TableNode;
|
use Behat\Gherkin\Node\TableNode;
|
||||||
|
|
||||||
use Behat\Behat\Event\ScenarioEvent;
|
|
||||||
|
|
||||||
use Behat\Mink\Mink,
|
use Behat\Mink\Mink,
|
||||||
Behat\Mink\WebAssert;
|
Behat\Mink\WebAssert;
|
||||||
|
|
||||||
@@ -93,31 +91,6 @@ trait MinkDictionary
|
|||||||
return $this->getMink()->assertSession($name);
|
return $this->getMink()->assertSession($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @BeforeScenario
|
|
||||||
*/
|
|
||||||
public function prepareMinkSessions($event)
|
|
||||||
{
|
|
||||||
$scenario = $event instanceof ScenarioEvent ? $event->getScenario() : $event->getOutline();
|
|
||||||
$session = $this->getMinkParameter('default_session');
|
|
||||||
|
|
||||||
foreach ($scenario->getTags() as $tag) {
|
|
||||||
if ('javascript' === $tag) {
|
|
||||||
$session = $this->getMinkParameter('javascript_session');
|
|
||||||
} elseif (preg_match('/^mink\:(.+)/', $tag, $matches)) {
|
|
||||||
$session = $matches[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($scenario->hasTag('insulated')) {
|
|
||||||
$this->getMink()->stopSessions();
|
|
||||||
} else {
|
|
||||||
$this->getMink()->resetSessions();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->getMink()->setDefaultSessionName($session);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens specified page.
|
* Opens specified page.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
<argument type="service" id="behat.mink" />
|
<argument type="service" id="behat.mink" />
|
||||||
<argument>%behat.mink.parameters%</argument>
|
<argument>%behat.mink.parameters%</argument>
|
||||||
<tag name="behat.context.initializer" />
|
<tag name="behat.context.initializer" />
|
||||||
|
<tag name="behat.event_subscriber" priority="0" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
</services>
|
</services>
|
||||||
|
|||||||
Reference in New Issue
Block a user