NEW Allows scenarios to anticipate an unsaved changes modal

This commit is contained in:
Guy Marriott 2018-10-15 11:26:34 +13:00
parent 5ea8aae96d
commit e892ef4829
No known key found for this signature in database
GPG Key ID: A80F9ACCB86D3DA7
2 changed files with 29 additions and 11 deletions

View File

@ -277,9 +277,11 @@ JS;
*/ */
public function closeModalDialog(AfterScenarioScope $event) public function closeModalDialog(AfterScenarioScope $event)
{ {
$expectsUnsavedChangesModal = $this->stepHasTag($event, 'unsavedChanges');
try { try {
// Only for failed tests on CMS page // 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'); $cmsElement = $this->getSession()->getPage()->find('css', '.cms');
if ($cmsElement) { if ($cmsElement) {
try { try {

View File

@ -2,11 +2,14 @@
namespace SilverStripe\BehatExtension\Utility; namespace SilverStripe\BehatExtension\Utility;
use Behat\Behat\Hook\Scope\ScenarioScope;
use Behat\Behat\Hook\Scope\StepScope; use Behat\Behat\Hook\Scope\StepScope;
use Behat\Gherkin\Node\FeatureNode; use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\NodeInterface; use Behat\Gherkin\Node\NodeInterface;
use Behat\Gherkin\Node\ScenarioInterface; use Behat\Gherkin\Node\ScenarioInterface;
use Behat\Gherkin\Node\TaggedNodeInterface;
use \Exception; use \Exception;
use InvalidArgumentException;
/** /**
* Helpers for working with steps * Helpers for working with steps
@ -65,22 +68,35 @@ trait StepHelper
/** /**
* Check if a step has a given tag * Check if a step has a given tag
* *
* @param StepScope $event * @param StepScope|ScenarioScope $event
* @param string $tag * @param string $tag
* @return bool * @return bool
*/ */
protected function stepHasTag(StepScope $event, $tag) protected function stepHasTag($event, $tag)
{ {
// Check feature $checks = [];
$feature = $event->getFeature(); if ($event instanceof StepScope) {
if ($feature && $feature->hasTag($tag)) { $checks[] = $feature = $event->getFeature();
return true; $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()); foreach ($checks as $check) {
if ($scenario && $scenario->hasTag($tag)) { if ($check instanceof TaggedNodeInterface && $check->hasTag($tag)) {
return true; return true;
}
} }
return false; return false;
} }
} }