fix(codegen): generate no Enter keyboard events for textareas (#23775)

Fixes https://github.com/microsoft/playwright/issues/23774
This commit is contained in:
Max Schmitt 2023-06-19 17:07:37 +02:00 committed by GitHub
parent e171194c86
commit 0f9f863183
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View file

@ -315,6 +315,9 @@ export class Recorder {
}
private _shouldGenerateKeyPressFor(event: KeyboardEvent): boolean {
// Enter aka. new line is handled in input event.
if (event.key === 'Enter' && (this._deepEventTarget(event).nodeName === 'TEXTAREA' || this._deepEventTarget(event).isContentEditable))
return false;
// Backspace, Delete, AltGraph are changing input, will handle it there.
if (['Backspace', 'Delete', 'AltGraph'].includes(event.key))
return false;

View file

@ -305,6 +305,21 @@ test.describe('cli codegen', () => {
expect(message.text()).toBe('John');
});
test('should fill textarea with new lines at the end', async ({ page, openRecorder }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/23774' });
const recorder = await openRecorder();
await recorder.setContentAndWait(`<textarea id="textarea"></textarea>`);
const textarea = page.locator('textarea');
await textarea.evaluate<void, HTMLTextAreaElement>(e => e.addEventListener('input', () => (window as any).lastInputValue = e.value));
const waitForOutputPromise = recorder.waitForOutput('JavaScript', 'Hello\\n');
await textarea.type('Hello\n');
// Issue was that the input event was not fired for the last newline, so we check for that.
await page.waitForFunction(() => (window as any).lastInputValue === 'Hello\n');
const sources = await waitForOutputPromise;
expect(sources.get('JavaScript').text).toContain(`await page.locator('#textarea').fill('Hello\\n');`);
expect(sources.get('JavaScript').text).not.toContain(`Enter`);
});
test('should fill [contentEditable]', async ({ page, openRecorder }) => {
const recorder = await openRecorder();