File tree Expand file tree Collapse file tree
packages/browser-playwright/src/commands Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import type { UserEvent } from 'vitest/browser'
22import type { UserEventCommand } from './utils'
3- import { getDescribedLocator } from './utils'
3+ import { focusIframe , getDescribedLocator } from './utils'
44
55export 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 ,
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import type { BrowserProvider } from 'vitest/node'
22import type { PlaywrightBrowserProvider } from '../playwright'
33import type { UserEventCommand } from './utils'
44import { parseKeyDef } from '@vitest/browser'
5+ import { focusIframe } from './utils'
56
67export interface KeyboardState {
78 unreleased : string [ ]
@@ -10,8 +11,7 @@ export interface KeyboardState {
1011export 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-
332322function selectAll ( ) {
333323 const element = document . activeElement as HTMLInputElement
334324 if ( element && typeof element . select === 'function' ) {
Original file line number Diff line number Diff 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// ⇓
You can’t perform that action at this time.
0 commit comments