Inroduce page.press (#96)

This patch:
- introduces page.press() method
- adds more input tests

References #89
This commit is contained in:
JoelEinbinder 2017-07-19 14:43:07 -07:00 committed by Andrey Lushnikov
parent 71f8c76f04
commit febd747c5b
3 changed files with 37 additions and 0 deletions

View File

@ -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)
@ -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.

View File

@ -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>}

View File

@ -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;