Add delay option to input methods (#171)

Add delay option to input methods for the keyboard and mouse.

Closes #156
This commit is contained in:
JoelEinbinder 2017-07-31 18:18:15 -07:00 committed by Andrey Lushnikov
parent 97f80d243c
commit fc70ab8f21
3 changed files with 27 additions and 5 deletions

View File

@ -54,7 +54,7 @@
+ [page.setUserAgent(userAgent)](#pagesetuseragentuseragent) + [page.setUserAgent(userAgent)](#pagesetuseragentuseragent)
+ [page.setViewport(viewport)](#pagesetviewportviewport) + [page.setViewport(viewport)](#pagesetviewportviewport)
+ [page.title()](#pagetitle) + [page.title()](#pagetitle)
+ [page.type(text)](#pagetypetext) + [page.type(text, options)](#pagetypetext-options)
+ [page.uploadFile(selector, ...filePaths)](#pageuploadfileselector-filepaths) + [page.uploadFile(selector, ...filePaths)](#pageuploadfileselector-filepaths)
+ [page.url()](#pageurl) + [page.url()](#pageurl)
+ [page.viewport()](#pageviewport) + [page.viewport()](#pageviewport)
@ -358,6 +358,7 @@ Shortcut for [page.mainFrame().addScriptTag(url)](#frameaddscripttagurl).
- `options` <[Object]> - `options` <[Object]>
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`. - `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
- `clickCount` <[number]> defaults to 1 - `clickCount` <[number]> defaults to 1
- `delay` <[number]> Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0.
- returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully clicked. Promise gets rejected if there's no element matching `selector`. - returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully clicked. Promise gets rejected if there's no element matching `selector`.
<!-- gen:stop --> <!-- gen:stop -->
@ -547,6 +548,7 @@ The `format` options are:
- `key` <[string]> Name of key to press, such as `ArrowLeft`. See [KeyboardEvent.key](https://www.w3.org/TR/uievents-key/) - `key` <[string]> Name of key to press, such as `ArrowLeft`. See [KeyboardEvent.key](https://www.w3.org/TR/uievents-key/)
- `options` <[Object]> - `options` <[Object]>
- `text` <[string]> If specified, generates an input event with this text. - `text` <[string]> If specified, generates an input event with this text.
- `delay` <[number]> Time to wait between `keydown` and `keyup` in milliseconds. Defaults to 0.
- returns: <[Promise]> - returns: <[Promise]>
Shortcut for [`keyboard.down`](#keyboarddownkey-options) and [`keyboard.up`](#keyboardupkey). Shortcut for [`keyboard.down`](#keyboarddownkey-options) and [`keyboard.up`](#keyboardupkey).
@ -659,14 +661,21 @@ In case of multiple pages in one browser, each page can have its own viewport si
Shortcut for [page.mainFrame().title()](#frametitle). Shortcut for [page.mainFrame().title()](#frametitle).
#### page.type(text) #### page.type(text, options)
- `text` <[string]> A text to type into a focused element. - `text` <[string]> A text to type into a focused element.
- `options` <[Object]>
- `delay` <[number]> Time to wait between key presses in milliseconds. Defaults to 0.
- 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. Sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text.
To press a special key, use [`page.press`](#pagepresskey-options). To press a special key, use [`page.press`](#pagepresskey-options).
```js
page.type('Hello'); // Types instantly
page.type('World', {delay: 100}); // Types slower, like a user
```
#### page.uploadFile(selector, ...filePaths) #### page.uploadFile(selector, ...filePaths)
<!-- gen:paste('frame.uploadFile') --> <!-- gen:paste('frame.uploadFile') -->
<!-- Text below is automatically copied from "gen:copy('frame.uploadFile')" --> <!-- Text below is automatically copied from "gen:copy('frame.uploadFile')" -->
@ -828,6 +837,7 @@ Dispatches a `keyup` event.
- `options` <[Object]> - `options` <[Object]>
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`. - `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
- `clickCount` <[number]> defaults to 1 - `clickCount` <[number]> defaults to 1
- `delay` <[number]> Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0.
- returns: <[Promise]> - returns: <[Promise]>
Shortcut for [`mouse.move`](#mousemovexy), [`mouse.down`](#mousedownkey) and [`mouse.up`](#mouseupkey). Shortcut for [`mouse.move`](#mousemovexy), [`mouse.down`](#mousedownkey) and [`mouse.up`](#mouseupkey).
@ -962,6 +972,7 @@ Adds a `<script>` tag to the frame with the desired url. Alternatively, JavaScri
- `options` <[Object]> - `options` <[Object]>
- `button` <[string]> `left`, `right`, or `middle`, defaults to `left`. - `button` <[string]> `left`, `right`, or `middle`, defaults to `left`.
- `clickCount` <[number]> defaults to 1 - `clickCount` <[number]> defaults to 1
- `delay` <[number]> Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0.
- returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully clicked. Promise gets rejected if there's no element matching `selector`. - returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully clicked. Promise gets rejected if there's no element matching `selector`.
<!-- gen:stop --> <!-- gen:stop -->

View File

@ -131,6 +131,8 @@ class Mouse {
async click(x, y, options) { async click(x, y, options) {
this.move(x, y); this.move(x, y);
this.down(options); this.down(options);
if (options && options.delay)
await new Promise(f => setTimeout(f, options.delay));
await this.up(options); await this.up(options);
} }

View File

@ -532,12 +532,19 @@ class Page extends EventEmitter {
/** /**
* @param {string} text * @param {string} text
* @param {{delay: (number|undefined)}=} options
* @return {!Promise} * @return {!Promise}
*/ */
async type(text) { async type(text, options) {
let delay = 0;
if (options && options.delay)
delay = options.delay;
let last; let last;
for (let char of text) for (let char of text) {
last = this.press(char, {text: char}); last = this.press(char, {text: char, delay});
if (delay)
await new Promise(f => setTimeout(f, delay));
}
await last; await last;
} }
@ -548,6 +555,8 @@ class Page extends EventEmitter {
*/ */
async press(key, options) { async press(key, options) {
this._keyboard.down(key, options); this._keyboard.down(key, options);
if (options && options.delay)
await new Promise(f => setTimeout(f, options.delay));
await this._keyboard.up(key); await this._keyboard.up(key);
} }