2018-11-21 03:43:07 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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-06-23 05:18:46 +00:00
|
|
|
import os from 'os';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
2023-02-15 23:09:31 +00:00
|
|
|
import {KeyInput} from 'puppeteer-core/internal/common/USKeyboardLayout.js';
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2022-06-15 10:09:22 +00:00
|
|
|
} from './mocha-utils.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
import utils from './utils.js';
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Keyboard', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should type into a textarea', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
const textarea = document.createElement('textarea');
|
|
|
|
document.body.appendChild(textarea);
|
|
|
|
textarea.focus();
|
2018-11-21 03:43:07 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
const text = 'Hello world. I am the text that was typed!';
|
|
|
|
await page.keyboard.type(text);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(text);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should press the metaKey', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, isFirefox} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.evaluate(() => {
|
2022-06-22 13:25:44 +00:00
|
|
|
(window as any).keyPromise = new Promise(resolve => {
|
|
|
|
return document.addEventListener('keydown', event => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return resolve(event.key);
|
|
|
|
});
|
|
|
|
});
|
2018-11-21 03:43:07 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.press('Meta');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.evaluate('keyPromise')).toBe(
|
|
|
|
isFirefox && os.platform() !== 'darwin' ? 'OS' : 'Meta'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should move with the arrow keys', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
await page.type('textarea', 'Hello World!');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('Hello World!');
|
2022-06-14 11:55:35 +00:00
|
|
|
for (let i = 0; i < 'World!'.length; i++) {
|
|
|
|
page.keyboard.press('ArrowLeft');
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.type('inserted ');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('Hello inserted World!');
|
2020-04-09 05:56:25 +00:00
|
|
|
page.keyboard.down('Shift');
|
2022-06-14 11:55:35 +00:00
|
|
|
for (let i = 0; i < 'inserted '.length; i++) {
|
2020-04-09 05:56:25 +00:00
|
|
|
page.keyboard.press('ArrowLeft');
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
page.keyboard.up('Shift');
|
|
|
|
await page.keyboard.press('Backspace');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('Hello World!');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-12-03 10:23:47 +00:00
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/1313
|
|
|
|
it('should trigger commands of keyboard shortcuts', async () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
const cmdKey = os.platform() !== 'darwin' ? 'Meta' : 'Control';
|
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
await page.type('textarea', 'hello');
|
|
|
|
|
|
|
|
await page.keyboard.down(cmdKey);
|
|
|
|
await page.keyboard.press('a', {commands: ['SelectAll']});
|
|
|
|
await page.keyboard.up(cmdKey);
|
|
|
|
|
|
|
|
await page.keyboard.down(cmdKey);
|
|
|
|
await page.keyboard.down('c', {commands: ['Copy']});
|
|
|
|
await page.keyboard.up('c');
|
|
|
|
await page.keyboard.up(cmdKey);
|
|
|
|
|
|
|
|
await page.keyboard.down(cmdKey);
|
|
|
|
await page.keyboard.press('v', {commands: ['Paste']});
|
|
|
|
await page.keyboard.up(cmdKey);
|
|
|
|
await page.keyboard.down(cmdKey);
|
|
|
|
await page.keyboard.press('v', {commands: ['Paste']});
|
|
|
|
await page.keyboard.up(cmdKey);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
|
|
|
).toBe('hellohello');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should send a character with ElementHandle.press', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const textarea = (await page.$('textarea'))!;
|
2020-04-09 05:56:25 +00:00
|
|
|
await textarea.press('a');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('a');
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return window.addEventListener(
|
|
|
|
'keydown',
|
2022-06-22 13:25:44 +00:00
|
|
|
e => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return e.preventDefault();
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
});
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await textarea.press('b');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('a');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('ElementHandle.press should support |text| option', async () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
const textarea = (await page.$('textarea'))!;
|
|
|
|
await textarea.press('a', {text: 'ё'});
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
|
|
|
).toBe('ё');
|
|
|
|
});
|
|
|
|
it('should send a character with sendCharacter', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2018-11-21 03:43:07 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
await page.focus('textarea');
|
|
|
|
await page.keyboard.sendCharacter('嗨');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('嗨');
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return window.addEventListener(
|
|
|
|
'keydown',
|
2022-06-22 13:25:44 +00:00
|
|
|
e => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return e.preventDefault();
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.sendCharacter('a');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.value;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('嗨a');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should report shiftKey', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/keyboard.html');
|
|
|
|
const keyboard = page.keyboard;
|
2020-06-23 05:18:46 +00:00
|
|
|
const codeForKey = new Map<KeyInput, number>([
|
|
|
|
['Shift', 16],
|
|
|
|
['Alt', 18],
|
|
|
|
['Control', 17],
|
|
|
|
]);
|
|
|
|
for (const [modifierKey, modifierCode] of codeForKey) {
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.down(modifierKey);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
'Keydown: ' +
|
|
|
|
modifierKey +
|
|
|
|
' ' +
|
|
|
|
modifierKey +
|
|
|
|
'Left ' +
|
2020-06-23 05:18:46 +00:00
|
|
|
modifierCode +
|
2020-05-07 10:54:55 +00:00
|
|
|
' [' +
|
|
|
|
modifierKey +
|
|
|
|
']'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.down('!');
|
|
|
|
// Shift+! will generate a keypress
|
2022-06-14 11:55:35 +00:00
|
|
|
if (modifierKey === 'Shift') {
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
'Keydown: ! Digit1 49 [' +
|
|
|
|
modifierKey +
|
|
|
|
']\nKeypress: ! Digit1 33 33 [' +
|
|
|
|
modifierKey +
|
|
|
|
']'
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']');
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await keyboard.up('!');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe('Keyup: ! Digit1 49 [' + modifierKey + ']');
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.up(modifierKey);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
'Keyup: ' +
|
|
|
|
modifierKey +
|
|
|
|
' ' +
|
|
|
|
modifierKey +
|
|
|
|
'Left ' +
|
2020-06-23 05:18:46 +00:00
|
|
|
modifierCode +
|
2020-05-07 10:54:55 +00:00
|
|
|
' []'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
}
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report multiple modifiers', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/keyboard.html');
|
|
|
|
const keyboard = page.keyboard;
|
|
|
|
await keyboard.down('Control');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe('Keydown: Control ControlLeft 17 [Control]');
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.down('Alt');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe('Keydown: Alt AltLeft 18 [Alt Control]');
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.down(';');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe('Keydown: ; Semicolon 186 [Alt Control]');
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.up(';');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe('Keyup: ; Semicolon 186 [Alt Control]');
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.up('Control');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe('Keyup: Control ControlLeft 17 [Alt]');
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.up('Alt');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe('Keyup: Alt AltLeft 18 []');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should send proper codes while typing', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/keyboard.html');
|
|
|
|
await page.keyboard.type('!');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
[
|
|
|
|
'Keydown: ! Digit1 49 []',
|
|
|
|
'Keypress: ! Digit1 33 33 []',
|
|
|
|
'Keyup: ! Digit1 49 []',
|
|
|
|
].join('\n')
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.type('^');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
[
|
|
|
|
'Keydown: ^ Digit6 54 []',
|
|
|
|
'Keypress: ^ Digit6 94 94 []',
|
|
|
|
'Keyup: ^ Digit6 54 []',
|
|
|
|
].join('\n')
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should send proper codes while typing with shift', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/keyboard.html');
|
|
|
|
const keyboard = page.keyboard;
|
|
|
|
await keyboard.down('Shift');
|
|
|
|
await page.keyboard.type('~');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).getResult();
|
|
|
|
})
|
|
|
|
).toBe(
|
2020-05-07 10:54:55 +00: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')
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
await keyboard.up('Shift');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not type canceled events', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
await page.focus('textarea');
|
|
|
|
await page.evaluate(() => {
|
2020-05-07 10:54:55 +00:00
|
|
|
window.addEventListener(
|
|
|
|
'keydown',
|
2022-06-22 13:25:44 +00:00
|
|
|
event => {
|
2020-05-07 10:54:55 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
event.stopImmediatePropagation();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (event.key === 'l') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
if (event.key === 'o') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2018-11-21 03:43:07 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.type('Hello World!');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).textarea.value;
|
|
|
|
})
|
|
|
|
).toBe('He Wrd!');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should specify repeat property', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
await page.focus('textarea');
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('textarea')!.addEventListener(
|
|
|
|
'keydown',
|
2022-06-22 13:25:44 +00:00
|
|
|
e => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return ((globalThis as any).lastEvent = e);
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.down('a');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).lastEvent.repeat;
|
|
|
|
})
|
|
|
|
).toBe(false);
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.press('a');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).lastEvent.repeat;
|
|
|
|
})
|
|
|
|
).toBe(true);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.keyboard.down('b');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).lastEvent.repeat;
|
|
|
|
})
|
|
|
|
).toBe(false);
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.down('b');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).lastEvent.repeat;
|
|
|
|
})
|
|
|
|
).toBe(true);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.keyboard.up('a');
|
|
|
|
await page.keyboard.down('a');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).lastEvent.repeat;
|
|
|
|
})
|
|
|
|
).toBe(false);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should type all kinds of characters', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
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.evaluate('result')).toBe(text);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should specify location', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
await page.evaluate(() => {
|
2020-05-07 10:54:55 +00:00
|
|
|
window.addEventListener(
|
|
|
|
'keydown',
|
2022-06-22 13:25:44 +00:00
|
|
|
event => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return ((globalThis as any).keyLocation = event.location);
|
|
|
|
},
|
2020-05-07 10:54:55 +00:00
|
|
|
true
|
|
|
|
);
|
2018-11-21 03:43:07 +00:00
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const textarea = (await page.$('textarea'))!;
|
2019-02-27 23:13:17 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await textarea.press('Digit5');
|
|
|
|
expect(await page.evaluate('keyLocation')).toBe(0);
|
2019-02-27 23:13:17 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await textarea.press('ControlLeft');
|
|
|
|
expect(await page.evaluate('keyLocation')).toBe(1);
|
2019-02-27 23:13:17 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await textarea.press('ControlRight');
|
|
|
|
expect(await page.evaluate('keyLocation')).toBe(2);
|
|
|
|
|
|
|
|
await textarea.press('NumpadSubtract');
|
|
|
|
expect(await page.evaluate('keyLocation')).toBe(3);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw on unknown keys', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
let error = await page.keyboard
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error bad input
|
2020-05-07 10:54:55 +00:00
|
|
|
.press('NotARealKey')
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(error.message).toBe('Unknown key: "NotARealKey"');
|
|
|
|
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error bad input
|
2022-06-22 13:25:44 +00:00
|
|
|
error = await page.keyboard.press('ё').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(error && error.message).toBe('Unknown key: "ё"');
|
|
|
|
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error bad input
|
2022-06-22 13:25:44 +00:00
|
|
|
error = await page.keyboard.press('😊').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(error && error.message).toBe('Unknown key: "😊"');
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should type emoji', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
|
|
|
await page.type('textarea', '👹 Tokyo street Japan 🇯🇵');
|
2020-07-02 09:09:34 +00:00
|
|
|
expect(
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.$eval('textarea', textarea => {
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
return textarea.value;
|
2022-06-15 10:09:22 +00:00
|
|
|
})
|
2020-07-02 09:09:34 +00:00
|
|
|
).toBe('👹 Tokyo street Japan 🇯🇵');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should type emoji into an iframe', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await utils.attachFrame(
|
|
|
|
page,
|
|
|
|
'emoji-test',
|
|
|
|
server.PREFIX + '/input/textarea.html'
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
const frame = page.frames()[1]!;
|
|
|
|
const textarea = (await frame.$('textarea'))!;
|
2020-04-09 05:56:25 +00:00
|
|
|
await textarea.type('👹 Tokyo street Japan 🇯🇵');
|
2020-07-02 09:09:34 +00:00
|
|
|
expect(
|
2022-06-22 13:25:44 +00:00
|
|
|
await frame.$eval('textarea', textarea => {
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
return textarea.value;
|
2022-06-15 10:09:22 +00:00
|
|
|
})
|
2020-07-02 09:09:34 +00:00
|
|
|
).toBe('👹 Tokyo street Japan 🇯🇵');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should press the meta key', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, isFirefox} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.evaluate(() => {
|
2022-06-15 10:09:22 +00:00
|
|
|
(globalThis as any).result = null;
|
2022-06-22 13:25:44 +00:00
|
|
|
document.addEventListener('keydown', event => {
|
2022-06-15 10:09:22 +00:00
|
|
|
(globalThis as any).result = [event.key, event.code, event.metaKey];
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2019-02-27 23:13:17 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.keyboard.press('Meta');
|
2020-07-10 10:52:13 +00:00
|
|
|
// Have to do this because we lose a lot of type info when evaluating a
|
|
|
|
// string not a function. This is why functions are recommended rather than
|
|
|
|
// using strings (although we'll leave this test so we have coverage of both
|
|
|
|
// approaches.)
|
|
|
|
const [key, code, metaKey] = (await page.evaluate('result')) as [
|
|
|
|
string,
|
|
|
|
string,
|
|
|
|
boolean
|
|
|
|
];
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isFirefox && os.platform() !== 'darwin') {
|
|
|
|
expect(key).toBe('OS');
|
|
|
|
} else {
|
|
|
|
expect(key).toBe('Meta');
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isFirefox) {
|
|
|
|
expect(code).toBe('OSLeft');
|
|
|
|
} else {
|
|
|
|
expect(code).toBe('MetaLeft');
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isFirefox && os.platform() !== 'darwin') {
|
|
|
|
expect(metaKey).toBe(false);
|
|
|
|
} else {
|
|
|
|
expect(metaKey).toBe(true);
|
|
|
|
}
|
2018-11-21 03:43:07 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|