Skip to content

Commit 2733247

Browse files
authored
fix: throw on locator.press with unknown key (#36703)
1 parent beffe02 commit 2733247

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

packages/playwright-core/src/server/input.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { assert } from '../utils';
1818
import * as keyboardLayout from './usKeyboardLayout';
19+
import { NonRecoverableDOMError } from './dom';
1920

2021
import type { Progress } from './progress';
2122
import type { Page } from './page';
@@ -64,7 +65,8 @@ export class Keyboard {
6465
private _keyDescriptionForString(str: string): KeyDescription {
6566
const keyString = resolveSmartModifierString(str);
6667
let description = usKeyboardLayout.get(keyString);
67-
assert(description, `Unknown key: "${keyString}"`);
68+
if (!description)
69+
throw new NonRecoverableDOMError(`Unknown key: "${keyString}"`);
6870
const shift = this._pressedModifiers.has('Shift');
6971
description = shift && description.shifted ? description.shifted : description;
7072

tests/page/locator-misc-2.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,11 @@ it('should fill programmatically enabled textarea', { annotation: { type: 'issue
201201
await page.locator('#text').fill('Hello');
202202
await expect(page.locator('#text')).toHaveValue('Hello');
203203
});
204+
205+
it('press should throw on unknown keys', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36697' } }, async ({ page, server }) => {
206+
await page.setContent(`<input type='text' value='hello' />`);
207+
const locator = page.getByRole('textbox');
208+
await expect(locator.press('NotARealKey')).rejects.toThrowError(/Unknown key: "NotARealKey"/);
209+
await expect(locator.press('ё')).rejects.toThrowError(/Unknown key: "ё"/);
210+
await expect(locator.press('😊')).rejects.toThrowError(/Unknown key: "😊"/);
211+
});

0 commit comments

Comments
 (0)