Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option: autoFocusDocumentBeforeTyping to reduce real browser testing flakiness #1092

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/keyboard/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {fireEvent} from '@testing-library/dom'
import type {Instance} from '../setup'
import type {keyboardKey} from '../system/keyboard'
import {wait} from '../utils'
import {getActiveElementOrBody, wait} from '../utils'
import {parseKeyDef} from './parseKeyDef'

interface KeyboardAction {
Expand All @@ -11,6 +12,9 @@ interface KeyboardAction {
}

export async function keyboard(this: Instance, text: string): Promise<void> {
if (this.config.autoFocusDocumentBeforeTyping) {
fireEvent.focus(getActiveElementOrBody(this.config.document))
}
const actions: KeyboardAction[] = parseKeyDef(this.config.keyboardMap, text)

for (let i = 0; i < actions.length; i++) {
Expand Down
10 changes: 10 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export interface Options {
*/
autoModify?: boolean

/**
* Automatically focus the active document before interacting with the keyboard.
*
* Recommended to enable it whenever the test is running against a real web browser and
* a real user interaction may interfere with the test.
*
* @default false
*/
autoFocusDocumentBeforeTyping?: boolean

/**
* Between some subsequent inputs like typing a series of characters
* the code execution is delayed per `setTimeout` for (at least) `delay` seconds.
Expand Down
1 change: 1 addition & 0 deletions src/setup/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {DirectOptions} from './directApi'
const defaultOptionsDirect: Required<Options> = {
applyAccept: true,
autoModify: true,
autoFocusDocumentBeforeTyping: false,
delay: 0,
document: globalThis.document,
keyboardMap: defaultKeyboardMap,
Expand Down