Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
[rubycdp/ferrum#689]

### Fixed
- A click on a node the page removes before the hit test raises `ObsoleteNode`, which Capybara retries, instead of
`Ferrum::JavaScriptError: node.getRootNode(...).elementFromPoint is not a function` [#359]

### Removed
- Methods deprecated in 0.20, see its Deprecated section for replacements
Expand Down
5 changes: 5 additions & 0 deletions lib/capybara/cuprite/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class InvalidSelector extends Error {}
class TimedOutPromise extends Error {}
class MouseEventFailed extends Error {}
class ObsoleteNode extends Error {}

const EVENTS = {
FOCUS: ["blur", "focus", "focusin", "focusout"],
Expand Down Expand Up @@ -329,6 +330,10 @@
}

mouseEventTest(node, name, x, y) {
// The page can remove the node after Ruby checked it; getRootNode() would
// then return the detached subtree, which has no elementFromPoint.
if (!node.isConnected) throw new ObsoleteNode();

let frameOffset = this._frameOffset();
x -= frameOffset.left;
y -= frameOffset.top;
Expand Down
1 change: 1 addition & 0 deletions lib/capybara/cuprite/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ def before_click(name, offset = {})
js(:mouseEventTest, name.to_s, x, y)
rescue Ferrum::JavaScriptError => e
raise MouseEventFailed, e.message if e.class_name == "MouseEventFailed"
raise ObsoleteNode.new(self, e.response) if e.class_name == "ObsoleteNode"

raise
end
Expand Down
13 changes: 13 additions & 0 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@
.to raise_error(Capybara::Cuprite::MouseEventFailed)
end

it "raises ObsoleteNode for an element removed before the hit test" do
@session.visit("/cuprite/with_js")
node = @session.find(:css, "#remove_me")
# The page removes the node after Ruby checked it and before the hit test runs.
@session.execute_script(<<~JS)
const test = _cuprite.mouseEventTest;
_cuprite.mouseEventTest = function(node, ...args) { node.remove(); return test.call(this, node, ...args); };
JS

# native.click bypasses Capybara::Node::Element#reload, which would re-find and retry.
expect { node.native.click }.to raise_error(Capybara::Cuprite::ObsoleteNode)
end

it "clicks an element brought into the viewport" do
@session.visit("/cuprite/with_js")
@session.execute_script %($("#off-the-left").animate({left: "10"});)
Expand Down
Loading