Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/capybara/cuprite/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ def path(node)
evaluate_on(node: node, expression: "_cuprite.path(this)")
end

def obscured?(node)
evaluate_on(node: node, expression: "_cuprite.isObscured(this)")
end

def all_text(node)
node.text
end
Expand Down
29 changes: 29 additions & 0 deletions lib/capybara/cuprite/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ class Cuprite {
return `//${selectors.join("/")}`;
}

/**
* Returns true if the node is obscured in the viewport.
*
* @param {Element} node
* @return {boolean} true if the node is obscured, false otherwise
*/
isObscured(node) {
let win = window;
let rect = node.getBoundingClientRect();
let px = rect.left + rect.width / 2;
let py = rect.top + rect.height / 2;

while (win) {
let topNode = win.document.elementFromPoint(px, py);

if (node !== topNode && !node.contains(topNode)) return true;

node = win.frameElement;
if (!node) return false;

rect = node.getBoundingClientRect();
px = rect.left + px;
py = rect.top + py;
win = win.parent;
}

return false;
}

set(node, value) {
if (node.readOnly) return;

Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/cuprite/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ def path
command(:path)
end

def obscured?
command(:obscured?)
end

def inspect
%(#<#{self.class} @node=#{@node.inspect}>)
end
Expand Down
25 changes: 25 additions & 0 deletions spec/features/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,31 @@
end
end

describe "Node#obscured?" do
context "when the element is not in the viewport of parent element" do
before do
@session.visit("/cuprite/scroll")
end

it "is is a boolean" do
expect(@session.find_link("Link outside viewport")).to be_obscured
expect(@session.find_link("Below the fold")).to be_obscured
end
end

context "when the element is only overlapped by descendants" do
before do
@session.visit("/with_html")
end

# copied from https://github.com/teamcapybara/capybara/blob/master/lib/capybara/spec/session/node_spec.rb#L328
# as this example is currently disabled on CI in the upstream suite
it "is not obscured" do
expect(@session.first(:css, "p:not(.para)")).not_to be_obscured
end
end
end

it "has no trouble clicking elements when the size of a document changes" do
@session.visit("/cuprite/long_page")
@session.find(:css, "#penultimate").click
Expand Down
5 changes: 0 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module TestSessions
RSpec.configure do |config|
config.define_derived_metadata do |metadata|
regexes = <<~REGEXP.split("\n").map { |s| Regexp.quote(s.strip) }.join("|")
node #obscured?
node #drag_to should work with jsTree
node #drag_to should drag and drop an object
node #drag_to should drag and drop if scrolling is needed
Expand Down Expand Up @@ -68,10 +67,6 @@ module TestSessions
node #path reports when element in shadow dom
node #shadow_root
node #set should submit single text input forms if ended with
#all with obscured filter should only find nodes on top in the viewport when false
#all with obscured filter should not find nodes on top outside the viewport when false
#all with obscured filter should find top nodes outside the viewport when true
#all with obscured filter should only find non-top nodes when true
#fill_in should fill in a color field
#fill_in should handle carriage returns with line feeds in a textarea correctly
#has_field with valid should be false if field is invalid
Expand Down