Skip to content

Conversation

@pythonandchips
Copy link

@pythonandchips pythonandchips commented Nov 14, 2024

The JS for filling in a field does not focus the element before filling it in, only triggers the focus event. This causes failures when checking the document.activeElement as the input will not be focused.

This fixes the issue by calling focus on the node before triggering the event.

Currently the javascript used to set a field value will trigger
a "focus" event prior to updating a field however this does not
set the input as focused so any use of `document.activeElement`
will return the body element.

To fix this a call to `node.focus()` has been added before the
focus event to ensure `document.activeElement` will respond
correctly.
@dmorgan-fa dmorgan-fa force-pushed the focus-element-when-setting branch from b390fde to 63c4547 Compare February 6, 2025 12:16
@dmorgan-fa
Copy link

Hi @route, I've tidied up this PR to reduce the dependabot changes from our fork.

Is there any chance you're able to review this?
We've been using the fix since late-October '24 (some 14,000+ runs) and it's been reliable for reducing flakey issues.

Thanks!

seanpdoyle added a commit to seanpdoyle/cuprite that referenced this pull request Dec 22, 2025
Closes rubycdp#157
Related to rubycdp#280

Change the `lib/capybara/cuprite/javascripts/index.js` to invoke
[HTMLElement.focus][] directly, rather than triggering a synthetic
`"focus"` event.

[HTMLElement.focus]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus
@seanpdoyle
Copy link
Contributor

I've opened #305, which seems to overlap with this PR. The changeset in #305 is smaller, but its goals are the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants