Skip to content

Commit 8e0c6ba

Browse files
authored
Merge pull request #222 from joelhawksley/computed-style
Add `Ferrum::Node#computed_style`
2 parents aa08ed8 + fd6dda1 commit 8e0c6ba

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ a block with this page, after which the page is closed.
1515
- `Ferrum::Node`
1616
- `#selected` check selected option
1717
- `#select` select option
18+
- `#computed_style` returns hash of computed styles
1819
- `Ferrum::JavaScriptError#stack_trace` attr_reader
1920
- Windows support
2021
- Show warning and accept dialog if no handler given

lib/ferrum/node.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ def find_position(x: nil, y: nil, position: :top)
193193
[x, y]
194194
end
195195

196+
# Returns a hash of the computed styles for the node
197+
def computed_style
198+
page
199+
.command("CSS.getComputedStyleForNode", nodeId: node_id)["computedStyle"]
200+
.inject({}) { |memo, style| memo[style["name"]] = style["value"]; memo }
201+
end
202+
196203
private
197204

198205
def bounding_rect_coordinates

spec/node_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,18 @@ module Ferrum
259259
it { expect(browser.at_css("#form_name").focusable?).to eq(true) }
260260
end
261261
end
262+
263+
describe "#computed_style" do
264+
before do
265+
browser.go_to("/ferrum/computed_style")
266+
end
267+
268+
it "returns the computed styles for the node" do
269+
styles = browser.at_css("#test_node").computed_style
270+
271+
expect(styles["color"]).to eq("rgb(255, 0, 0)")
272+
expect(styles["font-weight"]).to eq("700")
273+
end
274+
end
262275
end
263276
end

spec/support/views/computed_style.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<style>
2+
.foo { color: red; }
3+
.bar { font-weight: bold; }
4+
</style>
5+
6+
<span id="test_node" class="foo bar">Test</span>

0 commit comments

Comments
 (0)