Skip to content

Commit

Permalink
Inroduce page.press (#96)
Browse files Browse the repository at this point in the history
This patch:
- introduces page.press() method
- adds more input tests

References #89
  • Loading branch information
JoelEinbinder authored and aslushnikov committed Jul 19, 2017
1 parent 71f8c76 commit febd747
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* [page.navigate(url, options)](#pagenavigateurl-options)
* [page.pdf(options)](#pagepdfoptions)
* [page.plainText()](#pageplaintext)
* [page.press(key[, options])](#pagepresskey-options)
* [page.reload(options)](#pagereloadoptions)
* [page.screenshot([options])](#pagescreenshotoptions)
* [page.setContent(html)](#pagesetcontenthtml)
Expand Down Expand Up @@ -437,6 +438,14 @@ The `format` options are:
#### page.plainText()
- returns: <[Promise]<[string]>> Returns page's inner text.

#### page.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).

#### page.reload(options)
- `options` <[Object]> Navigation parameters, same as in [page.navigate](#pagenavigateurl-options).
- returns: <[Promise]<[Response]>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect.
Expand Down
8 changes: 8 additions & 0 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,14 @@ class Page extends EventEmitter {
return this._keyboard.type(text);
}

/**
* @param {string} text
* @param {!Object=} options
*/
async press(key, options) {
return this._keyboard.press(key, options);
}

/**
* @param {string} selector
* @return {!Promise<undefined>}
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,26 @@ describe('Puppeteer', function() {
await keyboard.press('Backspace');
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!');
}));
it('should send a character with Page.press', SX(async function() {
await page.navigate(PREFIX + '/input/textarea.html');
await page.focus('textarea');
await page.press('a', {text: 'f'});
expect(await page.$('textarea', t => t.value)).toBe('f');

await page.evaluate(() => window.addEventListener('keydown', e => e.preventDefault(), true));

await page.press('a', {text: 'y'});
expect(await page.$('textarea', t => t.value)).toBe('f');
}));
it('should send a character with sendCharacter', SX(async function() {
await page.navigate(PREFIX + '/input/textarea.html');
await page.focus('textarea');
await page.keyboard.sendCharacter('嗨');
expect(await page.$('textarea', t => t.value)).toBe('嗨');
await page.evaluate(() => window.addEventListener('keydown', e => e.preventDefault(), true));
await page.keyboard.sendCharacter('a');
expect(await page.$('textarea', t => t.value)).toBe('嗨a');
}));
it('should report shiftKey', SX(async function(){
await page.navigate(PREFIX + '/input/keyboard.html');
let keyboard = page.keyboard;
Expand Down

0 comments on commit febd747

Please sign in to comment.