Skip to content

Commit

Permalink
MDL-66979 behat: Update passwordunmask form field control
Browse files Browse the repository at this point in the history
Update the passwordunmask form field type for behat to interact with the
form element as a human would rather than via synthetic event triggers.
  • Loading branch information
andrewnicols committed Jan 22, 2021
1 parent 330b665 commit d1e52f7
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions lib/behat/form_field/behat_form_passwordunmask.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,21 @@ class behat_form_passwordunmask extends behat_form_text {
* @return void
*/
public function set_value($value) {
if ($this->running_javascript()) {
$id = $this->field->getAttribute('id');
$js = <<<JS
(function() {
require(["jquery"], function($) {
var wrapper = $(document.getElementById("{$id}")).closest('[data-passwordunmask="wrapper"]');
wrapper.find('[data-passwordunmask="edit"]').trigger("click");
});
})();
JS;
behat_base::execute_script_in_session($this->session, $js);
if (!$this->running_javascript()) {
$this->field->setValue($value);

return;
}

$this->field->setValue($value);
$id = $this->field->getAttribute('id');
$wrapper = $this->field->getParent()->getParent()->find('css', '[data-passwordunmask="wrapper"]');
$wrapper->click();
$this->wait_for_pending_js();

// Ensure all pending JS is finished.
if ($this->running_javascript()) {
// Press enter key after setting password, so we have a stable page.
$this->field->keyDown(13);
$this->field->keyPress(13);
$this->field->keyUp(13);
$this->session->wait(behat_base::get_timeout() * 1000, behat_base::PAGE_READY_JS);
}
behat_base::type_keys($this->session, str_split($value));
$this->wait_for_pending_js();

// Press enter key after setting password to save.
behat_base::type_keys($this->session, [behat_keys::ENTER]);
}
}

0 comments on commit d1e52f7

Please sign in to comment.