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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@
the page or the download. `wait_css`, `wait_xpath`, `network.wait_idle`, `node.wait_still` and `page.wait_until { }`
return what they waited for. Every one returns `nil` on timeout and has a `!` version that raises `TimeoutError`
- `node.hover` moves the mouse over the node, it raised `NotImplementedError`
- `node.pinpoint(x:, y:, position:)` returns the point a click on the node lands on, `x` and `y` are an offset from
the center or, with `position: :top`, from the top-left corner. A single offset applies, and so does the offset of a
node without a box

### Changed
- `node.click(offset:)` and `hover(offset:)` measure `x` and `y` from the center by default, pass `position: :top` for
the top-left corner
- `browser.contexts.each` and the `Enumerable` methods yield contexts instead of `[id, context]` pairs
- `refresh`, `back` and `forward` return `nil` when the page doesn't load in time instead of raising, use `refresh!`,
`back!` and `forward!` to raise
Expand Down Expand Up @@ -118,6 +123,8 @@
`resource_type` and `resource_type?`, like the intercepted requests
- Passing a Hash with an `"objectId"` key to `evaluate` as a remote object, will be removed in 0.22. Pass the
`Ferrum::RemoteObject` from `evaluate_handle`
- `node.find_position`, will be removed in 0.22. Use `pinpoint`, its offsets are measured from the center unless
`position: :top`

