From 478fcbfaac0a7a6bc28d4bd1d768e41e2f8b9260 Mon Sep 17 00:00:00 2001 From: Ryan Ong Date: Fri, 2 Dec 2022 11:05:28 -0500 Subject: [PATCH] detect if element to be acted on is actually on top/viewable --- lib/capybara/cuprite/javascripts/index.js | 16 ++++++-- spec/features/driver_spec.rb | 6 +++ spec/support/views/click_overlay.erb | 47 +++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 spec/support/views/click_overlay.erb diff --git a/lib/capybara/cuprite/javascripts/index.js b/lib/capybara/cuprite/javascripts/index.js index 2bab946..47648f6 100644 --- a/lib/capybara/cuprite/javascripts/index.js +++ b/lib/capybara/cuprite/javascripts/index.js @@ -344,10 +344,18 @@ class Cuprite { _isInViewport(node) { let rect = node.getBoundingClientRect(); - return rect.top >= 0 && - rect.left >= 0 && - rect.bottom <= window.innerHeight && - rect.right <= window.innerWidth; + + let inViewport = rect.top >= 0 && + rect.left >= 0 && + rect.bottom <= window.innerHeight && + rect.right <= window.innerWidth; + + if (inViewport) { + let topElement = window.elementFromPoint(x,y); + return topElement == node; + } + + return false; } select(node, value) { diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index 1fcd39e..dce0c42 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -514,6 +514,12 @@ def create_screenshot(file, *args) expect(@driver.body).to include("x: 100, y: 150") end + it "supports clicking overlayed elements" do + @session.visit("/cuprite/click_overlay") + @session.click_link "hidden link" + expect(@driver.body).to include("hidden-link") + end + it "supports executing multiple lines of javascript" do @driver.execute_script <<-JS var a = 1 diff --git a/spec/support/views/click_overlay.erb b/spec/support/views/click_overlay.erb new file mode 100644 index 0000000..4ec9177 --- /dev/null +++ b/spec/support/views/click_overlay.erb @@ -0,0 +1,47 @@ + + + + + + + +
+
+ hidden link +
+ + +