Skip to content

Commit

Permalink
feat(keyboard): keydown supports commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Junyan committed Dec 3, 2022
1 parent 3cdd5d8 commit b5a64ce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/puppeteer-core/src/common/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export class Keyboard {
*/
async down(
key: KeyInput,
options: {text?: string} = {text: undefined}
options: {text?: string; commands?: string[]} = {
text: undefined,
commands: [],
}
): Promise<void> {
const description = this.#keyDescriptionForString(key);

Expand All @@ -128,6 +131,7 @@ export class Keyboard {
autoRepeat,
location: description.location,
isKeypad: description.location === 3,
commands: options.commands,
});
}

Expand Down Expand Up @@ -308,7 +312,7 @@ export class Keyboard {
*/
async press(
key: KeyInput,
options: {delay?: number; text?: string} = {}
options: {delay?: number; text?: string; commands?: string[]} = {}
): Promise<void> {
const {delay = null} = options;
await this.down(key, options);
Expand Down
6 changes: 6 additions & 0 deletions test/TestExpectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@
"parameters": ["firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[keyboard.spec] Keyboard should trigger commands of keyboard shortcuts",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[keyboard.spec] Keyboard should report shiftKey",
"platforms": ["darwin", "linux", "win32"],
Expand Down
25 changes: 25 additions & 0 deletions test/src/keyboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ describe('Keyboard', function () {
})
).toBe('Hello World!');
});
// @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.press('c', {commands: ['Copy']});
await page.keyboard.up(cmdKey);
await page.keyboard.down(cmdKey);
await page.keyboard.press('c', {commands: ['Paste']});
await page.keyboard.up(cmdKey);
await page.keyboard.down(cmdKey);
await page.keyboard.press('c', {commands: ['Paste']});
await page.keyboard.up(cmdKey);
expect(
await page.evaluate(() => {
return document.querySelector('textarea')!.value;
})
).toBe('hellohello');
});
it('should send a character with ElementHandle.press', async () => {
const {page, server} = getTestState();

Expand Down

0 comments on commit b5a64ce

Please sign in to comment.