| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-02 21:43:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-26 16:05:58 -07:00
										 |  |  | import { it, expect } from './fixtures'; | 
					
						
							| 
									
										
										
										
											2020-09-25 23:30:46 -07:00
										 |  |  | import { attachFrame } from './utils'; | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 07:22:54 -07:00
										 |  |  | it('should type into a textarea', async ({page}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     const textarea = document.createElement('textarea'); | 
					
						
							|  |  |  |     document.body.appendChild(textarea); | 
					
						
							|  |  |  |     textarea.focus(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const text = 'Hello world. I am the text that was typed!'; | 
					
						
							|  |  |  |   await page.keyboard.type(text); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe(text); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should move with the arrow keys', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   await page.type('textarea', 'Hello World!'); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!'); | 
					
						
							|  |  |  |   for (let i = 0; i < 'World!'.length; i++) | 
					
						
							|  |  |  |     await page.keyboard.press('ArrowLeft'); | 
					
						
							|  |  |  |   await page.keyboard.type('inserted '); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello inserted World!'); | 
					
						
							|  |  |  |   await page.keyboard.down('Shift'); | 
					
						
							|  |  |  |   for (let i = 0; i < 'inserted '.length; i++) | 
					
						
							|  |  |  |     await page.keyboard.press('ArrowLeft'); | 
					
						
							|  |  |  |   await page.keyboard.up('Shift'); | 
					
						
							|  |  |  |   await page.keyboard.press('Backspace'); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should send a character with ElementHandle.press', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   const textarea = await page.$('textarea'); | 
					
						
							|  |  |  |   await textarea.press('a'); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('a'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.evaluate(() => window.addEventListener('keydown', e => e.preventDefault(), true)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await textarea.press('b'); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('a'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-11 12:43:38 -03:00
										 |  |  | it('should send a character with insertText', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   await page.focus('textarea'); | 
					
						
							|  |  |  |   await page.keyboard.insertText('嗨'); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('嗨'); | 
					
						
							|  |  |  |   await page.evaluate(() => window.addEventListener('keydown', e => e.preventDefault(), true)); | 
					
						
							|  |  |  |   await page.keyboard.insertText('a'); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('嗨a'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('insertText should only emit input event', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   await page.focus('textarea'); | 
					
						
							|  |  |  |   page.on('console', m => console.log(m.text())); | 
					
						
							|  |  |  |   const events = await page.evaluateHandle(() => { | 
					
						
							|  |  |  |     const events = []; | 
					
						
							|  |  |  |     document.addEventListener('keydown', e => events.push(e.type)); | 
					
						
							|  |  |  |     document.addEventListener('keyup', e => events.push(e.type)); | 
					
						
							|  |  |  |     document.addEventListener('keypress', e => events.push(e.type)); | 
					
						
							|  |  |  |     document.addEventListener('input', e => events.push(e.type)); | 
					
						
							|  |  |  |     return events; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.keyboard.insertText('hello world'); | 
					
						
							|  |  |  |   expect(await events.jsonValue()).toEqual(['input']); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-26 16:05:58 -07:00
										 |  |  | it('should report shiftKey', (test, { browserName, platform }) => { | 
					
						
							|  |  |  |   test.fail(browserName === 'firefox' && platform === 'darwin'); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  | }, async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   const keyboard = page.keyboard; | 
					
						
							|  |  |  |   const codeForKey = {'Shift': 16, 'Alt': 18, 'Control': 17}; | 
					
						
							|  |  |  |   for (const modifierKey in codeForKey) { | 
					
						
							|  |  |  |     await keyboard.down(modifierKey); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |     expect(await page.evaluate('getResult()')).toBe('Keydown: ' + modifierKey + ' ' + modifierKey + 'Left ' + codeForKey[modifierKey] + ' [' + modifierKey + ']'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     await keyboard.down('!'); | 
					
						
							|  |  |  |     // Shift+! will generate a keypress
 | 
					
						
							|  |  |  |     if (modifierKey === 'Shift') | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |       expect(await page.evaluate('getResult()')).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']\nKeypress: ! Digit1 33 33 [' + modifierKey + ']'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |       expect(await page.evaluate('getResult()')).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     await keyboard.up('!'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |     expect(await page.evaluate('getResult()')).toBe('Keyup: ! Digit1 49 [' + modifierKey + ']'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     await keyboard.up(modifierKey); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |     expect(await page.evaluate('getResult()')).toBe('Keyup: ' + modifierKey + ' ' + modifierKey + 'Left ' + codeForKey[modifierKey] + ' []'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should report multiple modifiers', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   const keyboard = page.keyboard; | 
					
						
							|  |  |  |   await keyboard.down('Control'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe('Keydown: Control ControlLeft 17 [Control]'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await keyboard.down('Alt'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe('Keydown: Alt AltLeft 18 [Alt Control]'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await keyboard.down(';'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe('Keydown: ; Semicolon 186 [Alt Control]'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await keyboard.up(';'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe('Keyup: ; Semicolon 186 [Alt Control]'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await keyboard.up('Control'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe('Keyup: Control ControlLeft 17 [Alt]'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await keyboard.up('Alt'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe('Keyup: Alt AltLeft 18 []'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should send proper codes while typing', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   await page.keyboard.type('!'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe( | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |       [ 'Keydown: ! Digit1 49 []', | 
					
						
							|  |  |  |         'Keypress: ! Digit1 33 33 []', | 
					
						
							|  |  |  |         'Keyup: ! Digit1 49 []'].join('\n')); | 
					
						
							|  |  |  |   await page.keyboard.type('^'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe( | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |       [ 'Keydown: ^ Digit6 54 []', | 
					
						
							|  |  |  |         'Keypress: ^ Digit6 94 94 []', | 
					
						
							|  |  |  |         'Keyup: ^ Digit6 54 []'].join('\n')); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should send proper codes while typing with shift', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   const keyboard = page.keyboard; | 
					
						
							|  |  |  |   await keyboard.down('Shift'); | 
					
						
							|  |  |  |   await page.keyboard.type('~'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe( | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |       [ 'Keydown: Shift ShiftLeft 16 [Shift]', | 
					
						
							|  |  |  |         'Keydown: ~ Backquote 192 [Shift]', // 192 is ` keyCode
 | 
					
						
							|  |  |  |         'Keypress: ~ Backquote 126 126 [Shift]', // 126 is ~ charCode
 | 
					
						
							|  |  |  |         'Keyup: ~ Backquote 192 [Shift]'].join('\n')); | 
					
						
							|  |  |  |   await keyboard.up('Shift'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should not type canceled events', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   await page.focus('textarea'); | 
					
						
							|  |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     window.addEventListener('keydown', event => { | 
					
						
							|  |  |  |       event.stopPropagation(); | 
					
						
							|  |  |  |       event.stopImmediatePropagation(); | 
					
						
							|  |  |  |       if (event.key === 'l') | 
					
						
							|  |  |  |         event.preventDefault(); | 
					
						
							|  |  |  |       if (event.key === 'o') | 
					
						
							|  |  |  |         event.preventDefault(); | 
					
						
							|  |  |  |     }, false); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.keyboard.type('Hello World!'); | 
					
						
							|  |  |  |   expect(await page.$eval('textarea', textarea => textarea.value)).toBe('He Wrd!'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should press plus', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   await page.keyboard.press('+'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe( | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |       [ 'Keydown: + Equal 187 []', // 192 is ` keyCode
 | 
					
						
							|  |  |  |         'Keypress: + Equal 43 43 []', // 126 is ~ charCode
 | 
					
						
							|  |  |  |         'Keyup: + Equal 187 []'].join('\n')); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should press shift plus', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   await page.keyboard.press('Shift++'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe( | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |       [ 'Keydown: Shift ShiftLeft 16 [Shift]', | 
					
						
							|  |  |  |         'Keydown: + Equal 187 [Shift]', // 192 is ` keyCode
 | 
					
						
							|  |  |  |         'Keypress: + Equal 43 43 [Shift]', // 126 is ~ charCode
 | 
					
						
							|  |  |  |         'Keyup: + Equal 187 [Shift]', | 
					
						
							|  |  |  |         'Keyup: Shift ShiftLeft 16 []'].join('\n')); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should support plus-separated modifiers', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   await page.keyboard.press('Shift+~'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe( | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |       [ 'Keydown: Shift ShiftLeft 16 [Shift]', | 
					
						
							|  |  |  |         'Keydown: ~ Backquote 192 [Shift]', // 192 is ` keyCode
 | 
					
						
							|  |  |  |         'Keypress: ~ Backquote 126 126 [Shift]', // 126 is ~ charCode
 | 
					
						
							|  |  |  |         'Keyup: ~ Backquote 192 [Shift]', | 
					
						
							|  |  |  |         'Keyup: Shift ShiftLeft 16 []'].join('\n')); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should support multiple plus-separated modifiers', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   await page.keyboard.press('Control+Shift+~'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe( | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |       [ 'Keydown: Control ControlLeft 17 [Control]', | 
					
						
							|  |  |  |         'Keydown: Shift ShiftLeft 16 [Control Shift]', | 
					
						
							|  |  |  |         'Keydown: ~ Backquote 192 [Control Shift]', // 192 is ` keyCode
 | 
					
						
							|  |  |  |         'Keyup: ~ Backquote 192 [Control Shift]', | 
					
						
							|  |  |  |         'Keyup: Shift ShiftLeft 16 [Control]', | 
					
						
							|  |  |  |         'Keyup: Control ControlLeft 17 []'].join('\n')); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should shift raw codes', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/keyboard.html'); | 
					
						
							|  |  |  |   await page.keyboard.press('Shift+Digit3'); | 
					
						
							| 
									
										
										
										
											2020-08-21 09:53:02 -07:00
										 |  |  |   expect(await page.evaluate('getResult()')).toBe( | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |       [ 'Keydown: Shift ShiftLeft 16 [Shift]', | 
					
						
							|  |  |  |         'Keydown: # Digit3 51 [Shift]', // 51 is # keyCode
 | 
					
						
							|  |  |  |         'Keypress: # Digit3 35 35 [Shift]', // 35 is # charCode
 | 
					
						
							|  |  |  |         'Keyup: # Digit3 51 [Shift]', | 
					
						
							|  |  |  |         'Keyup: Shift ShiftLeft 16 []'].join('\n')); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should specify repeat property', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   await page.focus('textarea'); | 
					
						
							|  |  |  |   const lastEvent = await captureLastKeydown(page); | 
					
						
							|  |  |  |   await page.keyboard.down('a'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.repeat)).toBe(false); | 
					
						
							|  |  |  |   await page.keyboard.press('a'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.repeat)).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.keyboard.down('b'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.repeat)).toBe(false); | 
					
						
							|  |  |  |   await page.keyboard.down('b'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.repeat)).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.keyboard.up('a'); | 
					
						
							|  |  |  |   await page.keyboard.down('a'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.repeat)).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should type all kinds of characters', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   await page.focus('textarea'); | 
					
						
							|  |  |  |   const text = 'This text goes onto two lines.\nThis character is 嗨.'; | 
					
						
							|  |  |  |   await page.keyboard.type(text); | 
					
						
							|  |  |  |   expect(await page.$eval('textarea', t => t.value)).toBe(text); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should specify location', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   const lastEvent = await captureLastKeydown(page); | 
					
						
							|  |  |  |   const textarea = await page.$('textarea'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await textarea.press('Digit5'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.location)).toBe(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await textarea.press('ControlLeft'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.location)).toBe(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await textarea.press('ControlRight'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.location)).toBe(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await textarea.press('NumpadSubtract'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(e => e.location)).toBe(3); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should press Enter', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.setContent('<textarea></textarea>'); | 
					
						
							|  |  |  |   await page.focus('textarea'); | 
					
						
							|  |  |  |   const lastEventHandle = await captureLastKeydown(page); | 
					
						
							|  |  |  |   await testEnterKey('Enter', 'Enter', 'Enter'); | 
					
						
							|  |  |  |   await testEnterKey('NumpadEnter', 'Enter', 'NumpadEnter'); | 
					
						
							|  |  |  |   await testEnterKey('\n', 'Enter', 'Enter'); | 
					
						
							|  |  |  |   await testEnterKey('\r', 'Enter', 'Enter'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function testEnterKey(key, expectedKey, expectedCode) { | 
					
						
							|  |  |  |     await page.keyboard.press(key); | 
					
						
							|  |  |  |     const lastEvent = await lastEventHandle.jsonValue(); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |     expect(lastEvent.key).toBe(expectedKey); // had the wrong key
 | 
					
						
							|  |  |  |     expect(lastEvent.code).toBe(expectedCode); // had the wrong code
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     const value = await page.$eval('textarea', t => t.value); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |     expect(value).toBe('\n'); // failed to create a newline
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     await page.$eval('textarea', t => t.value = ''); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should throw on unknown keys', async ({page, server}) => { | 
					
						
							|  |  |  |   let error = await page.keyboard.press('NotARealKey').catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toBe('Unknown key: "NotARealKey"'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   error = await page.keyboard.press('ё').catch(e => e); | 
					
						
							|  |  |  |   expect(error && error.message).toBe('Unknown key: "ё"'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   error = await page.keyboard.press('😊').catch(e => e); | 
					
						
							|  |  |  |   expect(error && error.message).toBe('Unknown key: "😊"'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should type emoji', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   await page.type('textarea', '👹 Tokyo street Japan 🇯🇵'); | 
					
						
							|  |  |  |   expect(await page.$eval('textarea', textarea => textarea.value)).toBe('👹 Tokyo street Japan 🇯🇵'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should type emoji into an iframe', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-09-18 15:52:14 -07:00
										 |  |  |   await attachFrame(page, 'emoji-test', server.PREFIX + '/input/textarea.html'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const frame = page.frames()[1]; | 
					
						
							|  |  |  |   const textarea = await frame.$('textarea'); | 
					
						
							|  |  |  |   await textarea.type('👹 Tokyo street Japan 🇯🇵'); | 
					
						
							|  |  |  |   expect(await frame.$eval('textarea', textarea => textarea.value)).toBe('👹 Tokyo street Japan 🇯🇵'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 21:36:36 -07:00
										 |  |  | it('should handle selectAll', async ({page, server, isMac}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   const textarea = await page.$('textarea'); | 
					
						
							|  |  |  |   await textarea.type('some text'); | 
					
						
							| 
									
										
										
										
											2020-09-16 21:36:36 -07:00
										 |  |  |   const modifier = isMac ? 'Meta' : 'Control'; | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.keyboard.down(modifier); | 
					
						
							|  |  |  |   await page.keyboard.press('a'); | 
					
						
							|  |  |  |   await page.keyboard.up(modifier); | 
					
						
							|  |  |  |   await page.keyboard.press('Backspace'); | 
					
						
							|  |  |  |   expect(await page.$eval('textarea', textarea => textarea.value)).toBe(''); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 21:36:36 -07:00
										 |  |  | it('should be able to prevent selectAll', async ({page, server, isMac}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   const textarea = await page.$('textarea'); | 
					
						
							|  |  |  |   await textarea.type('some text'); | 
					
						
							|  |  |  |   await page.$eval('textarea', textarea => { | 
					
						
							|  |  |  |     textarea.addEventListener('keydown', event => { | 
					
						
							|  |  |  |       if (event.key === 'a' && (event.metaKey || event.ctrlKey)) | 
					
						
							|  |  |  |         event.preventDefault(); | 
					
						
							|  |  |  |     }, false); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-09-16 21:36:36 -07:00
										 |  |  |   const modifier = isMac ? 'Meta' : 'Control'; | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.keyboard.down(modifier); | 
					
						
							|  |  |  |   await page.keyboard.press('a'); | 
					
						
							|  |  |  |   await page.keyboard.up(modifier); | 
					
						
							|  |  |  |   await page.keyboard.press('Backspace'); | 
					
						
							|  |  |  |   expect(await page.$eval('textarea', textarea => textarea.value)).toBe('some tex'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-26 16:05:58 -07:00
										 |  |  | it('should support MacOS shortcuts', (test, { platform }) => { | 
					
						
							|  |  |  |   test.skip(platform !== 'darwin'); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  | }, async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/textarea.html'); | 
					
						
							|  |  |  |   const textarea = await page.$('textarea'); | 
					
						
							|  |  |  |   await textarea.type('some text'); | 
					
						
							|  |  |  |   // select one word backwards
 | 
					
						
							|  |  |  |   await page.keyboard.press('Shift+Control+Alt+KeyB'); | 
					
						
							|  |  |  |   await page.keyboard.press('Backspace'); | 
					
						
							|  |  |  |   expect(await page.$eval('textarea', textarea => textarea.value)).toBe('some '); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 21:36:36 -07:00
										 |  |  | it('should press the meta key', async ({page, isFirefox, isMac}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const lastEvent = await captureLastKeydown(page); | 
					
						
							|  |  |  |   await page.keyboard.press('Meta'); | 
					
						
							|  |  |  |   const {key, code, metaKey} = await lastEvent.jsonValue(); | 
					
						
							| 
									
										
										
										
											2020-09-16 21:36:36 -07:00
										 |  |  |   if (isFirefox && !isMac) | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     expect(key).toBe('OS'); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(key).toBe('Meta'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-11 10:02:07 -07:00
										 |  |  |   if (isFirefox) | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     expect(code).toBe('OSLeft'); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(code).toBe('MetaLeft'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 21:36:36 -07:00
										 |  |  |   if (isFirefox && !isMac) | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     expect(metaKey).toBe(false); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(metaKey).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should work after a cross origin navigation', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   const lastEvent = await captureLastKeydown(page); | 
					
						
							|  |  |  |   await page.keyboard.press('a'); | 
					
						
							|  |  |  |   expect(await lastEvent.evaluate(l => l.key)).toBe('a'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // event.keyIdentifier has been removed from all browsers except WebKit
 | 
					
						
							| 
									
										
										
										
											2020-09-26 16:05:58 -07:00
										 |  |  | it('should expose keyIdentifier in webkit', (test, { browserName }) => { | 
					
						
							|  |  |  |   test.skip(browserName !== 'webkit'); | 
					
						
							| 
									
										
										
										
											2020-09-11 10:02:07 -07:00
										 |  |  | }, async ({page}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const lastEvent = await captureLastKeydown(page); | 
					
						
							|  |  |  |   const keyMap = { | 
					
						
							|  |  |  |     'ArrowUp': 'Up', | 
					
						
							|  |  |  |     'ArrowDown': 'Down', | 
					
						
							|  |  |  |     'ArrowLeft': 'Left', | 
					
						
							|  |  |  |     'ArrowRight': 'Right', | 
					
						
							|  |  |  |     'Backspace': 'U+0008', | 
					
						
							|  |  |  |     'Tab': 'U+0009', | 
					
						
							|  |  |  |     'Delete': 'U+007F', | 
					
						
							|  |  |  |     'a': 'U+0041', | 
					
						
							|  |  |  |     'b': 'U+0042', | 
					
						
							|  |  |  |     'F12': 'F12', | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   for (const [key, keyIdentifier] of Object.entries(keyMap)) { | 
					
						
							|  |  |  |     await page.keyboard.press(key); | 
					
						
							|  |  |  |     expect(await lastEvent.evaluate(e => e.keyIdentifier)).toBe(keyIdentifier); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should scroll with PageDown', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/scrollable.html'); | 
					
						
							|  |  |  |   // A click is required for WebKit to send the event into the body.
 | 
					
						
							|  |  |  |   await page.click('body'); | 
					
						
							|  |  |  |   await page.keyboard.press('PageDown'); | 
					
						
							|  |  |  |   // We can't wait for the scroll to finish, so just wait for it to start.
 | 
					
						
							|  |  |  |   await page.waitForFunction(() => scrollY > 0); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function captureLastKeydown(page) { | 
					
						
							|  |  |  |   const lastEvent = await page.evaluateHandle(() => { | 
					
						
							|  |  |  |     const lastEvent = { | 
					
						
							|  |  |  |       repeat: false, | 
					
						
							|  |  |  |       location: -1, | 
					
						
							|  |  |  |       code: '', | 
					
						
							|  |  |  |       key: '', | 
					
						
							|  |  |  |       metaKey: false, | 
					
						
							|  |  |  |       keyIdentifier: 'unsupported' | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     document.addEventListener('keydown', e => { | 
					
						
							|  |  |  |       lastEvent.repeat = e.repeat; | 
					
						
							|  |  |  |       lastEvent.location = e.location; | 
					
						
							|  |  |  |       lastEvent.key = e.key; | 
					
						
							|  |  |  |       lastEvent.code = e.code; | 
					
						
							|  |  |  |       lastEvent.metaKey = e.metaKey; | 
					
						
							|  |  |  |       // keyIdentifier only exists in WebKit, and isn't in TypeScript's lib.
 | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |       lastEvent.keyIdentifier = 'keyIdentifier' in e && e['keyIdentifier']; | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     }, true); | 
					
						
							|  |  |  |     return lastEvent; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   return lastEvent; | 
					
						
							|  |  |  | } |