Remove keyboard.type and keyboard.press (#98)

This patch removes keyboard.type and keyboard.press methods. The motivation
behind this is to keep only low-level API in the `keyboard` namespace.
This commit is contained in:
JoelEinbinder 2017-07-21 20:00:09 -07:00 committed by Andrey Lushnikov
parent 794f9bb82a
commit eb2cb67b0e
4 changed files with 31 additions and 66 deletions

View File

@ -65,9 +65,7 @@
* [class: Keyboard](#class-keyboard) * [class: Keyboard](#class-keyboard)
+ [keyboard.down(key[, options])](#keyboarddownkey-options) + [keyboard.down(key[, options])](#keyboarddownkey-options)
+ [keyboard.modifiers()](#keyboardmodifiers) + [keyboard.modifiers()](#keyboardmodifiers)
+ [keyboard.press(key[, options])](#keyboardpresskey-options)
+ [keyboard.sendCharacter(char)](#keyboardsendcharacterchar) + [keyboard.sendCharacter(char)](#keyboardsendcharacterchar)
+ [keyboard.type(text)](#keyboardtypetext)
+ [keyboard.up(key)](#keyboardupkey) + [keyboard.up(key)](#keyboardupkey)
* [class: Dialog](#class-dialog) * [class: Dialog](#class-dialog)
+ [dialog.accept([promptText])](#dialogacceptprompttext) + [dialog.accept([promptText])](#dialogacceptprompttext)
@ -578,6 +576,10 @@ In case of multiple pages in one browser, each page can have its own viewport si
- `text` <[string]> A text to type into a focused element. - `text` <[string]> A text to type into a focused element.
- returns: <[Promise]> Promise which resolves when the text has been successfully typed. - returns: <[Promise]> Promise which resolves when the text has been successfully typed.
Sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text.
To press a special key, use [`page.press`](#pagepresskey-options).
#### page.uploadFile(selector, ...filePaths) #### page.uploadFile(selector, ...filePaths)
- `selector` <[string]> A query selector to a file input - `selector` <[string]> A query selector to a file input
- `...filePaths` <[string]> Sets the value of the file input these paths - `...filePaths` <[string]> Sets the value of the file input these paths
@ -614,21 +616,21 @@ Shortcut for [page.mainFrame().waitForSelector()](#framewaitforselectorselectoro
### class: Keyboard ### class: Keyboard
Keyboard provides an api for managing a virtual keyboard. The high level api is [`keyboard.type`](#keyboardtypetext), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page. Keyboard provides an api for managing a virtual keyboard. The high level api is [`page.type`](#pageypetext), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page.
For finer control, you can use [`keyboard.down`](#keyboarddownkey-options), [`keyboard.up`](#keyboardupkey), and [`keyboard.sendCharacter`](#keyboardsendcharacterchar) to manually fire events as if they were generated from a real keyboard. For finer control, you can use [`keyboard.down`](#keyboarddownkey-options), [`keyboard.up`](#keyboardupkey), and [`keyboard.sendCharacter`](#keyboardsendcharacterchar) to manually fire events as if they were generated from a real keyboard.
An example of holding down `Shift` in order to select and delete some text: An example of holding down `Shift` in order to select and delete some text:
```js ```js
page.keyboard.type('Hello World!'); page.type('Hello World!');
page.keyboard.press('ArrowLeft'); page.press('ArrowLeft');
page.keyboard.down('Shift'); page.keyboard.down('Shift');
for (let i = 0; i = 0; i < ' World'.length; i++) for (let i = 0; i < ' World'.length; i++)
page.keyboard.press('ArrowLeft'); page.press('ArrowLeft');
page.keyboard.up('Shift'); page.keyboard.up('Shift');
page.keyboard.press('Backspace'); page.press('Backspace');
// Result text will end up saying 'Hello!' // Result text will end up saying 'Hello!'
``` ```
@ -651,15 +653,7 @@ If `key` is a modifier key, `Shift`, `Meta`, `Control`, or `Alt`, subsequent key
- `Control` <[boolean]> - `Control` <[boolean]>
- `Alt` <[boolean]> - `Alt` <[boolean]>
Returns which modifier keys are currently active. Use [`keyboard.down`](#keyboarddownkey) to activate a modifier key. Returns which modifier keys are currently active. Use [`keyboard.down`](#keyboarddownkey) to activate a modifier key.
#### keyboard.press(key[, options])
- `key` <[string]> Name of key to press, such as `ArrowLeft`. See [KeyboardEvent.key](https://www.w3.org/TR/uievents-key/)
- `options` <[Object]>
- `text` <[string]> If specified, generates an input event with this text.
- returns: <[Promise]>
Shortcut for [`keyboard.down`](#keyboarddownkey) and [`keyboard.up`](#keyboardupkey).
#### keyboard.sendCharacter(char) #### keyboard.sendCharacter(char)
- `char` <[string]> Character to send into the page. - `char` <[string]> Character to send into the page.
@ -671,17 +665,6 @@ Dispatches a `keypress` and `input` event. This does not send a `keydown` or `ke
page.keyboard.sendCharacter('嗨'); page.keyboard.sendCharacter('嗨');
``` ```
#### keyboard.type(text)
- `text` <[string]> Text to type into the page
- returns: <[Promise]>
Sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text.
This is the suggested way to type printable characters.
```js
page.keyboard.type('Hello World!');
```
#### keyboard.up(key) #### keyboard.up(key)
- `key` <[string]> Name of key to release, such as `ArrowLeft`. See [KeyboardEvent.key](https://www.w3.org/TR/uievents-key/) - `key` <[string]> Name of key to release, such as `ArrowLeft`. See [KeyboardEvent.key](https://www.w3.org/TR/uievents-key/)
- returns: <[Promise]> - returns: <[Promise]>

View File

@ -70,27 +70,6 @@ class Keyboard {
}); });
} }
/**
* @param {string} key
* @param {{text: (string|undefined)}} options
* @return {!Promise}
*/
async press(key, options) {
this.down(key, options);
await this.up(key);
}
/**
* @param {string} text
* @return {!Promise}
*/
async type(text) {
let last;
for (let char of text)
last = this.press(char, {text: char});
await last;
}
/** /**
* @param {string} char * @param {string} char
* @return {!Promise} * @return {!Promise}

View File

@ -514,7 +514,7 @@ class Page extends EventEmitter {
/** /**
* @param {string} selector * @param {string} selector
* @param {!Promise<number>} * @return {!Promise<number>}
*/ */
async _querySelector(selector) { async _querySelector(selector) {
let {root} = await this._client.send('DOM.getDocument', { depth: 1 }); let {root} = await this._client.send('DOM.getDocument', { depth: 1 });
@ -529,7 +529,7 @@ class Page extends EventEmitter {
/** /**
* @param {string} selector * @param {string} selector
* @param {!Promise} * @return {!Promise}
*/ */
async click(selector) { async click(selector) {
let center = await this.evaluate(selector => { let center = await this.evaluate(selector => {
@ -566,7 +566,7 @@ class Page extends EventEmitter {
/** /**
* @param {string} selector * @param {string} selector
* @param {!Promise} * @return {!Promise}
*/ */
async focus(selector) { async focus(selector) {
let success = await this.evaluate(selector => { let success = await this.evaluate(selector => {
@ -582,18 +582,23 @@ class Page extends EventEmitter {
/** /**
* @param {string} text * @param {string} text
* @param {!Promise} * @return {!Promise}
*/ */
async type(text) { async type(text) {
return this._keyboard.type(text); let last;
for (let char of text)
last = this.press(char, {text: char});
await last;
} }
/** /**
* @param {string} text * @param {string} text
* @param {!Object=} options * @param {!Object=} options
* @return {!Promise}
*/ */
async press(key, options) { async press(key, options) {
return this._keyboard.press(key, options); this._keyboard.down(key, options);
await this._keyboard.up(key);
} }
/** /**

View File

@ -759,17 +759,17 @@ describe('Puppeteer', function() {
await page.navigate(PREFIX + '/input/textarea.html'); await page.navigate(PREFIX + '/input/textarea.html');
await page.focus('textarea'); await page.focus('textarea');
let keyboard = page.keyboard; let keyboard = page.keyboard;
await keyboard.type('Hello World!'); await page.type('Hello World!');
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!'); expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!');
for (let i = 0; i < 'World!'.length; i++) for (let i = 0; i < 'World!'.length; i++)
keyboard.press('ArrowLeft'); page.press('ArrowLeft');
await keyboard.type('inserted '); await page.type('inserted ');
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello inserted World!'); expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello inserted World!');
keyboard.down('Shift'); keyboard.down('Shift');
for (let i = 0; i < 'inserted '.length; i++) for (let i = 0; i < 'inserted '.length; i++)
keyboard.press('ArrowLeft'); page.press('ArrowLeft');
keyboard.up('Shift'); keyboard.up('Shift');
await keyboard.press('Backspace'); await page.press('Backspace');
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!'); expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!');
})); }));
it('should send a character with Page.press', SX(async function() { it('should send a character with Page.press', SX(async function() {
@ -825,13 +825,12 @@ describe('Puppeteer', function() {
})); }));
it('should send proper codes while typing', SX(async function(){ it('should send proper codes while typing', SX(async function(){
await page.navigate(PREFIX + '/input/keyboard.html'); await page.navigate(PREFIX + '/input/keyboard.html');
let keyboard = page.keyboard; await page.type('!');
await keyboard.type('!');
expect(await page.evaluate(() => getResult())).toBe( expect(await page.evaluate(() => getResult())).toBe(
[ 'Keydown: ! 49 []', [ 'Keydown: ! 49 []',
'Keypress: ! 33 33 33 []', 'Keypress: ! 33 33 33 []',
'Keyup: ! 49 []'].join('\n')); 'Keyup: ! 49 []'].join('\n'));
await keyboard.type('^'); await page.type('^');
expect(await page.evaluate(() => getResult())).toBe( expect(await page.evaluate(() => getResult())).toBe(
[ 'Keydown: ^ 54 []', [ 'Keydown: ^ 54 []',
'Keypress: ^ 94 94 94 []', 'Keypress: ^ 94 94 94 []',
@ -841,7 +840,7 @@ describe('Puppeteer', function() {
await page.navigate(PREFIX + '/input/keyboard.html'); await page.navigate(PREFIX + '/input/keyboard.html');
let keyboard = page.keyboard; let keyboard = page.keyboard;
await keyboard.down('Shift'); await keyboard.down('Shift');
await keyboard.type('~'); await page.type('~');
expect(await page.evaluate(() => getResult())).toBe( expect(await page.evaluate(() => getResult())).toBe(
[ 'Keydown: Shift 16 [Shift]', [ 'Keydown: Shift 16 [Shift]',
'Keydown: ~ 192 [Shift]', // 192 is ` keyCode 'Keydown: ~ 192 [Shift]', // 192 is ` keyCode
@ -862,8 +861,7 @@ describe('Puppeteer', function() {
Promise.resolve().then(() => event.preventDefault()); Promise.resolve().then(() => event.preventDefault());
}, false); }, false);
}); });
let keyboard = page.keyboard; await page.type('Hello World!');
await keyboard.type('Hello World!');
expect(await page.evaluate(() => textarea.value)).toBe('He Wrd!'); expect(await page.evaluate(() => textarea.value)).toBe('He Wrd!');
})); }));
it('keyboard.modifiers()', SX(async function(){ it('keyboard.modifiers()', SX(async function(){