Skip to content

Commit

Permalink
feat: Add remove and exists? methods to Ferrum::Node
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Apr 25, 2024
1 parent 814e676 commit 4fad816
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- `Ferrum::Network#wait_for_idle!` raises an error if timeout reached.
- `Ferrum::Browser#close` closes browser gracefully issuing a CDP command, doesn't clean up ruby resources.
- `Ferrum::Node#remove` removes node from DOM tree.
- `Ferrum::Node#exists?` check whether the node in ruby world still exists in the DOM tree.

### Changed

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,8 @@ frame.at_css("//a[text() = 'Log in']") # => Node
#### select
#### scroll_into_view
#### in_viewport?(of: `Node | nil`) : `Boolean`
#### remove
#### exists?

(chainable) Selects options by passed attribute.

Expand Down
11 changes: 11 additions & 0 deletions lib/ferrum/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ def computed_style
.each_with_object({}) { |style, memo| memo.merge!(style["name"] => style["value"]) }
end

def remove
page.command("DOM.removeNode", nodeId: node_id)
end

def exists?
page.command("DOM.resolveNode", nodeId: node_id)
true
rescue Ferrum::NodeNotFoundError
false
end

private

def bounding_rect_coordinates
Expand Down
52 changes: 44 additions & 8 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,42 @@
end
end

describe "#remove" do
it "removes node" do
browser.go_to("/ferrum/simple")
node = browser.at_css("#break")

node.remove

expect { node.text }.to raise_error(Ferrum::NodeNotFoundError)
end

it "raises an error if node is already gone" do
browser.go_to("/ferrum/simple")
node = browser.at_css("#break")
node.evaluate("this.remove()")

expect { node.remove }.to raise_error(Ferrum::NodeNotFoundError)
end
end

describe "#exists?" do
it "returns true" do
browser.go_to("/ferrum/simple")
node = browser.at_css("#break")

expect(node.exists?).to be_truthy
end

it "returns false" do
browser.go_to("/ferrum/simple")
node = browser.at_css("#break")
node.remove

expect(node.exists?).to be_falsey
end
end

context "whitespace stripping tests", skip: true do
before do
browser.go_to("/ferrum/filter_text_test")
Expand Down Expand Up @@ -391,30 +427,30 @@
describe "#text" do
it "skips BR" do
browser.go_to("/ferrum/simple")
el = browser.at_css("#break")
node = browser.at_css("#break")

expect(el.text).to eq("FooBar")
expect(node.text).to eq("FooBar")
end

it "works with valid utf8" do
browser.go_to("/unicode")
el = browser.at_css("#valid")
node = browser.at_css("#valid")

expect(el.text).to eq("Havregrynskake med marengs og nøtter 😍 såå god!")
expect(node.text).to eq("Havregrynskake med marengs og nøtter 😍 såå god!")
end

it "works with invalid utf8" do
browser.go_to("/unicode")
el = browser.at_css("#invalid")
node = browser.at_css("#invalid")

expect(el.text).to eq("Havregrynskake med marengs og nøtter ???")
expect(node.text).to eq("Havregrynskake med marengs og nøtter ???")
end

it "works with curly utf8" do
browser.go_to("/unicode")
el = browser.at_css("#curly")
node = browser.at_css("#curly")

expect(el.text).to eq("😀 and ☀")
expect(node.text).to eq("😀 and ☀")
end

context "SVG tests" do
Expand Down

0 comments on commit 4fad816

Please sign in to comment.