diff --git a/admin/tool/behat/tests/behat/tabs.feature b/admin/tool/behat/tests/behat/tabs.feature new file mode 100644 index 00000000000..0fe38b3a784 --- /dev/null +++ b/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 "You're not enrolled in any course" + 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" diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index ec12005bdf9..fe9b2ea0c96 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -200,7 +200,7 @@ class behat_general extends behat_base { /** * Switches to the specified window. Useful when interacting with popup windows. * - * @Given /^I switch to "(?P(?:[^"]|\\")*)" window$/ + * @Given /^I switch to "(?P(?:[^"]|\\")*)" (window|tab)$/ * @param string $windowname */ public function switch_to_window($windowname) { @@ -232,7 +232,7 @@ class behat_general extends behat_base { /** * 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); diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index a751114a147..fdd866c16ca 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -965,6 +965,63 @@ class behat_navigation extends behat_base { 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 "(?[^"]*)" 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 "(?[^"]*)" on the "(?[^"]*)" 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 "(?[^"]*)" on the "(?[^"]*)" "(?[^"]*)" 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.) *