Skip to content

Commit eb20a3a

Browse files
authored
fix(browser): focus the iframe before clicking in playwright (#11798)
1 parent e72fa3e commit eb20a3a

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

‎packages/browser-playwright/src/commands/click.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { UserEvent } from 'vitest/browser'
22
import type { UserEventCommand } from './utils'
3-
import { getDescribedLocator } from './utils'
3+
import { focusIframe, getDescribedLocator } from './utils'
44

55
export const click: UserEventCommand<UserEvent['click']> = async (
66
context,
77
selector,
88
options = {},
99
) => {
10+
await focusIframe(context)
1011
await getDescribedLocator(context, selector).click(options)
1112
}
1213

@@ -15,6 +16,7 @@ export const dblClick: UserEventCommand<UserEvent['dblClick']> = async (
1516
selector,
1617
options = {},
1718
) => {
19+
await focusIframe(context)
1820
await getDescribedLocator(context, selector).dblclick(options)
1921
}
2022

@@ -23,6 +25,7 @@ export const tripleClick: UserEventCommand<UserEvent['tripleClick']> = async (
2325
selector,
2426
options = {},
2527
) => {
28+
await focusIframe(context)
2629
await getDescribedLocator(context, selector).click({
2730
...options,
2831
clickCount: 3,

‎packages/browser-playwright/src/commands/keyboard.ts‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { BrowserProvider } from 'vitest/node'
22
import type { PlaywrightBrowserProvider } from '../playwright'
33
import type { UserEventCommand } from './utils'
44
import { parseKeyDef } from '@vitest/browser'
5+
import { focusIframe } from './utils'
56

67
export interface KeyboardState {
78
unreleased: string[]
@@ -10,8 +11,7 @@ export interface KeyboardState {
1011
export const keyboard: UserEventCommand<
1112
(text: string, state: KeyboardState) => Promise<{ unreleased: string[] }>
1213
> = async (context, text, state) => {
13-
const frame = await context.frame()
14-
await frame.evaluate(focusIframe)
14+
await focusIframe(context)
1515

1616
const pressed = new Set<string>(state.unreleased)
1717

@@ -319,16 +319,6 @@ export async function keyboardImplementation(
319319
}
320320
}
321321

322-
function focusIframe() {
323-
if (
324-
!document.activeElement ||
325-
document.activeElement.ownerDocument !== document ||
326-
document.activeElement === document.body
327-
) {
328-
window.focus()
329-
}
330-
}
331-
332322
function selectAll() {
333323
const element = document.activeElement as HTMLInputElement
334324
if (element && typeof element.select === 'function') {

‎packages/browser-playwright/src/commands/utils.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ type ConvertUserEventParameters<T extends unknown[]> = {
1212
[K in keyof T]: ConvertElementToLocator<T[K]>
1313
}
1414

15+
export async function focusIframe(context: BrowserCommandContext): Promise<void> {
16+
const frame = await context.frame()
17+
await frame.evaluate(() => {
18+
if (
19+
!document.activeElement ||
20+
document.activeElement.ownerDocument !== document ||
21+
document.activeElement === document.body
22+
) {
23+
window.focus()
24+
}
25+
})
26+
}
27+
1528
// strip iframe locator part from the trace description e.g.
1629
// - locator('[data-vitest="true"]').contentFrame().getByRole('button')
1730
// ⇓

0 commit comments

Comments
 (0)