From a92105fd3f29b18b2276219d02dbfec60669f2fd Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 14 Oct 2014 14:35:30 +0100 Subject: [PATCH] MDL-47588 behat: fix switching back to the main window in Chrome This fix is mostly based on what Colin Chambers found out. This commit is a simplification of his work. The problem is that the Chrome / Selenium 2 integration cannot swich to a window with a blank name. The work-around applied here is, when we switch away from an unnamed window, we set a name on it. Then we can use that name to switch back. --- lib/tests/behat/behat_general.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index ada004e97df..6e1934c4f35 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -49,6 +49,12 @@ use Behat\Mink\Exception\ExpectationException as ExpectationException, */ class behat_general extends behat_base { + /** + * @var string used by {@link switch_to_window()} and + * {@link switch_to_the_main_window()} to work-around a Chrome browser issue. + */ + const MAIN_WINDOW_NAME = '__moodle_behat_main_window_name'; + /** * Opens Moodle homepage. * @@ -157,6 +163,15 @@ class behat_general extends behat_base { * @param string $windowname */ public function switch_to_window($windowname) { + // In Behat, some browsers (e.g. Chrome) are unable to switch to a + // window without a name, and by default the main browser window does + // not have a name. To work-around this, when we switch away from an + // unnamed window (presumably the main window) to some other named + // window, then we first set the main window name to a conventional + // value that we can later use this name to switch back. + $this->getSession()->evaluateScript( + 'if (window.name == "") window.name = "' . self::MAIN_WINDOW_NAME . '"'); + $this->getSession()->switchToWindow($windowname); } @@ -166,7 +181,7 @@ class behat_general extends behat_base { * @Given /^I switch to the main window$/ */ public function switch_to_the_main_window() { - $this->getSession()->switchToWindow(); + $this->getSession()->switchToWindow(self::MAIN_WINDOW_NAME); } /**