Skip to content

Commit aa5b70c

Browse files
authored
detect if element to be acted on is actually on top/viewable (#251)
1 parent 696b7ef commit aa5b70c

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

lib/capybara/cuprite/javascripts/index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class Cuprite {
8484
return true;
8585
}
8686

87-
8887
isDisabled(node) {
8988
let xpath = "parent::optgroup[@disabled] | \
9089
ancestor::select[@disabled] | \
@@ -344,10 +343,24 @@ class Cuprite {
344343

345344
_isInViewport(node) {
346345
let rect = node.getBoundingClientRect();
347-
return rect.top >= 0 &&
348-
rect.left >= 0 &&
349-
rect.bottom <= window.innerHeight &&
350-
rect.right <= window.innerWidth;
346+
347+
let inViewport = rect.top >= 0 &&
348+
rect.left >= 0 &&
349+
rect.bottom <= window.innerHeight &&
350+
rect.right <= window.innerWidth;
351+
352+
if (inViewport) {
353+
// check if obscured by another element
354+
let x = rect.width/2;
355+
let y = rect.height/2 ;
356+
357+
let px = rect.left + x,
358+
py = rect.top + y,
359+
e = document.elementFromPoint(px, py);
360+
return node == e;
361+
}
362+
363+
return false;
351364
}
352365

353366
select(node, value) {

spec/features/driver_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ def create_screenshot(file, *args)
514514
expect(@driver.body).to include("x: 100, y: 150")
515515
end
516516

517+
it "supports clicking overlayed elements" do
518+
@session.visit("/cuprite/click_overlay")
519+
@session.click_link "hidden link"
520+
expect(@driver.body).to include("hidden-link")
521+
end
522+
517523
it "supports executing multiple lines of javascript" do
518524
@driver.execute_script <<-JS
519525
var a = 1

spec/support/views/click_overlay.erb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style type="text/css">
5+
body {
6+
width: 800px;
7+
margin: 0;
8+
}
9+
#hidden-link {
10+
position: relative;
11+
top: 740px;
12+
}
13+
#wrapper {
14+
height: 1200px;
15+
}
16+
#footer {
17+
position: fixed;
18+
left: 0;
19+
bottom: 0;
20+
width: 100%;
21+
background-color: red;
22+
color: white;
23+
text-align: center;
24+
height: 100px;
25+
}
26+
</style>
27+
<script type="text/javascript">
28+
window.onload = function() {
29+
var log = document.querySelector("#log")
30+
var boxes = document.querySelectorAll("#hidden-link")
31+
for (var i = 0; i < boxes.length; i++) {
32+
var el = boxes[i]
33+
el.onclick = function() {
34+
log.textContent = this.id
35+
}
36+
}
37+
}
38+
</script>
39+
</head>
40+
<body>
41+
<div id="log"></div>
42+
<div id="wrapper">
43+
<a id="hidden-link" href="#">hidden link</a>
44+
</div>
45+
<div id="footer"></div>
46+
</body>
47+
</html>

0 commit comments

Comments
 (0)