Skip to content

Commit

Permalink
MDL-66979 behat: Fix window switching
Browse files Browse the repository at this point in the history
Normalise switching of window contexts by setting the main window name
to a null.

Where the window has no name (null/empty), then the root context is
selected.
  • Loading branch information
andrewnicols committed Jan 22, 2021
1 parent 74a86ac commit 6a2a2e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/tests/behat/behat_forms.php
Expand Up @@ -67,9 +67,11 @@ public function press_button_and_switch_to_main_window($button) {
// Ensures the button is present, before pressing.
$buttonnode = $this->find_button($button);
$buttonnode->press();
$this->wait_for_pending_js();
$this->look_for_exceptions();

// Switch to main window.
$this->getSession()->switchToWindow(behat_general::MAIN_WINDOW_NAME);
$this->execute('behat_general::switch_to_the_main_window');
}

/**
Expand Down
14 changes: 6 additions & 8 deletions lib/tests/behat/behat_general.php
Expand Up @@ -203,13 +203,11 @@ public function switch_to_the_main_frame() {
* @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->execute_script('if (window.name == "") window.name = "' . self::MAIN_WINDOW_NAME . '"');
if ($windowname === self::MAIN_WINDOW_NAME) {
// When switching to the main window normalise the window name to null.
// This is normalised further in the Mink driver to the root window ID.
$windowname = null;
}

$this->getSession()->switchToWindow($windowname);
}
Expand All @@ -220,7 +218,7 @@ public function switch_to_window($windowname) {
* @Given /^I switch to the main window$/
*/
public function switch_to_the_main_window() {
$this->getSession()->switchToWindow(self::MAIN_WINDOW_NAME);
$this->switch_to_window(self::MAIN_WINDOW_NAME);
}

/**
Expand Down

0 comments on commit 6a2a2e9

Please sign in to comment.