From 870349ee6743ea635ea06bf8bcd2110967a83ebb Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 14 May 2013 14:05:32 +0800 Subject: [PATCH] MDL-39528 behat: Avoid modal window suite failure Catching JS alerts/confirms exceptions and auto-accepting them before each test run to avoid chained scenarios failures. --- lib/tests/behat/behat_hooks.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 9bd1f5d3546..5cc8112cb65 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -32,7 +32,9 @@ require_once(__DIR__ . '/../../behat/behat_base.php'); use Behat\Behat\Event\SuiteEvent as SuiteEvent, Behat\Behat\Event\ScenarioEvent as ScenarioEvent, Behat\Behat\Event\StepEvent as StepEvent, - WebDriver\Exception\NoSuchWindow as NoSuchWindow; + WebDriver\Exception\NoSuchWindow as NoSuchWindow, + WebDriver\Exception\UnexpectedAlertOpen as UnexpectedAlertOpen, + WebDriver\Exception\NoAlertOpenError as NoAlertOpenError; /** * Hooks to the behat process. @@ -138,6 +140,16 @@ class behat_hooks extends behat_base { // Start always in the the homepage. $this->getSession()->visit($this->locate_path('/')); + + // Closing JS dialogs if present. Otherwise they would block this scenario execution. + if ($this->running_javascript()) { + try { + $this->getSession()->getDriver()->getWebDriverSession()->accept_alert(); + } catch (NoAlertOpenError $e) { + // All ok, there should not be JS dialogs in theory. + } + } + } /** @@ -257,6 +269,13 @@ class behat_hooks extends behat_base { } catch (NoSuchWindow $e) { // If we were interacting with a popup window it will not exists after closing it. + } catch (UnexpectedAlertOpen $e) { + // We fail the scenario if we find an opened JS alert/confirm, in most of the cases it + // will be there because we are leaving an edited form without submitting/cancelling + // it, but moodle is using JS confirms and we can not just cancel the JS dialog + // as in some cases (delete activity with JS enabled for example) the test writer should + // use extra steps to deal with moodle's behaviour. + throw new Exception('Modal window present. Ensure there are no edited forms pending to submit/cancel.'); } }