Skip to content

Commit bbab47c

Browse files
committed
Refactor and fix issues
1 parent be4229c commit bbab47c

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

lib/ferrum.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ def initialize(message = "Browser is dead or given window is closed")
4949
end
5050

5151
class NodeIsMovingError < Error
52-
def initialize(node, prev_pos, current_pos)
53-
@node, @prev_pos, @current_pos = node, prev_pos, current_pos
52+
def initialize(node, prev, current)
53+
@node, @prev, @current = node, prev, current
5454
super(message)
5555
end
5656

5757
def message
58-
"Node `#{@node.inspect}` that you're trying to click is moving, hence " \
59-
"we cannot. Previosuly it was at #{@prev_pos.inspect} but now at " \
60-
"#{@current_pos.inspect}."
58+
"#{@node.inspect} that you're trying to click is moving, hence " \
59+
"we cannot. Previosuly it was at #{@prev.inspect} but now at " \
60+
"#{@current.inspect}."
6161
end
6262
end
6363

lib/ferrum/node.rb

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -124,54 +124,42 @@ def inspect
124124
end
125125

126126
def find_position(x: nil, y: nil, position: :top)
127-
quads = node_quads(x, y)
128-
get_position(quads, x, y)
129-
end
130-
131-
private
127+
prev = get_content_quads
132128

133-
def get_content_quads
134-
result = page.command("DOM.getContentQuads", nodeId: node_id)
135-
raise "Node is either not visible or not an HTMLElement" if result["quads"].size == 0
136-
result
137-
end
138-
139-
def node_quads(x, y)
140-
prev_result = get_content_quads
141-
142-
result = Ferrum.with_attempts(errors: NodeIsMovingError, max: MOVING_ATTEMPTS, wait: 0) do
129+
# FIXME: Case when a few quads returned
130+
points = Ferrum.with_attempts(errors: NodeIsMovingError, max: MOVING_ATTEMPTS, wait: 0) do
143131
sleep(MOVING_WAIT)
144-
current_result = get_content_quads
145-
146-
if current_result["quads"] != prev_result["quads"]
147-
prev_pos = get_position(prev_result, x, y)
148-
current_pos = get_position(current_result, x, y)
132+
current = get_content_quads
149133

150-
prev_result = current_result
151-
raise NodeIsMovingError.new(self, prev_pos, current_pos)
134+
if current != prev
135+
error = NodeIsMovingError.new(self, prev, current)
136+
prev = current
137+
raise(error)
152138
end
153139

154-
current_result
155-
end
140+
current
141+
end.map { |q| to_points(q) }.first
156142

157-
# FIXME: Case when a few quads returned
158-
result["quads"].map do |quad|
159-
[{x: quad[0], y: quad[1]},
160-
{x: quad[2], y: quad[3]},
161-
{x: quad[4], y: quad[5]},
162-
{x: quad[6], y: quad[7]}]
163-
end.first
143+
get_position(points, x, y, position)
164144
end
165145

166-
def get_position(quads, offset_x, offset_y)
146+
private
147+
148+
def get_content_quads
149+
quads = page.command("DOM.getContentQuads", nodeId: node_id)["quads"]
150+
raise "Node is either not visible or not an HTMLElement" if quads.size == 0
151+
quads
152+
end
153+
154+
def get_position(points, offset_x, offset_y, position)
167155
x = y = nil
168156

169157
if offset_x && offset_y && position == :top
170-
point = quads.first
158+
point = points.first
171159
x = point[:x] + offset_x.to_i
172160
y = point[:y] + offset_y.to_i
173161
else
174-
x, y = quads.inject([0, 0]) do |memo, point|
162+
x, y = points.inject([0, 0]) do |memo, point|
175163
[memo[0] + point[:x],
176164
memo[1] + point[:y]]
177165
end
@@ -187,5 +175,12 @@ def get_position(quads, offset_x, offset_y)
187175

188176
[x, y]
189177
end
178+
179+
def to_points(quad)
180+
[{x: quad[0], y: quad[1]},
181+
{x: quad[2], y: quad[3]},
182+
{x: quad[4], y: quad[5]},
183+
{x: quad[6], y: quad[7]}]
184+
end
190185
end
191186
end

0 commit comments

Comments
 (0)