Commit 2d11e356 authored by Jérome Perrin's avatar Jérome Perrin

Zelenium: update key event dispatching to support modern browsers

Use the new KeyboardEvent instead of deprecated KeyEvent and
initKeyEvent, see
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/initKeyEvent#specifications
and
nexedi/slapos!800 (comment 115488)

In old firefox dispatching a fake keydown event with keycode 13 (enter)
on an input field was enough to submit the corresponding form, like in
a real browser and like in webdriver, but in modern browsers this no
longer submits the form. Adjust the logic to submit the form to mimic
real browser behavior.
Co-authored-by: Romain Courteaud's avatarRomain Courteaud <romain@nexedi.com>
parent 6ddf3e67
...@@ -356,7 +356,7 @@ function triggerEvent(element, eventType, canBubble, controlKeyDown, altKeyDown, ...@@ -356,7 +356,7 @@ function triggerEvent(element, eventType, canBubble, controlKeyDown, altKeyDown,
function getKeyCodeFromKeySequence(keySequence) { function getKeyCodeFromKeySequence(keySequence) {
var match = /^\\(\d{1,3})$/.exec(keySequence); var match = /^\\(\d{1,3})$/.exec(keySequence);
if (match != null) { if (match != null) {
return match[1]; return parseInt(match[1], 10);
} }
match = /^.$/.exec(keySequence); match = /^.$/.exec(keySequence);
if (match != null) { if (match != null) {
...@@ -366,7 +366,7 @@ function getKeyCodeFromKeySequence(keySequence) { ...@@ -366,7 +366,7 @@ function getKeyCodeFromKeySequence(keySequence) {
// 1 digit ascii codes will break however because they are used for the digit chars // 1 digit ascii codes will break however because they are used for the digit chars
match = /^\d{2,3}$/.exec(keySequence); match = /^\d{2,3}$/.exec(keySequence);
if (match != null) { if (match != null) {
return match[0]; return parseInt(match[0], 10);
} }
throw new SeleniumError("invalid keySequence"); throw new SeleniumError("invalid keySequence");
} }
...@@ -390,7 +390,18 @@ function triggerKeyEvent(element, eventType, keySequence, canBubble, controlKeyD ...@@ -390,7 +390,18 @@ function triggerKeyEvent(element, eventType, keySequence, canBubble, controlKeyD
} }
else { else {
var evt; var evt;
if (window.KeyEvent) { if (window.KeyboardEvent) {
evt = new KeyboardEvent(eventType, {
ctrlKey: controlKeyDown,
altKey: altKeyDown,
shiftKey: shiftKeyDown,
metaKey: metaKeyDown,
keyCode: keycode,
which: keycode,
cancelable: true,
bubbles: canBubble
});
} else if (window.KeyEvent) {
evt = document.createEvent('KeyEvents'); evt = document.createEvent('KeyEvents');
evt.initKeyEvent(eventType, true, true, window, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown, keycode, keycode); evt.initKeyEvent(eventType, true, true, window, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown, keycode, keycode);
} else { } else {
...@@ -406,7 +417,23 @@ function triggerKeyEvent(element, eventType, keySequence, canBubble, controlKeyD ...@@ -406,7 +417,23 @@ function triggerKeyEvent(element, eventType, keySequence, canBubble, controlKeyD
evt.which = keycode; evt.which = keycode;
} }
element.dispatchEvent(evt); const dispatched = element.dispatchEvent(evt);
// perform "Implicit submission" of the form, as specified in
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#implicit-submission
if ( keycode === 13
&& dispatched
&& (eventType === "keypress" || eventType === "keyup")
&& (element.type === "text" || element.type === "password")
&& element.form
&& window.SubmitEvent ) {
const defaultButton = element.form.querySelector("input[type='submit']");
if (defaultButton && !defaultButton.disabled) {
defaultButton.click();
} else {
element.form.requestSubmit();
}
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment