From 77b0811021fa500a846a90aca266170a22795519 Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Mon, 2 Nov 2015 06:47:42 +0000 Subject: [PATCH] MDL-51983 actionmenu: behat tests --- lib/tests/behat/action_menu.feature | 29 +++++++++++++++++++++++ lib/tests/behat/behat_general.php | 36 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 lib/tests/behat/action_menu.feature diff --git a/lib/tests/behat/action_menu.feature b/lib/tests/behat/action_menu.feature new file mode 100644 index 00000000000..994604161d8 --- /dev/null +++ b/lib/tests/behat/action_menu.feature @@ -0,0 +1,29 @@ +@core +Feature: Navigate action menu + In order to navigate an action menu + As a user + I need to be able to use the keyboard + + @javascript + Scenario: The menu does not close on keyboard navigation + When I log in as "admin" + # Click to open the user menu. + And I click on ".usermenu a.toggle-display" "css_element" in the ".usermenu" "css_element" + # The menu should now be visible. + Then ".usermenu [role='menu']" "css_element" should be visible + # Press down arrow. + And I press key "40" in "#actionmenuaction-1" "css_element" + # The menu should still be visible. + And ".usermenu [role='menu']" "css_element" should be visible + + @javascript + Scenario: The menu closes when it clicked outside + When I log in as "admin" + # Click to open the user menu. + And I click on ".usermenu a.toggle-display" "css_element" in the ".usermenu" "css_element" + # The menu should now be visible. + Then ".usermenu [role='menu']" "css_element" should be visible + # Click outside the menu. + And I click on "adminsearchquery" "field" + # The menu should now be hidden. + And ".usermenu [role='menu']" "css_element" should not be visible diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index bd0d4ff3a2a..5feaa377ddd 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -1444,4 +1444,40 @@ class behat_general extends behat_base { throw new ExpectationException('Unknown browser button.', $session); } } + + /** + * Trigger a keydown event for a key on a specific element. + * + * @When /^I press key "(?P(?:[^"]|\\")*)" in "(?P(?:[^"]|\\")*)" "(?P[^"]*)"$/ + * @param string $key either char-code or character itself, + * may optionally be prefixed with ctrl-, alt-, shift- or meta- + * @param string $element Element we look for + * @param string $selectortype The type of what we look for + * @throws DriverException + * @throws ExpectationException + */ + public function i_press_key_in_element($key, $element, $selectortype) { + if (!$this->running_javascript()) { + throw new DriverException('Key down step is not available with Javascript disabled'); + } + // Gets the node based on the requested selector type and locator. + $node = $this->get_selected_node($selectortype, $element); + $modifier = null; + $validmodifiers = array('ctrl', 'alt', 'shift', 'meta'); + $char = $key; + if (strpos($key, '-')) { + list($modifier, $char) = preg_split('/-/', $key, 2); + $modifier = strtolower($modifier); + if (!in_array($modifier, $validmodifiers)) { + throw new ExpectationException(sprintf('Unknown key modifier: %s.', $modifier)); + } + } + if (is_numeric($char)) { + $char = (int)$char; + } + + $node->keyDown($char, $modifier); + $node->keyPress($char, $modifier); + $node->keyUp($char, $modifier); + } }