feat(keyboard): make keyboard.down generate input event by default (#1016)

This patch starts generating input events for `keyboard.down`, addressing bullet 2 of
#723. With this patch, there's no longer any difference between `keboard.press('a')` and
`keyboard.press('a', {text: 'a'})`.

BREAKING CHANGE:
`keyboard.down('a')` starts generating input event (wasn't the case before).

References #723
This commit is contained in:
JoelEinbinder 2017-10-13 15:22:55 -07:00 committed by Andrey Lushnikov
parent a02347e3ef
commit 6a8865cd85
3 changed files with 12 additions and 4 deletions

View File

@ -1100,7 +1100,7 @@ page.keyboard.press('Backspace');
Dispatches a `keydown` event.
This will not send input events unless `text` is specified.
If `key` is a single character and no modifier keys besides `Shift` are being held down, a `keypress`/`input` event will also generated. The `text` option can be specified to force an input event to be generated.
If `key` is a modifier key, `Shift`, `Meta`, `Control`, or `Alt`, subsequent key presses will be sent with that modifier active. To release the modifier key, use [`keyboard.up`](#keyboardupkey).

View File

@ -30,8 +30,11 @@ class Keyboard {
* @param {string} key
* @param {{text: string}=} options
*/
async down(key, options = {text: ''}) {
const {text} = options;
async down(key, options = {text: undefined}) {
let { text } = options;
// If the key is a single character, and no modifiers are pressed except shift, a keypress should be generated by default.
if (text === undefined)
text = (key.length === 1 && !(this._modifiers & ~8)) ? key : '';
const autoRepeat = this._pressedKeys.has(key);
this._pressedKeys.add(key);
this._modifiers |= this._modifierBit(key);

View File

@ -1676,7 +1676,12 @@ describe('Page', function() {
await keyboard.down(modifierKey);
expect(await page.evaluate(() => getResult())).toBe('Keydown: ' + modifierKey + ' ' + modifierKey + 'Left ' + codeForKey[modifierKey] + ' [' + modifierKey + ']');
await keyboard.down('!');
expect(await page.evaluate(() => getResult())).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']');
// Shift+! will generate a keypress
if (modifierKey === 'Shift')
expect(await page.evaluate(() => getResult())).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']\nKeypress: ! Digit1 33 33 33 [' + modifierKey + ']');
else
expect(await page.evaluate(() => getResult())).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']');
await keyboard.up('!');
expect(await page.evaluate(() => getResult())).toBe('Keyup: ! Digit1 49 [' + modifierKey + ']');
await keyboard.up(modifierKey);