diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index f211d9fc923..ee6175ae56b 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -243,6 +243,30 @@ class behat_general extends behat_base { $this->getSession()->switchToWindow(self::MAIN_WINDOW_NAME); } + /** + * Closes all extra windows opened during the navigation. + * + * This assumes all popups are opened by the main tab and you will now get back. + * + * @Given /^I close all opened windows$/ + * @throws DriverException If there aren't exactly 1 tabs open when finish or no javascript running + */ + public function i_close_all_opened_windows() { + if (!$this->running_javascript()) { + throw new DriverException('Closing windows steps require javascript'); + } + $names = $this->getSession()->getWindowNames(); + for ($index = 1; $index < count($names); $index ++) { + $this->getSession()->switchToWindow($names[$index]); + $this->getSession()->executeScript("window.open('', '_self').close();"); + } + $names = $this->getSession()->getWindowNames(); + if (count($names) !== 1) { + throw new DriverException('Expected to see 1 tabs open, not ' . count($names)); + } + $this->getSession()->switchToWindow($names[0]); + } + /** * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental. * @Given /^I accept the currently displayed dialog$/