Skip to content

Commit

Permalink
detect if element to be acted on is actually on top/viewable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Ong committed Nov 8, 2023
1 parent 3940696 commit 96d3248
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/capybara/cuprite/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions spec/features/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions spec/support/views/click_overlay.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
width: 800px;
margin: 0;
}
#hidden-link {
position: relative;
top: 740px;
}
#wrapper {
height: 1200px;
}
#footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: red;
color: white;
text-align: center;
height: 100px;
}
</style>
<script type="text/javascript">
window.onload = function() {
var log = document.querySelector("#log")
var boxes = document.querySelectorAll("#hidden-link")
for (var i = 0; i < boxes.length; i++) {
var el = boxes[i]
el.onclick = function() {
log.textContent = this.id
}
}
}
</script>
</head>
<body>
<div id="log"></div>
<div id="wrapper">
<a id="hidden-link" href="#">hidden link</a>
</div>
<div id="footer"></div>
</body>
</html>

0 comments on commit 96d3248

Please sign in to comment.