Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a trait for determining current scenario tags. #484

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Drupal/DrupalExtension/Context/MinkContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use Behat\Behat\Context\TranslatableContext;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\MinkExtension\Context\MinkContext as MinkExtension;
use Drupal\DrupalExtension\ScenarioTagTrait;

/**
* Extensions to the Mink Extension.
*/
class MinkContext extends MinkExtension implements TranslatableContext
{

use ScenarioTagTrait;

/**
* Returns list of definition translation resources paths.
*
Expand Down Expand Up @@ -92,7 +95,7 @@ public function assertEnterField($field, $value)
public function beforeJavascriptStep($event)
{
/** @var \Behat\Behat\Hook\Scope\BeforeStepScope $event */
$tags = $event->getFeature()->getTags();
$tags = $this->getCurrentScenarioTags($event);
if (!in_array('javascript', $tags)) {
return;
}
Expand All @@ -110,7 +113,7 @@ public function beforeJavascriptStep($event)
public function afterJavascriptStep($event)
{
/** @var \Behat\Behat\Hook\Scope\BeforeStepScope $event */
$tags = $event->getFeature()->getTags();
$tags = $this->getCurrentScenarioTags($event);
if (!in_array('javascript', $tags)) {
return;
}
Expand Down
61 changes: 61 additions & 0 deletions src/Drupal/DrupalExtension/ScenarioTagTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\DrupalExtension;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\StepScope;
use Behat\Gherkin\Node\ScenarioInterface;

/**
* A workaround to discover the current scenario.
*
* @see https://github.com/Behat/Behat/issues/653
* @see https://github.com/Behat/Behat/issues/650
*
* The solution is documented in this issue: https://github.com/Behat/Behat/issues/703#issuecomment-86687563
*
* @package Drupal\DrupalExtension
*/
trait ScenarioTagTrait
{

/**
* The registered scenario.
*
* @var ScenarioInterface
*/
protected $currentScenario;

/**
* Register the scenario.
*
* @param BeforeScenarioScope $scope
*
* @BeforeScenario
*/
public function registerScenario(BeforeScenarioScope $scope)
{
$this->currentScenario = $scope->getScenario();
}

/**
* @return ScenarioInterface
*/
protected function getScenario()
{
return $this->currentScenario;
}

/**
* Get all tags for the current scenario.
*
* @param StepScope $scope
* @return string[]
*/
protected function getCurrentScenarioTags(StepScope $scope)
{
$featureTags = $scope->getFeature()->getTags();
$scenarioTags = $this->getScenario()->getTags();
return array_merge($featureTags, $scenarioTags);
}
}