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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
- A `Ferrum::Node` found before a navigation or reload kept its cached CDP node id, which Chrome reuses for the new
document, so `focus`, `click`, `scroll_into_view` and friends silently hit another element. Node ids are now dropped
on `DOM.documentUpdated` and such nodes raise `Ferrum::NodeNotFoundError` [#86]
- Replacing an iframe's content with `frame.content=` or querying the whole page with `accessibility.query` broke every
node found before it with `Ferrum::NodeNotFoundError`, even though the nodes were still on the page. Their ids are
requested again now. A node used while the page is still loading a new document says so in its error [#650]
- Behind a proxy with credentials, `request.abort` in `on(:request)` and `network.blacklist`/`whitelist` were ignored
because Ferrum's own proxy handler continued every request first. It now only continues requests nobody else handles [#332]

Expand Down
4 changes: 3 additions & 1 deletion lib/ferrum/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def node_id
document_version = page.document_version
@node_id = begin
id = page.command("DOM.requestNode", objectId: @object_id)["nodeId"]
raise NodeNotFoundError, { "message" => "node is not trackable" } if id.zero?
if id.zero?
raise NodeNotFoundError, { "message" => "node is not trackable, the page is still loading a new document" }
end

id
rescue NoExecutionContextError
Expand Down
14 changes: 9 additions & 5 deletions lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class Page
# @return [Downloads]
attr_reader :downloads

# Bumped every time Chrome reports `DOM.documentUpdated`, which
# invalidates every node id handed out before it.
# Bumped every time Chrome reports `DOM.documentUpdated` and after every
# `DOM.getDocument`, both invalidate every node id handed out before them.
#
# @return [Integer]
attr_reader :document_version
Expand Down Expand Up @@ -521,9 +521,13 @@ def use_authorized_proxy?
# The root node id, or `true` when sent asynchronously.
#
def document_node_id(async: false)
return client.command("DOM.getDocument", async: true, depth: 0) if async

command("DOM.getDocument", depth: 0).dig("root", "nodeId")
result = if async
client.command("DOM.getDocument", async: true, depth: 0)
else
command("DOM.getDocument", depth: 0).dig("root", "nodeId")
end
@document_version += 1
result
end

private
Expand Down
24 changes: 24 additions & 0 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@
expect(browser.evaluate("document.activeElement.id")).not_to eq("customer_email")
end

it "keeps working after an iframe's content is replaced" do
browser.go_to("/form")
node = browser.at_css("#customer_email")
node.focus
node.blur

browser.frame_by(name: "frame").content = "<p>new</p>"
node.focus

expect(browser.evaluate("document.activeElement.id")).to eq("customer_email")
end

it "keeps working after an accessibility query of the whole page" do
browser.go_to("/form")
node = browser.at_css("#customer_email")
node.focus
node.blur

browser.accessibility.query(role: "textbox")
node.focus

expect(browser.evaluate("document.activeElement.id")).to eq("customer_email")
end

it "raises an error if the element is not visible" do
browser.go_to("/index")

Expand Down
Loading