Skip to content

Commit

Permalink
Allow fill_in to work with reactjs
Browse files Browse the repository at this point in the history
React overrides the native setter of html input elements, so it doesn't fire change events when the value is changed. Because of that, for those input types where capybara uses javascript to change the value, change events are not fired as you'd expect when using react.

I've updated the code to use the un-overridden value setter instead as recommended here:

https://stackoverflow.com/a/46012210

I believe this shouldn't adversely affect apps using other javascript frameworks or no frameworks at all
  • Loading branch information
iainbeeston authored Mar 13, 2023
1 parent f857a99 commit b30b604
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/capybara/selenium/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ def update_value_js(value)
arguments[0].focus();
}
if (arguments[0].value != arguments[1]) {
arguments[0].value = arguments[1]
var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
nativeInputValueSetter.call(arguments[0], arguments[1]);
arguments[0].dispatchEvent(new InputEvent('input'));
arguments[0].dispatchEvent(new Event('change', { bubbles: true }));
}
Expand Down

0 comments on commit b30b604

Please sign in to comment.