From 972e89768c677ce6c1274effd849b22252d5d4f7 Mon Sep 17 00:00:00 2001 From: David Runger Date: Sun, 25 Aug 2024 13:19:08 -0500 Subject: [PATCH] Generate a FocusEvent (not Event) for focus-related events --- lib/capybara/cuprite/javascripts/index.js | 3 ++- spec/features/session_spec.rb | 4 ++-- spec/support/public/test.js | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/capybara/cuprite/javascripts/index.js b/lib/capybara/cuprite/javascripts/index.js index 81bc8db8..dec7f09c 100644 --- a/lib/capybara/cuprite/javascripts/index.js +++ b/lib/capybara/cuprite/javascripts/index.js @@ -416,7 +416,8 @@ class Cuprite { options["button"] || 0, null ) } else if (EVENTS.FOCUS.indexOf(name) != -1) { - event = this.obtainEvent(name); + event = document.createEvent("FocusEvent"); + event.initEvent(name, true, true); } else if (EVENTS.FORM.indexOf(name) != -1) { event = this.obtainEvent(name); } else { diff --git a/spec/features/session_spec.rb b/spec/features/session_spec.rb index db0e640d..bbcdbdde 100644 --- a/spec/features/session_spec.rb +++ b/spec/features/session_spec.rb @@ -200,11 +200,11 @@ end it "fires the focus event" do - expect(@session.find(:css, "#changes_on_focus").text).to eq("Focus") + expect(@session.find(:css, "#changes_on_focus").text).to eq("Focus (FocusEvent)") end it "fires the blur event" do - expect(@session.find(:css, "#changes_on_blur").text).to eq("Blur") + expect(@session.find(:css, "#changes_on_blur").text).to eq("Blur (FocusEvent)") end it "fires the keydown event before the value is updated" do diff --git a/spec/support/public/test.js b/spec/support/public/test.js index 7b11808e..9bf8a108 100644 --- a/spec/support/public/test.js +++ b/spec/support/public/test.js @@ -28,10 +28,10 @@ $(function() { $("#changes_on_keypress").text(increment) }) .focus(function(event) { - $("#changes_on_focus").text("Focus") + $("#changes_on_focus").text(`Focus (${event.originalEvent.constructor.name})`) }) - .blur(function() { - $("#changes_on_blur").text("Blur") + .blur(function(event) { + $("#changes_on_blur").text(`Blur (${event.originalEvent.constructor.name})`) }) $("#browser")