Merge branch 'MDL-70148-master' of git://github.com/andrewnicols/moodle into master

This commit is contained in:
Eloy Lafuente (stronk7) 2020-11-17 23:12:56 +01:00
commit 6ca32e21e5
34 changed files with 323 additions and 124 deletions

View file

@ -38,6 +38,10 @@ use Behat\Mink\Session;
require_once(__DIR__ . '/classes/component_named_selector.php');
require_once(__DIR__ . '/classes/component_named_replacement.php');
// Alias the WebDriver\Key class to behat_keys to make future transition to a different WebDriver implementation
// easier.
class_alias('WebDriver\\Key', 'behat_keys');
/**
* Steps definitions base class.
*
@ -264,6 +268,44 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
];
}
/**
* Send key presses straight to the currently active element.
*
* The `$keys` array contains a list of key values to send to the session as defined in the WebDriver and JsonWire
* specifications:
* - JsonWire: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#sessionsessionidkeys
* - W3C WebDriver: https://www.w3.org/TR/webdriver/#keyboard-actions
*
* This may be a combination of typable characters, modifier keys, and other supported keypoints.
*
* The NULL_KEY should be used to release modifier keys. If the NULL_KEY is not used then modifier keys will remain
* in the pressed state.
*
* Example usage:
*
* behat_base::type_keys($this->getSession(), [behat_keys::SHIFT, behat_keys::TAB, behat_keys::NULL_KEY]);
* behat_base::type_keys($this->getSession(), [behat_keys::ENTER, behat_keys::NULL_KEY]);
* behat_base::type_keys($this->getSession(), [behat_keys::ESCAPE, behat_keys::NULL_KEY]);
*
* It can also be used to send text input, for example:
*
* behat_base::type_keys(
* $this->getSession(),
* ['D', 'o', ' ', 'y', 'o', 'u', ' ', 'p', 'l', 'a' 'y', ' ', 'G', 'o', '?', behat_base::NULL_KEY]
* );
*
*
* Please note: This function does not use the element/sendKeys variants but sends keys straight to the browser.
*
* @param Session $session
* @param string[] $keys
*/
public static function type_keys(Session $session, array $keys): void {
$session->getDriver()->getWebDriverSession()->keys([
'value' => $keys,
]);
}
/**
* Finds DOM nodes in the page using named selectors.
*

View file

@ -78,14 +78,12 @@ class behat_form_autocomplete extends behat_form_text {
$suggestion->click();
} else {
// Press the return key to create a new tag.
// Note: We cannot use $this->key_press() because the keyPress action, in combination with the keyDown
// submits the form.
$this->field->keyDown(13);
$this->field->keyUp(13);
behat_base::type_keys($this->session, [behat_keys::ENTER]);
}
$this->wait_for_pending_js();
$this->key_press(27);
// Press the escape to close the autocomplete suggestions list.
behat_base::type_keys($this->session, [behat_keys::ESCAPE]);
$this->wait_for_pending_js();
}
}

View file

@ -12,7 +12,7 @@ Feature: Navigate action menu
# 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"
And I press the down key
# The menu should still be visible.
And ".usermenu [role='menu']" "css_element" should be visible

View file

@ -27,12 +27,12 @@
require_once(__DIR__ . '/../../behat/behat_base.php');
use Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
Behat\Mink\Exception\DriverException as DriverException,
WebDriver\Exception\NoSuchElement as NoSuchElement,
WebDriver\Exception\StaleElementReference as StaleElementReference,
Behat\Gherkin\Node\TableNode as TableNode;
use Behat\Gherkin\Node\TableNode as TableNode;
use Behat\Mink\Exception\DriverException as DriverException;
use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
use Behat\Mink\Exception\ExpectationException as ExpectationException;
use WebDriver\Exception\NoSuchElement as NoSuchElement;
use WebDriver\Exception\StaleElementReference as StaleElementReference;
/**
* Cross component steps definitions.
@ -1710,6 +1710,143 @@ EOF;
}
}
/**
* Send key presses to the browser without first changing focusing, or applying the key presses to a specific
* element.
*
* Example usage of this step:
* When I type "Penguin"
*
* @When I type :keys
* @param string $keys The key, or list of keys, to type
*/
public function i_type(string $keys): void {
behat_base::type_keys($this->getSession(), str_split($keys));
}
/**
* Press a named key with an optional set of modifiers.
*
* Supported named keys are:
* - up
* - down
* - left
* - right
* - pageup|page_up
* - pagedown|page_down
* - home
* - end
* - insert
* - delete
* - backspace
* - escape
* - enter
* - tab
*
* Supported moderators are:
* - shift
* - ctrl
* - alt
* - meta
*
* Example usage of this new step:
* When I press the up key
* When I press the space key
* When I press the shift tab key
*
* Multiple moderator keys can be combined using the '+' operator, for example:
* When I press the ctrl+shift enter key
* When I press the ctrl + shift enter key
*
* @When /^I press the (?P<modifiers_string>.* )?(?P<key_string>.*) key$/
* @param string $modifiers A list of keyboard modifiers, separated by the `+` character
* @param string $key The name of the key to press
*/
public function i_press_named_key(string $modifiers, string $key): void {
behat_base::require_javascript_in_session($this->getSession());
$keys = [];
foreach (explode('+', $modifiers) as $modifier) {
switch (strtoupper(trim($modifier))) {
case '':
break;
case 'SHIFT':
$keys[] = behat_keys::SHIFT;
break;
case 'CTRL':
$keys[] = behat_keys::CONTROL;
break;
case 'ALT':
$keys[] = behat_keys::ALT;
break;
case 'META':
$keys[] = behat_keys::META;
break;
default:
throw new \coding_exception("Unknown modifier key '$modifier'}");
}
}
$modifier = trim($key);
switch (strtoupper($key)) {
case 'UP':
$keys[] = behat_keys::UP_ARROW;
break;
case 'DOWN':
$keys[] = behat_keys::DOWN_ARROW;
break;
case 'LEFT':
$keys[] = behat_keys::LEFT_ARROW;
break;
case 'RIGHT':
$keys[] = behat_keys::RIGHT_ARROW;
break;
case 'HOME':
$keys[] = behat_keys::HOME;
break;
case 'END':
$keys[] = behat_keys::END;
break;
case 'INSERT':
$keys[] = behat_keys::INSERT;
break;
case 'BACKSPACE':
$keys[] = behat_keys::BACKSPACE;
break;
case 'DELETE':
$keys[] = behat_keys::DELETE;
break;
case 'PAGEUP':
case 'PAGE_UP':
$keys[] = behat_keys::PAGE_UP;
break;
case 'PAGEDOWN':
case 'PAGE_DOWN':
$keys[] = behat_keys::PAGE_DOWN;
break;
case 'ESCAPE':
$keys[] = behat_keys::ESCAPE;
break;
case 'ENTER':
$keys[] = behat_keys::ENTER;
break;
case 'TAB':
$keys[] = behat_keys::TAB;
break;
case 'SPACE':
$keys[] = behat_keys::SPACE;
break;
default:
throw new \coding_exception("Unknown key '$key'}");
}
// Always send the NULL key as the last key.
$keys[] = behat_keys::NULL_KEY;
behat_base::type_keys($this->getSession(), $keys);
}
/**
* Trigger a keydown event for a key on a specific element.
*
@ -1761,12 +1898,8 @@ EOF;
}
// Gets the node based on the requested selector type and locator.
$node = $this->get_selected_node($selectortype, $element);
$driver = $this->getSession()->getDriver();
if ($driver instanceof \Moodle\BehatExtension\Driver\MoodleSelenium2Driver) {
$driver->post_key("\xEE\x80\x84", $node->getXpath());
} else {
$driver->keyDown($node->getXpath(), "\t");
}
$this->execute('behat_general::i_click_on', [$node, 'NodeElement']);
$this->execute('behat_general::i_press_named_key', ['', 'tab']);
}
/**
@ -1863,12 +1996,11 @@ EOF;
* @throws DriverException
*/
public function i_manually_press_tab($shift = '') {
if (!$this->running_javascript()) {
throw new DriverException($shift . ' Tab press step is not available with Javascript disabled');
if (empty($shift)) {
$this->execute('behat_general::i_press_named_key', ['', 'tab']);
} else {
$this->execute('behat_general::i_press_named_key', ['shift', 'tab']);
}
$value = ($shift == ' shift') ? [\WebDriver\Key::SHIFT . \WebDriver\Key::TAB] : [\WebDriver\Key::TAB];
$this->getSession()->getDriver()->getWebDriverSession()->activeElement()->postValue(['value' => $value]);
}
/**
@ -1930,12 +2062,7 @@ EOF;
* @throws DriverException
*/
public function i_manually_press_enter() {
if (!$this->running_javascript()) {
throw new DriverException('Enter press step is not available with Javascript disabled');
}
$value = [\WebDriver\Key::ENTER];
$this->getSession()->getDriver()->getWebDriverSession()->activeElement()->postValue(['value' => $value]);
$this->execute('behat_general::i_press_named_key', ['', 'enter']);
}
/**