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)
{
$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 {

View File

@ -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
@ -65,22 +68,35 @@ trait StepHelper
/**
* 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)) {
$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)
));
}
foreach ($checks as $check) {
if ($check instanceof TaggedNodeInterface && $check->hasTag($tag)) {
return true;
}
// Check scenario
$scenario = $this->getStepScenario($feature, $event->getStep());
if ($scenario && $scenario->hasTag($tag)) {
return true;
}
return false;
}
}