Skip to content

Commit

Permalink
Merge pull request #484 from jhedstrom/468-js-steps
Browse files Browse the repository at this point in the history
Adds a trait for determining current scenario tags.
  • Loading branch information
jhedstrom committed Apr 17, 2018
2 parents 8adc65c + a6e7dd2 commit bd90c35
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
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);
}
}

0 comments on commit bd90c35

Please sign in to comment.