diff --git a/CHANGELOG.md b/CHANGELOG.md index 96256d47..963cf2c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 9af47332..4140eb3b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/ferrum/node.rb b/lib/ferrum/node.rb index c6c485e3..e5a71a08 100644 --- a/lib/ferrum/node.rb +++ b/lib/ferrum/node.rb @@ -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 diff --git a/spec/node_spec.rb b/spec/node_spec.rb index 3e3bfa4e..a4d2af8b 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -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") @@ -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