From a92105fd3f29b18b2276219d02dbfec60669f2fd Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 14 Oct 2014 14:35:30 +0100 Subject: [PATCH 1/2] 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); } /** From 6f336f3602a54931caf15f7ecc1f7fdb4110e6ca Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 14 Oct 2014 15:10:59 +0100 Subject: [PATCH 2/2] MDL-47588 question behat tests: don't close the preview window. If you do. Chrome + Selenium2 gets stuck (on Windows at least). Selenium must be trying to send commands ot a window that does not exist any more. --- question/tests/behat/edit_questions.feature | 1 - question/tests/behat/preview_question.feature | 1 - 2 files changed, 2 deletions(-) diff --git a/question/tests/behat/edit_questions.feature b/question/tests/behat/edit_questions.feature index e9f3987be59..b5269bd802c 100644 --- a/question/tests/behat/edit_questions.feature +++ b/question/tests/behat/edit_questions.feature @@ -40,5 +40,4 @@ Feature: A teacher can edit questions in the question bank And I switch to "questionpreview" window And I should see "Edited question name" And I should see "Write a lot about what you want" - And I press "Close preview" And I switch to the main window diff --git a/question/tests/behat/preview_question.feature b/question/tests/behat/preview_question.feature index 45ad854fb09..1065fb357b4 100644 --- a/question/tests/behat/preview_question.feature +++ b/question/tests/behat/preview_question.feature @@ -42,5 +42,4 @@ Feature: A teacher can preview questions in the question bank And I press "Start again" And I press "Fill in correct responses" And the field "Answer:" matches value "2" - And I press "Close preview" And I switch to the main window