Skip to content

Commit 281deb7

Browse files
committed
feat(keyboard): keydown supports commands
Issue: puppeteer#1313
1 parent 3cdd5d8 commit 281deb7

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

docs/api/puppeteer.keyboard.down.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ class Keyboard {
1414
key: KeyInput,
1515
options?: {
1616
text?: string;
17+
commands?: string[];
1718
}
1819
): Promise<void>;
1920
}
2021
```
2122

2223
## Parameters
2324

24-
| Parameter | Type | Description |
25-
| --------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
26-
| key | [KeyInput](./puppeteer.keyinput.md) | Name of key to press, such as <code>ArrowLeft</code>. See [KeyInput](./puppeteer.keyinput.md) for a list of all key names. |
27-
| options | { text?: string; } | <i>(Optional)</i> An object of options. Accepts text which, if specified, generates an input event with this text. |
25+
| Parameter | Type | Description |
26+
| --------- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
27+
| key | [KeyInput](./puppeteer.keyinput.md) | Name of key to press, such as <code>ArrowLeft</code>. See [KeyInput](./puppeteer.keyinput.md) for a list of all key names. |
28+
| options | { text?: string; commands?: string\[\]; } | <i>(Optional)</i> An object of options. Accepts text which, if specified, generates an input event with this text. Accepts commands which, if specified, is the commands of keyboard shortcuts, see [editor_command_names.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/editing/commands/editor_command_names.h) for valid command names. |
2829

2930
**Returns:**
3031

docs/api/puppeteer.keyboard.press.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ class Keyboard {
1515
options?: {
1616
delay?: number;
1717
text?: string;
18+
commands?: string[];
1819
}
1920
): Promise<void>;
2021
}
2122
```
2223

2324
## Parameters
2425

25-
| Parameter | Type | Description |
26-
| --------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
27-
| key | [KeyInput](./puppeteer.keyinput.md) | Name of key to press, such as <code>ArrowLeft</code>. See [KeyInput](./puppeteer.keyinput.md) for a list of all key names. |
28-
| options | { delay?: number; text?: string; } | <i>(Optional)</i> An object of options. Accepts text which, if specified, generates an input event with this text. Accepts delay which, if specified, is the time to wait between <code>keydown</code> and <code>keyup</code> in milliseconds. Defaults to 0. |
26+
| Parameter | Type | Description |
27+
| --------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
28+
| key | [KeyInput](./puppeteer.keyinput.md) | Name of key to press, such as <code>ArrowLeft</code>. See [KeyInput](./puppeteer.keyinput.md) for a list of all key names. |
29+
| options | { delay?: number; text?: string; commands?: string\[\]; } | <i>(Optional)</i> An object of options. Accepts text which, if specified, generates an input event with this text. Accepts delay which, if specified, is the time to wait between <code>keydown</code> and <code>keyup</code> in milliseconds. Defaults to 0. Accepts commands which, if specified, is the commands of keyboard shortcuts, see [editor_command_names.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/editing/commands/editor_command_names.h) for valid command names. |
2930

3031
**Returns:**
3132

packages/puppeteer-core/src/common/Input.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ export class Keyboard {
108108
*/
109109
async down(
110110
key: KeyInput,
111-
options: {text?: string} = {text: undefined}
111+
options: {text?: string; commands?: string[]} = {
112+
text: undefined,
113+
commands: [],
114+
}
112115
): Promise<void> {
113116
const description = this.#keyDescriptionForString(key);
114117

@@ -128,6 +131,7 @@ export class Keyboard {
128131
autoRepeat,
129132
location: description.location,
130133
isKeypad: description.location === 3,
134+
commands: options.commands,
131135
});
132136
}
133137

@@ -308,7 +312,7 @@ export class Keyboard {
308312
*/
309313
async press(
310314
key: KeyInput,
311-
options: {delay?: number; text?: string} = {}
315+
options: {delay?: number; text?: string; commands?: string[]} = {}
312316
): Promise<void> {
313317
const {delay = null} = options;
314318
await this.down(key, options);

test/TestExpectations.json

+6
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,12 @@
569569
"parameters": ["firefox"],
570570
"expectations": ["SKIP"]
571571
},
572+
{
573+
"testIdPattern": "[keyboard.spec] Keyboard should trigger commands of keyboard shortcuts",
574+
"platforms": ["darwin", "linux", "win32"],
575+
"parameters": ["firefox"],
576+
"expectations": ["SKIP"]
577+
},
572578
{
573579
"testIdPattern": "[keyboard.spec] Keyboard should report shiftKey",
574580
"platforms": ["darwin", "linux", "win32"],

test/src/keyboard.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,36 @@ describe('Keyboard', function () {
9090
})
9191
).toBe('Hello World!');
9292
});
93+
// @see https://github.com./puppeteer/puppeteer/issues/1313
94+
it('should trigger commands of keyboard shortcuts', async () => {
95+
const {page, server} = getTestState();
96+
const cmdKey = os.platform() !== 'darwin' ? 'Meta' : 'Control';
97+
98+
await page.goto(server.PREFIX + '/input/textarea.html');
99+
await page.type('textarea', 'hello');
100+
101+
await page.keyboard.down(cmdKey);
102+
await page.keyboard.press('a', {commands: ['SelectAll']});
103+
await page.keyboard.up(cmdKey);
104+
105+
await page.keyboard.down(cmdKey);
106+
await page.keyboard.down('c', {commands: ['Copy']});
107+
await page.keyboard.up('c');
108+
await page.keyboard.up(cmdKey);
109+
110+
await page.keyboard.down(cmdKey);
111+
await page.keyboard.press('v', {commands: ['Paste']});
112+
await page.keyboard.up(cmdKey);
113+
await page.keyboard.down(cmdKey);
114+
await page.keyboard.press('v', {commands: ['Paste']});
115+
await page.keyboard.up(cmdKey);
116+
117+
expect(
118+
await page.evaluate(() => {
119+
return document.querySelector('textarea')!.value;
120+
})
121+
).toBe('hellohello');
122+
});
93123
it('should send a character with ElementHandle.press', async () => {
94124
const {page, server} = getTestState();
95125

0 commit comments

Comments
 (0)