Skip to content

Commit

Permalink
Merge pull request #184 from creative-commoners/pulls/4.0/save-me-fro…
Browse files Browse the repository at this point in the history
…m-unsaved

NEW Allows scenarios to anticipate an unsaved changes modal
  • Loading branch information
robbieaverill authored Oct 15, 2018
2 parents 5ea8aae + e892ef4 commit 5146e6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Context/BasicContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,11 @@ public function takeScreenshotAfterFailedStep(AfterStepScope $event)
*/
public function closeModalDialog(AfterScenarioScope $event)
{
$expectsUnsavedChangesModal = $this->stepHasTag($event, 'unsavedChanges');

try {
// Only for failed tests on CMS page
if ($event->getTestResult()->getResultCode() === TestResult::FAILED) {
if ($expectsUnsavedChangesModal || $event->getTestResult()->getResultCode() === TestResult::FAILED) {
$cmsElement = $this->getSession()->getPage()->find('css', '.cms');
if ($cmsElement) {
try {
Expand Down
36 changes: 26 additions & 10 deletions src/Utility/StepHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace SilverStripe\BehatExtension\Utility;

use Behat\Behat\Hook\Scope\ScenarioScope;
use Behat\Behat\Hook\Scope\StepScope;
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\NodeInterface;
use Behat\Gherkin\Node\ScenarioInterface;
use Behat\Gherkin\Node\TaggedNodeInterface;
use \Exception;
use InvalidArgumentException;

/**
* Helpers for working with steps
Expand Down Expand Up @@ -65,22 +68,35 @@ protected function retryThrowable($callback, $timeout = 3)
/**
* Check if a step has a given tag
*
* @param StepScope $event
* @param StepScope|ScenarioScope $event
* @param string $tag
* @return bool
*/
protected function stepHasTag(StepScope $event, $tag)
protected function stepHasTag($event, $tag)
{
// Check feature
$feature = $event->getFeature();
if ($feature && $feature->hasTag($tag)) {
return true;
$checks = [];
if ($event instanceof StepScope) {
$checks[] = $feature = $event->getFeature();
$checks[] = $this->getStepScenario($feature, $event->getStep());
} elseif ($event instanceof ScenarioScope) {
$checks[] = $event->getFeature();
$checks[] = $event->getScenario();
} else {
throw new InvalidArgumentException(sprintf(
'%s expected an instance of either %s or %s. Got %s instead',
__METHOD__,
StepScope::class,
ScenarioScope::class,
is_object($event) ? sprintf('an instance of %s', get_class($event)) : gettype($event)
));
}
// Check scenario
$scenario = $this->getStepScenario($feature, $event->getStep());
if ($scenario && $scenario->hasTag($tag)) {
return true;

foreach ($checks as $check) {
if ($check instanceof TaggedNodeInterface && $check->hasTag($tag)) {
return true;
}
}

return false;
}
}

0 comments on commit 5146e6f

Please sign in to comment.