Skip to content

Commit

Permalink
MDL-79938 Behat: Add steps to support multiple tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Dec 13, 2023
1 parent 86716dc commit 607a452
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
34 changes: 34 additions & 0 deletions admin/tool/behat/tests/behat/tabs.feature
@@ -0,0 +1,34 @@
@tool_behat
Feature: Confirm that we can open multiple browser tabs
In order to use multiple browser tabs
As a test writer
I need the relevant Behat steps to work

@javascript @_switch_window
Scenario: Open multiple browser tabs
Given the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
| Course 2 | C2 |
| Course 3 | C3 |
And I am on the "C1" "Course" page logged in as "admin"

# Open a new tab on the same page.
When I open a tab named "CourseViewer1" on the current page
And I should see "Course 1" in the "h1" "css_element"
And I am on the "C2" "Course" page

# Open new tab for specified page with identifier.
And I open a tab named "CourseViewer2" on the "C3" "Course" page

# And for a specified page without identifier.
And I open a tab named "CourseViewer4" on the "My courses" page

# Switch between all the tabs and confirm their different contents.
Then I should see "No courses"
And I switch to "CourseViewer2" tab
And I should see "Course 3" in the "h1" "css_element"
And I switch to "CourseViewer1" tab
And I should see "Course 2" in the "h1" "css_element"
And I switch to the main tab
And I should see "Course 1" in the "h1" "css_element"
4 changes: 2 additions & 2 deletions lib/tests/behat/behat_general.php
Expand Up @@ -200,7 +200,7 @@ public function switch_to_the_main_frame() {
/**
* Switches to the specified window. Useful when interacting with popup windows.
*
* @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/
* @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" (window|tab)$/
* @param string $windowname
*/
public function switch_to_window($windowname) {
Expand Down Expand Up @@ -232,7 +232,7 @@ public function switch_to_second_window() {
/**
* Switches to the main Moodle window. Useful when you finish interacting with popup windows.
*
* @Given /^I switch to the main window$/
* @Given /^I switch to the main (window|tab)$/
*/
public function switch_to_the_main_window() {
$this->switch_to_window(self::MAIN_WINDOW_NAME);
Expand Down
57 changes: 57 additions & 0 deletions lib/tests/behat/behat_navigation.php
Expand Up @@ -909,6 +909,63 @@ protected function resolve_core_page_instance_url(string $type, string $identifi
throw new Exception('Unrecognised core page type "' . $type . '."');
}

/**
* Opens a new tab with given name on the same URL as current page and switches to it.
*
* @param string $name Tab name that can be used for switching later (no whitespace)
* @When /^I open a tab named "(?<name>[^"]*)" on the current page$/
*/
public function i_open_a_tab_on_the_current_page(string $name): void {
$this->open_tab($name, 'location.href');
}

/**
* Opens a new tab with given name on specified page, and switches to it.
*
* @param string $name Tab name that can be used for switching later (no whitespace)
* @param string $page Page name
* @When /^I open a tab named "(?<name>[^"]*)" on the "(?<page>[^"]*)" page$/
*/
public function i_open_a_tab_on_the_page(string $name, string $page): void {
if ($page === 'current') {
$jstarget = 'location.href';
} else {
$jstarget = '"' . addslashes_js($this->resolve_page_helper($page)->out(false)) . '"';
}
$this->open_tab($name, $jstarget);
}

/**
* Opens a new tab with given name (on specified page), and switches to it.
*
* @param string $name Tab name that can be used for switching later (no whitespace)
* @param string $identifier Page identifier
* @param string $page Page type
* @When /^I open a tab named "(?<name>[^"]*)" on the "(?<identifier>[^"]*)" "(?<page>[^"]*)" page$/
*/
public function i_open_a_tab_on_the_page_instance(string $name, string $identifier, string $page): void {
$this->open_tab($name, '"' . addslashes_js(
$this->resolve_page_instance_helper($identifier, $page)->out(false)) . '"');
}

/**
* Opens a new tab at the given target URL.
*
* @param string $name Name for tab
* @param string $jstarget Target in JavaScript syntax, i.e. if a string, must be quoted
*/
protected function open_tab(string $name, string $jstarget): void {
// Tab names aren't allowed spaces, and our JavaScript below doesn't do any escaping.
if (clean_param($name, PARAM_ALPHANUMEXT) !== $name) {
throw new Exception('Tab name may not contain whitespace or special characters: "' . $name . '"');
}

// Normally you can't open a tab unless in response to a user action, but presumably Behat
// is exempt from this restriction, because it works to just open it directly.
$this->execute_script('window.open(' . $jstarget . ', "' . $name . '");');
$this->execute('behat_general::switch_to_window', [$name]);
}

/**
* Opens the course homepage. (Consider using 'I am on the "shortname" "Course" page' step instead.)
*
Expand Down

0 comments on commit 607a452

Please sign in to comment.