Skip to content

Add support for global keyboard events and focus-aware testing #5

@CharlonTank

Description

@CharlonTank

Problem

The current testing API can produce false positive test results because it allows tests to bypass browser focus management and perform actions that real users cannot.

Examples of False Positives:

1. Auto-focus is bypassed:

-- My app auto-focuses an input on init
-- But tests can type into ANY field regardless of focus:
client.keyDown sd (Dom.id "other-input") "a" []
-- This passes even though "other-input" has no focus\!

2. Direct message updates bypass UI flow:

-- This bypasses all UI validation/interaction:
client.update sd (SubmitRegistrationForm)

-- What users actually must do:
client.click sd (Dom.id "submit-button")

3. Games with global keyboard subscriptions:

-- Current workaround is verbose:
client.update sd (GameMsg (KeyDown (keyEventFromString "Space")))

-- Would be clearer and more accurate:
client.keyDownGlobal sd "Space"

Proposed Solution

Add global keyboard event methods that respect browser focus state:

type alias FrontendActions =
    { -- existing methods...
    , keyDownGlobal : DelayInMs -> KeyEvent -> List KeyOptions -> Action
    , keyUpGlobal : DelayInMs -> KeyEvent -> List KeyOptions -> Action
    }

These would:

  1. Trigger global keyboard subscriptions when no element has focus
  2. Trigger on the focused element when one exists
  3. Fail if trying to type when no valid target exists

This ensures tests can only perform actions that real users can perform, catching focus-related bugs that currently slip through.

Benefits

  • Eliminates false positives from focus-related bugs
  • Makes game testing more intuitive
  • Ensures tests match real user capabilities
  • Maintains backward compatibility

Would you be open to this enhancement? I'm happy to contribute to the implementation if this aligns with your vision for the library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions