From 7326bff1fe8d9cc94fd7b87b377e02d7fb3f1daa Mon Sep 17 00:00:00 2001 From: lindagruenwald Date: Fri, 22 Sep 2023 09:41:17 +0200 Subject: [PATCH] Send correct inputType when typing (#244) --- lib/capybara/cuprite/javascripts/index.js | 3 +-- spec/features/driver_spec.rb | 5 +++++ spec/support/views/input_events.erb | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/capybara/cuprite/javascripts/index.js b/lib/capybara/cuprite/javascripts/index.js index 91708925..e1af821f 100644 --- a/lib/capybara/cuprite/javascripts/index.js +++ b/lib/capybara/cuprite/javascripts/index.js @@ -180,8 +180,7 @@ class Cuprite { } input(node) { - let event = document.createEvent("HTMLEvents"); - event.initEvent("input", true, false); + let event = new InputEvent("input", { inputType: "insertText", bubbles: true, cancelable: false }); node.dispatchEvent(event); } diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index 073cafc9..8602f6fb 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -1470,6 +1470,11 @@ def create_screenshot(file, *args) expect(input.value).to eq("abc") end + it "uses the 'insertText' inputType for input event" do + input.set("a") + expect(output["data-input-type"]).to eq("insertText") + end + it "doesn't call the change event if there is no change" do input.set("a") input.set("a") diff --git a/spec/support/views/input_events.erb b/spec/support/views/input_events.erb index 8b1cfdb2..d35219d4 100644 --- a/spec/support/views/input_events.erb +++ b/spec/support/views/input_events.erb @@ -9,7 +9,10 @@ const log = (s) => output.textContent = `${output.textContent} ${s}` input.addEventListener('keydown', () => log('keydown')) input.addEventListener('keypress', () => log('keypress')) - input.addEventListener('input', () => log('input')) + input.addEventListener('input', (event) => { + log('input'); + output.dataset.inputType = event.inputType; + }) input.addEventListener('keyup', () => log('keyup')) input.addEventListener('change', () => log('change'))