### Fixed
- `Network::Request#time` keeps the fraction of a second, it was truncated to whole seconds
Expand Down
14 changes: 7 additions & 7 deletions docs/18-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,16 @@ The node in the accessibility tree, `nil` if it is ignored there. See
page.at_css("a").ax_node.role # => "link"
```

#### find_position(x: `Integer`, y: `Integer`, position: `Symbol`) : `Array<Float>`
#### pinpoint(x: `Numeric`, y: `Numeric`, position: `Symbol`) : `Array<Float>`

The `[x, y]` coordinates a click on the node would land on, without clicking. Without `x` and `y` it's the center of
the node. With both, it's that offset from the top-left corner (`position: :top`, the default) or from the center
(`position: :center`).
The `[x, y]` point a click on the node would land on, in viewport coordinates, without clicking. `x` and `y` are an
offset from the node's center (`position: :center`, the default) or from its top-left corner (`position: :top`).
`click(offset:)` and `hover(offset:)` take the same keys.

```ruby
page.at_css("input").find_position # => [126.7, 401.4]
page.at_css("input").find_position(x: 5, y: 5) # => [55.2, 395.9]
page.at_css("input").find_position(x: 5, y: 5, position: :center) # => [131.7, 406.4]
page.at_css("input").pinpoint # => [126.7, 401.4]
page.at_css("input").pinpoint(x: 5, y: 5) # => [131.7, 406.4]
page.at_css("input").pinpoint(x: 5, y: 5, position: :top) # => [55.2, 395.9]
```

#### remove : `Hash`
Expand Down
78 changes: 63 additions & 15 deletions lib/ferrum/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Node
MOVING_WAIT_ATTEMPTS = ENV.fetch("FERRUM_NODE_MOVING_ATTEMPTS", 50).to_i
MOVING_CHECK_DELAY = ENV.fetch("FERRUM_NODE_MOVING_CHECK", 0.05).to_f
WAIT_FOR_STOP_MOVING_DEPRECATED = "wait_for_stop_moving will be removed in Ferrum 0.22. Use wait_still instead."
FIND_POSITION_DEPRECATED = "find_position will be removed in Ferrum 0.22. Use pinpoint instead, its offsets are " \
"measured from the center unless position: :top."
CLICK_MODES = {
left: [:left, 1, "drop it, a left click is the default"],
right: [:right, 1, "use button: :right instead"],
Expand Down Expand Up @@ -301,15 +303,14 @@ def type(*keys, timeout: nil)
# `keyboard.hold`, or none outside of it.
#
# @param [Hash{Symbol => Object}] offset
# Where to click instead of the center, needs both `:x` and `:y`.
# Where to click instead of the center, passed to {#pinpoint}.
#
# @option offset [Integer] :x
# @option offset [Numeric] :x
#
# @option offset [Integer] :y
# @option offset [Numeric] :y
#
# @option offset [:top, :center] :position (:top)
# What `:x` and `:y` are measured from: the node's top-left corner or its
# center.
# @option offset [:center, :top] :position (:center)
# What `:x` and `:y` are measured from: the node's center or its top-left corner.
#
# @param [Float] delay
# Delay between mouse down and mouse up events.
Expand Down Expand Up @@ -344,7 +345,7 @@ def type(*keys, timeout: nil)
# page.at_css("a").click(count: 2)
# page.at_css("a").click(modifiers: [:ctrl, :shift])
# page.at_css("canvas").click(offset: { x: 10, y: 20 })
# page.at_css("canvas").click(offset: { x: 10, y: 20, position: :center })
# page.at_css("canvas").click(offset: { x: 10, y: 20, position: :top })
#
def click(button: :left, count: 1, modifiers: nil, offset: {}, delay: 0, # rubocop:disable Metrics/ParameterLists
navigation_wait: Page::NAVIGATION_WAIT, timeout: nil, mode: nil, keys: nil, wait: nil)
Expand All @@ -355,7 +356,7 @@ def click(button: :left, count: 1, modifiers: nil, offset: {}, delay: 0, # ruboc
navigation_wait = false unless button.to_s == "left" && count == 1
deadline = Deadline.wrap(timeout || page.timeout)
Deadline.within(deadline) do
x, y = find_position(**offset)
x, y = pinpoint(**offset)
page.mouse.click(x:, y:, button:, count:, modifiers:, delay:, navigation_wait:, timeout: deadline)
end

Expand All @@ -368,7 +369,7 @@ def click(button: :left, count: 1, modifiers: nil, offset: {}, delay: 0, # ruboc
# for the node to stop moving, see {#wait_still}.
#
# @param [Hash{Symbol => Object}] offset
# Where to move instead of the center, see {#click}.
# Where to move instead of the center, passed to {#pinpoint}.
#
# @param [Numeric, Deadline, nil] timeout
# How long finding the node's position and moving there may take
Expand All @@ -385,7 +386,7 @@ def click(button: :left, count: 1, modifiers: nil, offset: {}, delay: 0, # ruboc
def hover(offset: {}, timeout: nil)
deadline = Deadline.wrap(timeout || page.timeout)
Deadline.within(deadline) do
x, y = find_position(**offset)
x, y = pinpoint(**offset)
page.mouse.move(x:, y:, timeout: deadline)
end

Expand Down Expand Up @@ -789,9 +790,52 @@ def inspect
%(@backend_node_id=#{@backend_node_id} @tag_name=#{@tag_name.inspect}>)
end

#
# The point a click on the node lands on, in viewport coordinates, without clicking. Waits for the node to stop
# moving, see {#wait_still}.
#
# @param [Numeric] x
# Horizontal offset from the reference point.
#
# @param [Numeric] y
# Vertical offset from the reference point.
#
# @param [:center, :top] position
# What `x` and `y` are measured from: the node's center or its top-left corner.
#
# @return [(Float, Float)]
#
# @raise [ArgumentError]
# When `position` is neither `:center` nor `:top`.
#
# @raise [CoordinatesNotFoundError]
# When the node has no box, e.g. it isn't displayed.
#
# @raise [TimeoutError]
#
# @example
# page.at_css("input").pinpoint # => [126.7, 401.4]
# page.at_css("input").pinpoint(x: 5, y: 5) # => [131.7, 406.4]
# page.at_css("input").pinpoint(x: 5, y: 5, position: :top) # => [55.2, 395.9]
#
def pinpoint(x: 0, y: 0, position: :center)
raise ArgumentError, "position: should be :center or :top" unless %i[center top].include?(position)

corners = to_points(settled_quads.first)
left, top = position == :top ? corners.first.values_at(:x, :y) : center_of(corners)
[left + x, top + y]
rescue CoordinatesNotFoundError
left, top, width, height = bounding_rect
raise if (left + (width / 2.0)).zero? && (top + (height / 2.0)).zero?

position == :top ? [left + x, top + y] : [left + (width / 2.0) + x, top + (height / 2.0) + y]
end

#
# Finds the x, y coordinates to click or hover on the node.
#
# @deprecated Will be removed in Ferrum 0.22. Use {#pinpoint}.
#
# @param [Integer, nil] x
# Horizontal offset from the reference point.
#
Expand All @@ -806,12 +850,8 @@ def inspect
#
# @raise [TimeoutError]
#
# @example
# page.at_css("input").find_position # => [126.7, 401.4]
# page.at_css("input").find_position(x: 5, y: 5) # => [55.2, 395.9]
# page.at_css("input").find_position(x: 5, y: 5, position: :center) # => [131.7, 406.4]
#
def find_position(x: nil, y: nil, position: :top)
Utils::Deprecate.warn(FIND_POSITION_DEPRECATED, user_code: true)
points = settled_quads.map { |q| to_points(q) }.first
get_position(points, x, y, position)
rescue CoordinatesNotFoundError
Expand Down Expand Up @@ -903,6 +943,14 @@ def click_mode(mode)
[button, count]
end

def center_of(points)
[points.sum { |p| p[:x] } / 4.0, points.sum { |p| p[:y] } / 4.0]
end

def bounding_rect
evaluate("function() { const r = this.getBoundingClientRect(); return [r.left, r.top, r.width, r.height]; }")
end

def bounding_rect_coordinates
evaluate(<<~JS)
function() {
Expand Down
10 changes: 9 additions & 1 deletion sig/ferrum/node.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Ferrum
MOVING_WAIT_DELAY: ::Float
MOVING_CHECK_DELAY: ::Float
WAIT_FOR_STOP_MOVING_DEPRECATED: String

FIND_POSITION_DEPRECATED: String
CLICK_MODES: ::Hash[::Symbol, [::Symbol, ::Integer, String]]

MOVING_WAIT_ATTEMPTS: ::Integer
Expand Down Expand Up @@ -101,7 +103,9 @@ module Ferrum

def inspect: () -> ::String

def find_position: (?x: ::Numeric?, ?y: ::Numeric?, ?position: ::Symbol) -> [::Numeric, ::Numeric]
def pinpoint: (?x: ::Numeric, ?y: ::Numeric, ?position: (:center | :top)) -> [::Float, ::Float]

%a{deprecated} def find_position: (?x: ::Numeric?, ?y: ::Numeric?, ?position: ::Symbol) -> [::Numeric, ::Numeric]

def computed_style: () -> ::Hash[String, String]

Expand All @@ -117,6 +121,10 @@ module Ferrum

def click_mode: (::Symbol mode) -> [::Symbol, ::Integer]

def center_of: (::Array[{ x: ::Numeric, y: ::Numeric }] points) -> [::Float, ::Float]

def bounding_rect: () -> [::Numeric, ::Numeric, ::Numeric, ::Numeric]

def bounding_rect_coordinates: () -> [Numeric, Numeric]

def content_quads: () -> untyped
Expand Down
79 changes: 67 additions & 12 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,27 @@ def record_mousedowns
JS
end

it "clicks the center of the node without an offset" do
browser.go_to("/click_coordinates")
browser.at_css("#box").click

expect(browser.at_css("#log").text).to eq("x: 400, y: 300")
end

it "clicks at the offset from the center of the node" do
browser.go_to("/click_coordinates")
browser.at_css("#box").click(offset: { x: 10, y: 20 })

expect(browser.at_css("#log").text).to eq("x: 410, y: 320")
end

it "clicks at the offset from the top-left corner of the node with position: :top" do
browser.go_to("/click_coordinates")
browser.at_css("#box").click(offset: { x: 10, y: 20, position: :top })

expect(browser.at_css("#log").text).to eq("x: 10, y: 20")
end

it "bounds the click by the given timeout" do
browser.go_to("/with_js")
node = browser.at_css("#remove")
Expand Down Expand Up @@ -1452,46 +1473,80 @@ def expect_select_to_clear(options)
end
end

describe "#find_position" do
describe "#pinpoint" do
before { browser.go_to("/simple") }

def rect
browser.at_css("#nav").evaluate("this.getBoundingClientRect().toJSON()")
end

def without_content_quads
allow(browser.page).to receive(:command).and_wrap_original do |command, method, **params|
method == "DOM.getContentQuads" ? { "quads" => [] } : command.call(method, **params)
end
end

it "returns the center of the node" do
x, y = browser.at_css("#nav").find_position
x, y = browser.at_css("#nav").pinpoint

expect(x).to be_within(0.5).of(rect["left"] + (rect["width"] / 2))
expect(y).to be_within(0.5).of(rect["top"] + (rect["height"] / 2))
end

it "returns the offset from the top-left corner of the node" do
x, y = browser.at_css("#nav").find_position(x: 5, y: 3)
it "returns the offset from the center of the node" do
x, y = browser.at_css("#nav").pinpoint(x: 5, y: 3)

expect(x).to be_within(0.5).of(rect["left"] + (rect["width"] / 2) + 5)
expect(y).to be_within(0.5).of(rect["top"] + (rect["height"] / 2) + 3)
end

it "returns the offset from the top-left corner of the node with position: :top" do
x, y = browser.at_css("#nav").pinpoint(x: 5, y: 3, position: :top)

expect(x).to be_within(0.5).of(rect["left"] + 5)
expect(y).to be_within(0.5).of(rect["top"] + 3)
end

it "returns the offset from the center of the node with position: :center" do
x, y = browser.at_css("#nav").find_position(x: 5, y: 3, position: :center)
it "applies a single offset" do
x, y = browser.at_css("#nav").pinpoint(x: 5)

expect(x).to be_within(0.5).of(rect["left"] + (rect["width"] / 2) + 5)
expect(y).to be_within(0.5).of(rect["top"] + (rect["height"] / 2) + 3)
expect(y).to be_within(0.5).of(rect["top"] + (rect["height"] / 2))
end

it "returns the center of the node when only one offset is given" do
x, y = browser.at_css("#nav").find_position(x: 5)
it "applies the offset to the bounding rect when the node has no content quads" do
node = browser.at_css("#nav")
without_content_quads

expect(x).to be_within(0.5).of(rect["left"] + (rect["width"] / 2))
expect(y).to be_within(0.5).of(rect["top"] + (rect["height"] / 2))
x, y = node.pinpoint(x: 5, y: 3, position: :top)

expect(x).to be_within(0.5).of(rect["left"] + 5)
expect(y).to be_within(0.5).of(rect["top"] + 3)
end

it "raises ArgumentError for an unknown position" do
expect { browser.at_css("#nav").pinpoint(position: :left) }.to raise_error(ArgumentError, /:center or :top/)
end

it "raises CoordinatesNotFoundError for a hidden node" do
node = browser.at_css("#nav")
node.execute("this.style.display = 'none'; this.offsetHeight")

expect { node.find_position }.to raise_error(Ferrum::CoordinatesNotFoundError)
expect { node.pinpoint }.to raise_error(Ferrum::CoordinatesNotFoundError)
end
end

describe "#find_position" do
it "warns that find_position is deprecated and returns the offset from the top-left corner" do
browser.go_to("/simple")
node = browser.at_css("#nav")
left, top = node.evaluate("[this.getBoundingClientRect().left, this.getBoundingClientRect().top]")
Ferrum::Utils::Deprecate.reset!

expect { expect(node.find_position(x: 5, y: 3)).to match([be_within(0.5).of(left + 5), be_within(0.5).of(top + 3)]) }
.to output(/find_position will be removed in Ferrum 0\.22.*called from #{__FILE__}/m).to_stderr
ensure
Ferrum::Utils::Deprecate.reset!
end
end

Expand Down
Loading