Skip to content

Commit

Permalink
chore: implement BiDi sendCharacter
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Sep 22, 2023
1 parent f21e903 commit dfb0aab
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
30 changes: 9 additions & 21 deletions packages/puppeteer-core/src/api/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export type KeyPressOptions = KeyDownOptions & KeyboardTypeOptions;
*
* @public
*/
export class Keyboard {
export abstract class Keyboard {
/**
* @internal
*/
Expand Down Expand Up @@ -120,10 +120,10 @@ export class Keyboard {
* is the commands of keyboard shortcuts,
* see {@link https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/editing/commands/editor_command_names.h | Chromium Source Code} for valid command names.
*/
async down(key: KeyInput, options?: Readonly<KeyDownOptions>): Promise<void>;
async down(): Promise<void> {
throw new Error('Not implemented');
}
abstract down(
key: KeyInput,
options?: Readonly<KeyDownOptions>
): Promise<void>;

/**
* Dispatches a `keyup` event.
Expand All @@ -132,10 +132,7 @@ export class Keyboard {
* See {@link KeyInput | KeyInput}
* for a list of all key names.
*/
async up(key: KeyInput): Promise<void>;
async up(): Promise<void> {
throw new Error('Not implemented');
}
abstract up(key: KeyInput): Promise<void>;

/**
* Dispatches a `keypress` and `input` event.
Expand All @@ -153,10 +150,7 @@ export class Keyboard {
*
* @param char - Character to send into the page.
*/
async sendCharacter(char: string): Promise<void>;
async sendCharacter(): Promise<void> {
throw new Error('Not implemented');
}
abstract sendCharacter(char: string): Promise<void>;

/**
* Sends a `keydown`, `keypress`/`input`,
Expand All @@ -181,13 +175,10 @@ export class Keyboard {
* if specified, is the time to wait between `keydown` and `keyup` in milliseconds.
* Defaults to 0.
*/
async type(
abstract type(
text: string,
options?: Readonly<KeyboardTypeOptions>
): Promise<void>;
async type(): Promise<void> {
throw new Error('Not implemented');
}

/**
* Shortcut for {@link Keyboard.down}
Expand All @@ -211,13 +202,10 @@ export class Keyboard {
* is the commands of keyboard shortcuts,
* see {@link https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/editing/commands/editor_command_names.h | Chromium Source Code} for valid command names.
*/
async press(
abstract press(
key: KeyInput,
options?: Readonly<KeyPressOptions>
): Promise<void>;
async press(): Promise<void> {
throw new Error('Not implemented');
}
}

/**
Expand Down
11 changes: 10 additions & 1 deletion packages/puppeteer-core/src/bidi/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {type Point} from '../api/ElementHandle.js';
import {
Keyboard,
Mouse,
MouseButton,
Touchscreen,
type KeyDownOptions,
type KeyPressOptions,
type KeyboardTypeOptions,
MouseButton,
type MouseClickOptions,
type MouseMoveOptions,
type MouseOptions,
Expand Down Expand Up @@ -415,6 +415,15 @@ export class BidiKeyboard extends Keyboard {
],
});
}

override async sendCharacter(char: string): Promise<void> {
if ([...char].length > 1) {
throw new Error('Cannot send more than 1 character.');
}
await this.#context.evaluate(async char => {
document.execCommand('insertText', false, char);
}, char);
}
}

/**
Expand Down
6 changes: 0 additions & 6 deletions test/TestExpectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,6 @@
"parameters": ["webDriverBiDi"],
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[keyboard.spec] Keyboard should send a character with sendCharacter",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Browser.disconnect should reject navigation when browser closes",
"platforms": ["darwin", "linux", "win32"],
Expand Down
44 changes: 32 additions & 12 deletions test/src/keyboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,27 +146,47 @@ describe('Keyboard', function () {

await page.goto(server.PREFIX + '/input/textarea.html');
await page.focus('textarea');
await page.keyboard.sendCharacter('嗨');
expect(
await page.evaluate(() => {
return document.querySelector('textarea')!.value;
})
).toBe('嗨');

await page.evaluate(() => {
return window.addEventListener(
(globalThis as any).inputCount = 0;
(globalThis as any).keyDownCount = 0;
window.addEventListener(
'input',
() => {
(globalThis as any).inputCount += 1;
},
true
);
window.addEventListener(
'keydown',
e => {
return e.preventDefault();
() => {
(globalThis as any).keyDownCount += 1;
},
true
);
});

await page.keyboard.sendCharacter('嗨');
expect(
await page.$eval('textarea', textarea => {
return {
value: textarea.value,
inputs: (globalThis as any).inputCount,
keyDowns: (globalThis as any).keyDownCount,
};
})
).toMatchObject({value: '嗨', inputs: 1, keyDowns: 0});

await page.keyboard.sendCharacter('a');
expect(
await page.evaluate(() => {
return document.querySelector('textarea')!.value;
await page.$eval('textarea', textarea => {
return {
value: textarea.value,
inputs: (globalThis as any).inputCount,
keyDowns: (globalThis as any).keyDownCount,
};
})
).toBe('嗨a');
).toMatchObject({value: '嗨a', inputs: 2, keyDowns: 0});
});
it('should report shiftKey', async () => {
const {page, server} = await getTestState();
Expand Down

0 comments on commit dfb0aab

Please sign in to comment.