From 1303eb290f01df37102cd186525f0613cc2f6edd Mon Sep 17 00:00:00 2001 From: David Monllao Date: Thu, 11 Apr 2013 13:34:22 +0800 Subject: [PATCH] MDL-38958 behat: Step definitions to interact with popup windows --- lib/tests/behat/behat_general.php | 19 +++++++++++++++++++ lib/tests/behat/behat_hooks.php | 13 +++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 76fbdc3a56a..02e890cda67 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -63,6 +63,25 @@ class behat_general extends behat_base { $this->getSession()->reload(); } + /** + * Switches to the specified window. Useful when interacting with popup windows. + * + * @Given /^I switch to "(?P(?:[^"]|\\")*)" window$/ + * @param string $windowname + */ + public function switch_to_window($windowname) { + $this->getSession()->switchToWindow($windowname); + } + + /** + * Switches to the main Moodle window. Useful when you finish interacting with popup windows. + * + * @Given /^I switch to the main window$/ + */ + public function switch_to_the_main_window() { + $this->getSession()->switchToWindow(); + } + /** * Clicks link with specified id|title|alt|text. * diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 60b6768778e..8d99b54d8ea 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -29,9 +29,10 @@ require_once(__DIR__ . '/../../behat/behat_base.php'); -use Behat\Behat\Event\SuiteEvent as SuiteEvent; -use Behat\Behat\Event\ScenarioEvent as ScenarioEvent; -use Behat\Behat\Event\StepEvent as StepEvent; +use Behat\Behat\Event\SuiteEvent as SuiteEvent, + Behat\Behat\Event\ScenarioEvent as ScenarioEvent, + Behat\Behat\Event\StepEvent as StepEvent, + WebDriver\Exception\NoSuchWindow as NoSuchWindow; /** * Hooks to the behat process. @@ -170,7 +171,11 @@ class behat_hooks extends behat_base { } // Wait until the page is ready. - $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")'); + try { + $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")'); + } catch (NoSuchWindow $e) { + // If we were interacting with a popup window it will not exists after closing it. + } } }