Skip to content

Commit 41ad9e5

Browse files
DavertMikclaude
andcommitted
fix: check raw key array before normalization in pressKey focus check
WebDriver's getNormalizedKey converts key names to Unicode code points, so checking after normalization misses the modifier. Now check the original user-provided key array before any normalization happens. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ccc1e29 commit 41ad9e5

4 files changed

Lines changed: 18 additions & 9 deletions

File tree

lib/helper/Playwright.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,7 @@ class Playwright extends Helper {
22322232
* {{> pressKeyWithKeyNormalization }}
22332233
*/
22342234
async pressKey(key) {
2235+
await checkFocusBeforePressKey(this, key)
22352236
const modifiers = []
22362237
if (Array.isArray(key)) {
22372238
for (let k of key) {
@@ -2246,7 +2247,6 @@ class Playwright extends Helper {
22462247
} else {
22472248
key = getNormalizedKey.call(this, key)
22482249
}
2249-
await checkFocusBeforePressKey(this, modifiers, key)
22502250
for (const modifier of modifiers) {
22512251
await this.page.keyboard.down(modifier)
22522252
}

lib/helper/Puppeteer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,7 @@ class Puppeteer extends Helper {
15481548
* {{> pressKeyWithKeyNormalization }}
15491549
*/
15501550
async pressKey(key) {
1551+
await checkFocusBeforePressKey(this, key)
15511552
const modifiers = []
15521553
if (Array.isArray(key)) {
15531554
for (let k of key) {
@@ -1562,7 +1563,6 @@ class Puppeteer extends Helper {
15621563
} else {
15631564
key = getNormalizedKey.call(this, key)
15641565
}
1565-
await checkFocusBeforePressKey(this, modifiers, key)
15661566
for (const modifier of modifiers) {
15671567
await this.page.keyboard.down(modifier)
15681568
}

lib/helper/WebDriver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,7 @@ class WebDriver extends Helper {
22382238
* {{> pressKeyWithKeyNormalization }}
22392239
*/
22402240
async pressKey(key) {
2241+
await checkFocusBeforePressKey(this, key)
22412242
const modifiers = []
22422243
if (Array.isArray(key)) {
22432244
for (let k of key) {
@@ -2252,7 +2253,6 @@ class WebDriver extends Helper {
22522253
} else {
22532254
key = getNormalizedKey.call(this, key)
22542255
}
2255-
await checkFocusBeforePressKey(this, modifiers, key)
22562256
for (const modifier of modifiers) {
22572257
await this.pressKeyDown(modifier)
22582258
}

lib/helper/extras/focusCheck.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import store from '../../store.js'
22
import NonFocusedType from '../errors/NonFocusedType.js'
33

4+
const MODIFIER_PATTERN = /^(control|ctrl|meta|cmd|command|commandorcontrol|ctrlorcommand)/i
45
const EDITING_KEYS = new Set(['a', 'c', 'x', 'v', 'z', 'y'])
56

67
async function isNoElementFocused(helper) {
@@ -19,16 +20,24 @@ export async function checkFocusBeforeType(helper) {
1920
helper.debugSection('Warning', message)
2021
}
2122

22-
export async function checkFocusBeforePressKey(helper, modifiers, key) {
23+
export async function checkFocusBeforePressKey(helper, originalKey) {
2324
if (!helper.options.strict && !store.debugMode) return
24-
25-
const hasCtrlOrMeta = modifiers.some(m => m === 'Control' || m === 'Meta'
26-
|| m === 'ControlLeft' || m === 'ControlRight' || m === 'MetaLeft' || m === 'MetaRight')
27-
if (!hasCtrlOrMeta || !EDITING_KEYS.has(key.toLowerCase())) return
25+
if (!Array.isArray(originalKey)) return
26+
27+
let hasCtrlOrMeta = false
28+
let actionKey = null
29+
for (const k of originalKey) {
30+
if (MODIFIER_PATTERN.test(k)) {
31+
hasCtrlOrMeta = true
32+
} else {
33+
actionKey = k
34+
}
35+
}
36+
if (!hasCtrlOrMeta || !actionKey || !EDITING_KEYS.has(actionKey.toLowerCase())) return
2837

2938
if (!await isNoElementFocused(helper)) return
3039

31-
const message = `No element is in focus. Key combination with "${key}" may not work as expected. Use I.click() or I.focus() first.`
40+
const message = `No element is in focus. Key combination with "${originalKey.join('+')}" may not work as expected. Use I.click() or I.focus() first.`
3241
if (helper.options.strict) throw new NonFocusedType(message)
3342
helper.debugSection('Warning', message)
3443
}

0 commit comments

Comments
 (0)