Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(keyboard): event order for Escape key (#27711)

Max Schmittb1325c920830
diff --git a/packages/playwright-core/src/server/input.ts b/packages/playwright-core/src/server/input.ts
index 149c720dbb..973743f115 100644
--- a/packages/playwright-core/src/server/input.ts
+++ b/packages/playwright-core/src/server/input.ts
@@ -114,19 +114,15 @@ export class Keyboard {
}
const tokens = split(key);
- const promises = [];
key = tokens[tokens.length - 1];
for (let i = 0; i < tokens.length - 1; ++i)
- promises.push(this.down(tokens[i]));
- promises.push(this.down(key));
- if (options.delay) {
- await Promise.all(promises);
+ await this.down(tokens[i]);
+ await this.down(key);
+ if (options.delay)
await new Promise(f => setTimeout(f, options.delay));
- }
- promises.push(this.up(key));
+ await this.up(key);
for (let i = tokens.length - 2; i >= 0; --i)
- promises.push(this.up(tokens[i]));
- await Promise.all(promises);
+ await this.up(tokens[i]);
}
async _ensureModifiers(modifiers: types.KeyboardModifier[]): Promise<types.KeyboardModifier[]> {
@@ -135,16 +131,14 @@ export class Keyboard {
throw new Error('Unknown modifier ' + modifier);
}
const restore: types.KeyboardModifier[] = Array.from(this._pressedModifiers);
- const promises: Promise<void>[] = [];
for (const key of kModifiers) {
const needDown = modifiers.includes(key);
const isDown = this._pressedModifiers.has(key);
if (needDown && !isDown)
- promises.push(this.down(key));
+ await this.down(key);
else if (!needDown && isDown)
- promises.push(this.up(key));
+ await this.up(key);
}
- await Promise.all(promises);
return restore;
}
diff --git a/tests/page/page-keyboard.spec.ts b/tests/page/page-keyboard.spec.ts
index 07897d4a92..d0a0fd1ff3 100644
--- a/tests/page/page-keyboard.spec.ts
+++ b/tests/page/page-keyboard.spec.ts
@@ -702,3 +702,14 @@ it('should type after context menu was opened', async ({ server, page, browserNa
await expect.poll(() => page.evaluate('window.keys')).toEqual(['ArrowDown']);
});
+
+it('should have correct Keydown/Keyup order when pressing Escape key', async ({ page, server, browserName }) => {
+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/27709' });
+
+ await page.goto(server.PREFIX + '/input/keyboard.html');
+ await page.keyboard.press('Escape');
+ expect(await page.evaluate('getResult()')).toBe(`
+Keydown: Escape Escape 27 []
+Keyup: Escape Escape 27 []
+`.trim());
+});