ferrum 0.20.2 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -4
  3. data/lib/ferrum/accessibility.rb +40 -6
  4. data/lib/ferrum/browser/command.rb +2 -11
  5. data/lib/ferrum/browser/options/chrome.rb +58 -1
  6. data/lib/ferrum/browser/options.rb +63 -9
  7. data/lib/ferrum/browser/process.rb +12 -4
  8. data/lib/ferrum/browser/xvfb.rb +22 -2
  9. data/lib/ferrum/browser.rb +74 -35
  10. data/lib/ferrum/client/web_socket.rb +13 -6
  11. data/lib/ferrum/client.rb +119 -81
  12. data/lib/ferrum/concurrency/awaitable_map.rb +171 -0
  13. data/lib/ferrum/concurrency/cell.rb +128 -0
  14. data/lib/ferrum/concurrency/dispatcher.rb +146 -0
  15. data/lib/ferrum/console.rb +3 -2
  16. data/lib/ferrum/context.rb +162 -27
  17. data/lib/ferrum/contexts.rb +45 -22
  18. data/lib/ferrum/cookies/collection.rb +93 -0
  19. data/lib/ferrum/cookies/cookie.rb +0 -11
  20. data/lib/ferrum/cookies.rb +79 -86
  21. data/lib/ferrum/deadline.rb +140 -0
  22. data/lib/ferrum/dialog.rb +22 -0
  23. data/lib/ferrum/downloads.rb +83 -10
  24. data/lib/ferrum/errors.rb +96 -6
  25. data/lib/ferrum/frame/dom.rb +147 -38
  26. data/lib/ferrum/frame/runtime.rb +40 -32
  27. data/lib/ferrum/frame/script.rb +7 -38
  28. data/lib/ferrum/frame.rb +57 -20
  29. data/lib/ferrum/headers.rb +32 -27
  30. data/lib/ferrum/interceptable/chain.rb +99 -0
  31. data/lib/ferrum/interceptable.rb +44 -27
  32. data/lib/ferrum/isolated_world.rb +9 -10
  33. data/lib/ferrum/keyboard.rb +27 -6
  34. data/lib/ferrum/mouse.rb +139 -52
  35. data/lib/ferrum/network/auth_request.rb +42 -7
  36. data/lib/ferrum/network/error.rb +34 -4
  37. data/lib/ferrum/network/exchange.rb +35 -7
  38. data/lib/ferrum/network/intercepted_request.rb +42 -9
  39. data/lib/ferrum/network/request.rb +33 -9
  40. data/lib/ferrum/network/request_params.rb +11 -2
  41. data/lib/ferrum/network/response.rb +23 -2
  42. data/lib/ferrum/network.rb +267 -64
  43. data/lib/ferrum/node.rb +430 -62
  44. data/lib/ferrum/page/animation.rb +4 -0
  45. data/lib/ferrum/page/cookies.rb +133 -0
  46. data/lib/ferrum/page/frames.rb +7 -7
  47. data/lib/ferrum/page/navigation.rb +430 -0
  48. data/lib/ferrum/page/screencast.rb +4 -0
  49. data/lib/ferrum/page/screenshot.rb +13 -1
  50. data/lib/ferrum/page/stream.rb +13 -5
  51. data/lib/ferrum/page/tracing.rb +12 -7
  52. data/lib/ferrum/page.rb +168 -190
  53. data/lib/ferrum/remote_object.rb +4 -0
  54. data/lib/ferrum/target.rb +49 -11
  55. data/lib/ferrum/utils/deprecate.rb +73 -2
  56. data/lib/ferrum/utils/poll.rb +57 -0
  57. data/lib/ferrum/version.rb +1 -1
  58. data/lib/ferrum/worker.rb +34 -19
  59. data/lib/ferrum.rb +5 -1
  60. metadata +11 -6
  61. data/lib/ferrum/browser/options/base.rb +0 -112
  62. data/lib/ferrum/client/subscriber.rb +0 -145
  63. data/lib/ferrum/frame/runtime/legacy.rb +0 -336
  64. data/lib/ferrum/utils/event.rb +0 -33
data/lib/ferrum/node.rb CHANGED
@@ -13,6 +13,16 @@ module Ferrum
13
13
  class Node
14
14
  MOVING_WAIT_DELAY = ENV.fetch("FERRUM_NODE_MOVING_WAIT", 0.01).to_f
15
15
  MOVING_WAIT_ATTEMPTS = ENV.fetch("FERRUM_NODE_MOVING_ATTEMPTS", 50).to_i
16
+ MOVING_CHECK_DELAY = ENV.fetch("FERRUM_NODE_MOVING_CHECK", 0.05).to_f
17
+ WAIT_FOR_STOP_MOVING_DEPRECATED = "wait_for_stop_moving will be removed in Ferrum 0.22. Use wait_still instead."
18
+ FIND_POSITION_DEPRECATED = "find_position will be removed in Ferrum 0.22. Use pinpoint instead, its offsets are " \
19
+ "measured from the center unless position: :top."
20
+ CLICK_MODES = {
21
+ left: [:left, 1, "drop it, a left click is the default"],
22
+ right: [:right, 1, "use button: :right instead"],
23
+ double: [:left, 2, "use count: 2 instead"]
24
+ }.freeze
25
+ private_constant :CLICK_MODES
16
26
 
17
27
  # @return [Page] the page the node belongs to.
18
28
  attr_reader :page
@@ -53,6 +63,11 @@ module Ferrum
53
63
  #
54
64
  # @return [Hash{String => Object}]
55
65
  #
66
+ # @raise [TimeoutError]
67
+ #
68
+ # @example
69
+ # page.at_css("a").description["nodeName"] # => "A"
70
+ #
56
71
  def description
57
72
  @description ||= page.command("DOM.describeNode", backendNodeId: backend_node_id)["node"]
58
73
  end
@@ -63,11 +78,16 @@ module Ferrum
63
78
  #
64
79
  # @return [String]
65
80
  #
81
+ # @raise [TimeoutError]
82
+ #
83
+ # @example
84
+ # page.at_css("#nav").tag_name # => "div"
85
+ #
66
86
  def tag_name
67
87
  @tag_name ||= description["nodeName"].downcase
68
88
  end
69
89
 
70
- # Frontend node id is resolved lazily, on first actual need (focus, click, scroll_into_view, etc.)
90
+ # Frontend node id is resolved lazily, on first actual need (focus, click, scroll, etc.)
71
91
  # We can try to subscribe to `DOM.childNodeRemoved` and `DOM.childNodeInserted` in the future
72
92
  # to keep track of nodes.
73
93
  def node_id
@@ -92,6 +112,13 @@ module Ferrum
92
112
  # Whether this is an element node, as opposed to e.g. a text node.
93
113
  #
94
114
  # @return [Boolean]
115
+ #
116
+ # @raise [TimeoutError]
117
+ #
118
+ # @example
119
+ # page.at_css("a").node? # => true
120
+ # page.xpath("//a/text()").first.node? # => false
121
+ #
95
122
  def node?
96
123
  node_type == 1 # nodeType: 3, nodeName: "#text" e.g.
97
124
  end
@@ -103,6 +130,8 @@ module Ferrum
103
130
  #
104
131
  # @return [Frame, nil]
105
132
  #
133
+ # @raise [TimeoutError]
134
+ #
106
135
  # @example
107
136
  # frame = page.at_xpath("//iframe").frame # => Frame
108
137
  # frame.at_css("//a[text() = 'Log in']") # => Node
@@ -116,6 +145,8 @@ module Ferrum
116
145
  #
117
146
  # @return [self]
118
147
  #
148
+ # @raise [TimeoutError]
149
+ #
119
150
  # @example
120
151
  # input = page.at_css("input[name='q']")
121
152
  # input.focus
@@ -125,9 +156,11 @@ module Ferrum
125
156
  end
126
157
 
127
158
  # Whether the node can receive focus. Attempts to {#focus} the node to
128
- # find out.
159
+ # find out, and leaves it focused when it can.
129
160
  #
130
161
  # @return [Boolean]
162
+ #
163
+ # @raise [TimeoutError]
131
164
  def focusable?
132
165
  focus
133
166
  true
@@ -135,39 +168,86 @@ module Ferrum
135
168
  e.message == "Element is not focusable" ? false : raise
136
169
  end
137
170
 
171
+ #
172
+ # Polls the node's position until two snapshots taken `interval` seconds
173
+ # apart match, see {#moving?}.
174
+ #
175
+ # @param [Numeric, Deadline, nil] timeout
176
+ # How long to keep trying, the page's `timeout` by default.
177
+ #
178
+ # @param [Numeric] interval
179
+ # Seconds between the two snapshots of an attempt.
180
+ #
181
+ # @return [Array<Array<Float>>, nil]
182
+ # The content quads of the node once it stands still, each one as the
183
+ # eight coordinates of its four corners. `nil` if it keeps moving.
184
+ #
185
+ # @raise [CoordinatesNotFoundError]
186
+ # If the node isn't visible.
187
+ #
188
+ # @example
189
+ # page.at_css("#modal").wait_still
190
+ # # => [[50.2, 390.9, 203.2, 390.9, 203.2, 411.9, 50.2, 411.9]]
191
+ #
192
+ def wait_still(timeout: nil, interval: MOVING_WAIT_DELAY)
193
+ Utils::Poll.call(timeout || page.timeout, 0) do
194
+ previous, current = content_quads_with(delay: interval)
195
+ current if previous == current
196
+ end
197
+ end
198
+
199
+ #
200
+ # Same as {#wait_still}, but raises if the node keeps moving.
201
+ #
202
+ # @param [Hash] options
203
+ # The options of {#wait_still}.
204
+ #
205
+ # @return [Array<Array<Float>>]
206
+ #
207
+ # @raise [TimeoutError]
208
+ # If the node keeps moving.
209
+ #
210
+ def wait_still!(**options)
211
+ wait_still(**options) || raise(TimeoutError)
212
+ end
213
+
138
214
  #
139
215
  # Waits until the node's position stops changing, retrying up to
140
- # `attempts` times. Raises {NodeMovingError} if the node is still moving
141
- # after the last attempt.
216
+ # `attempts` times.
217
+ #
218
+ # @deprecated Will be removed in Ferrum 0.22. Use {#wait_still}.
142
219
  #
143
220
  # @param [Float] delay
144
- # Seconds to wait between two position checks.
145
221
  #
146
222
  # @param [Integer] attempts
147
- # Maximum number of attempts before raising.
148
223
  #
149
- # @return [Array]
150
- # The content quads of the node once it has stopped moving.
224
+ # @return [Array<Array<Float>>]
225
+ #
226
+ # @raise [NodeMovingError]
151
227
  #
152
228
  def wait_for_stop_moving(delay: MOVING_WAIT_DELAY, attempts: MOVING_WAIT_ATTEMPTS)
153
- Utils::Attempt.with_retry(errors: NodeMovingError, max: attempts, wait: 0) do
154
- previous, current = content_quads_with(delay: delay)
155
- raise NodeMovingError.new(self, previous, current) if previous != current
156
-
157
- current
158
- end
229
+ Utils::Deprecate.warn(WAIT_FOR_STOP_MOVING_DEPRECATED, user_code: true)
230
+ settled_quads(delay: delay, attempts: attempts)
159
231
  end
160
232
 
161
- # Checks whether the node's position has stopped changing, by comparing
162
- # two content-quad snapshots taken `delay` seconds apart.
233
+ # Checks whether the node's position is changing, by comparing two
234
+ # content-quad snapshots taken `delay` seconds apart. The default is a few
235
+ # frames, a shorter one can miss a node that moves slowly.
163
236
  #
164
237
  # @param [Float] delay
165
238
  # Seconds to wait between the two position checks.
166
239
  #
167
240
  # @return [Boolean]
168
- def moving?(delay: MOVING_WAIT_DELAY)
241
+ #
242
+ # @raise [TimeoutError]
243
+ #
244
+ # @example
245
+ # page.at_css(".animated").moving? # => true
246
+ # page.at_css("#nav").moving? # => false
247
+ #
248
+ def moving?(delay: MOVING_CHECK_DELAY)
169
249
  previous, current = content_quads_with(delay: delay)
170
- previous == current
250
+ previous != current
171
251
  end
172
252
 
173
253
  #
@@ -175,6 +255,11 @@ module Ferrum
175
255
  #
176
256
  # @return [self]
177
257
  #
258
+ # @raise [TimeoutError]
259
+ #
260
+ # @example
261
+ # page.at_css("input[name='q']").focus.blur
262
+ #
178
263
  def blur
179
264
  tap { evaluate("this.blur()") }
180
265
  end
@@ -186,41 +271,126 @@ module Ferrum
186
271
  # @param [Array<String, Symbol, (Symbol, String)>] keys
187
272
  # The keys to type, e.g. `"Input"`, `[:Shift, "s"], "tring"`.
188
273
  #
274
+ # @param [Numeric, Deadline, nil] timeout
275
+ # How long typing all the keys may take together, the page timeout by default.
276
+ #
189
277
  # @return [self]
190
278
  #
279
+ # @raise [TimeoutError]
280
+ #
191
281
  # @example
192
282
  # input.focus.type("Input")
283
+ # input.focus.type(long_text, timeout: 30)
284
+ #
285
+ def type(*keys, timeout: nil)
286
+ tap { page.keyboard.type(*keys, timeout: timeout) }
287
+ end
288
+
193
289
  #
194
- def type(*keys)
195
- tap { page.keyboard.type(*keys) }
196
- end
197
-
198
- # mode: (:left | :right | :double)
199
- # keys: (:alt, (:ctrl | :control), (:meta | :command), :shift)
200
- # offset: { :x, :y, :position (:top | :center) }
201
- def click(mode: :left, keys: [], offset: {}, delay: 0)
202
- x, y = find_position(**offset)
203
- modifiers = page.keyboard.modifiers(keys)
204
-
205
- # `:right` and `:double` pass `wait: 0` to preserve the historical
206
- # no-network-wait default of `Mouse#up` and `Mouse#down`
207
- case mode
208
- when :right
209
- page.mouse.click(x:, y:, modifiers:, delay:, button: :right, wait: 0)
210
- when :double
211
- page.mouse.click(x:, y:, modifiers:, delay:, count: 2, wait: 0)
212
- when :left
213
- page.mouse.click(x:, y:, modifiers:, delay:)
290
+ # Clicks the node at its center, or at an offset from it, by moving the
291
+ # mouse there first. Waits for the node to stop moving, see
292
+ # {#wait_still}.
293
+ #
294
+ # @param [:left, :right, :middle, :back, :forward] button
295
+ # The mouse button to click.
296
+ #
297
+ # @param [Integer] count
298
+ # How many times to click, `2` is a double click.
299
+ #
300
+ # @param [Array<Symbol>, nil] modifiers
301
+ # Modifier keys to hold while clicking: `:alt`, `:ctrl` (or `:control`),
302
+ # `:meta` (or `:command`) and `:shift`. If omitted, uses the keys held by
303
+ # `keyboard.hold`, or none outside of it.
304
+ #
305
+ # @param [Hash{Symbol => Object}] offset
306
+ # Where to click instead of the center, passed to {#pinpoint}.
307
+ #
308
+ # @option offset [Numeric] :x
309
+ #
310
+ # @option offset [Numeric] :y
311
+ #
312
+ # @option offset [:center, :top] :position (:center)
313
+ # What `:x` and `:y` are measured from: the node's center or its top-left corner.
314
+ #
315
+ # @param [Float] delay
316
+ # Delay between mouse down and mouse up events.
317
+ #
318
+ # @param [Numeric, false] navigation_wait
319
+ # Seconds to watch for a navigation started by a single left click, then
320
+ # wait for it to load. Ignored for other buttons and counts, which never
321
+ # wait. `false` returns as soon as the click is dispatched.
322
+ #
323
+ # @param [:left, :right, :double, nil] mode
324
+ # Deprecated, use `button: :right` or `count: 2`.
325
+ #
326
+ # @param [Array<Symbol>, nil] keys
327
+ # Deprecated, renamed to `modifiers`.
328
+ #
329
+ # @param [Numeric, nil] wait
330
+ # Deprecated, renamed to `navigation_wait`.
331
+ #
332
+ # @param [Numeric, Deadline, nil] timeout
333
+ # How long finding the node's position, the click and the page load it starts may take together, the page
334
+ # timeout by default.
335
+ #
336
+ # @return [self]
337
+ #
338
+ # @raise [TimeoutError]
339
+ #
340
+ # @example
341
+ # page.at_css("a").click
342
+ # page.at_css("a").click(timeout: 30)
343
+ # page.at_css("a").click(navigation_wait: false)
344
+ # page.at_css("a").click(button: :right)
345
+ # page.at_css("a").click(count: 2)
346
+ # page.at_css("a").click(modifiers: [:ctrl, :shift])
347
+ # page.at_css("canvas").click(offset: { x: 10, y: 20 })
348
+ # page.at_css("canvas").click(offset: { x: 10, y: 20, position: :top })
349
+ #
350
+ def click(button: :left, count: 1, modifiers: nil, offset: {}, delay: 0, # rubocop:disable Metrics/ParameterLists
351
+ navigation_wait: Page::NAVIGATION_WAIT, timeout: nil, mode: nil, keys: nil, wait: nil)
352
+ navigation_wait = Utils::Deprecate.renamed_keyword(:wait, :navigation_wait, wait, navigation_wait)
353
+ modifiers = Utils::Deprecate.renamed_keyword(:keys, :modifiers, keys, modifiers)
354
+ button, count = click_mode(mode) if mode
355
+ # Only a single left click waits, preserving the historical no-network-wait default of `Mouse#up`
356
+ navigation_wait = false unless button.to_s == "left" && count == 1
357
+ deadline = Deadline.wrap(timeout || page.timeout)
358
+ Deadline.within(deadline) do
359
+ x, y = pinpoint(**offset)
360
+ page.mouse.click(x:, y:, button:, count:, modifiers:, delay:, navigation_wait:, timeout: deadline)
214
361
  end
215
362
 
216
363
  self
217
364
  end
218
365
 
219
- # Not currently implemented.
220
366
  #
221
- # @raise [NotImplementedError] always
222
- def hover
223
- raise NotImplementedError
367
+ # Moves the mouse over the node's center, or an offset from it, firing
368
+ # `mouseover`, `mouseenter` and `mousemove` and matching `:hover`. Waits
369
+ # for the node to stop moving, see {#wait_still}.
370
+ #
371
+ # @param [Hash{Symbol => Object}] offset
372
+ # Where to move instead of the center, passed to {#pinpoint}.
373
+ #
374
+ # @param [Numeric, Deadline, nil] timeout
375
+ # How long finding the node's position and moving there may take
376
+ # together, the page timeout by default.
377
+ #
378
+ # @return [self]
379
+ #
380
+ # @raise [TimeoutError]
381
+ #
382
+ # @example
383
+ # menu = page.at_css("#menu").hover
384
+ # menu.evaluate("this.matches(':hover')") # => true
385
+ #
386
+ def hover(offset: {}, timeout: nil)
387
+ deadline = Deadline.wrap(timeout || page.timeout)
388
+ Deadline.within(deadline) do
389
+ x, y = pinpoint(**offset)
390
+ page.mouse.move(x:, y:, timeout: deadline)
391
+ end
392
+
393
+ self
224
394
  end
225
395
 
226
396
  #
@@ -228,13 +398,27 @@ module Ferrum
228
398
  #
229
399
  # @return [self]
230
400
  #
401
+ # @raise [TimeoutError]
402
+ #
231
403
  # @example
232
- # page.at_css("#footer").scroll_into_view
404
+ # page.at_css("#footer").scroll
233
405
  #
234
- def scroll_into_view
406
+ def scroll
235
407
  tap { page.command("DOM.scrollIntoViewIfNeeded", nodeId: node_id) }
236
408
  end
237
409
 
410
+ #
411
+ # @deprecated Will be removed in Ferrum 0.22. Use {#scroll}.
412
+ #
413
+ # @return [self]
414
+ #
415
+ # @raise [TimeoutError]
416
+ #
417
+ def scroll_into_view
418
+ Utils::Deprecate.warn("scroll_into_view will be removed in Ferrum 0.22. Use scroll instead.", user_code: true)
419
+ scroll
420
+ end
421
+
238
422
  # Whether the node's bounding rect is fully within the viewport (or,
239
423
  # when `of:` is given, within that scoping element's bounds).
240
424
  #
@@ -242,6 +426,14 @@ module Ferrum
242
426
  # An element to use as the visible bounds instead of the window.
243
427
  #
244
428
  # @return [Boolean]
429
+ #
430
+ # @raise [TimeoutError]
431
+ #
432
+ # @example
433
+ # page.at_css("#footer").in_viewport? # => false
434
+ # page.at_css("#footer").scroll.in_viewport? # => true
435
+ # page.at_css("#row").in_viewport?(of: page.at_css("#table")) # => true
436
+ #
245
437
  def in_viewport?(of: nil)
246
438
  evaluate(<<~JS, of)
247
439
  function(scope) {
@@ -263,7 +455,9 @@ module Ferrum
263
455
  # @param [String, Array<String>] value
264
456
  # Path or paths to the file(s) to upload.
265
457
  #
266
- # @return [Hash{String => Object}]
458
+ # @return [self]
459
+ #
460
+ # @raise [TimeoutError]
267
461
  #
268
462
  # @example
269
463
  # page.at_css("input[type=file]").select_file("/path/to/file.png")
@@ -275,6 +469,7 @@ module Ferrum
275
469
  backendNodeId: backend_node_id,
276
470
  files: Array(value)
277
471
  )
472
+ self
278
473
  end
279
474
 
280
475
  #
@@ -285,11 +480,13 @@ module Ferrum
285
480
  #
286
481
  # @return [Node, nil]
287
482
  #
483
+ # @raise [TimeoutError]
484
+ #
288
485
  # @example
289
- # page.at_xpath("//iframe").at_xpath(".//a") # => Node
486
+ # page.at_xpath("//nav").at_xpath(".//a") # => Node
290
487
  #
291
488
  def at_xpath(selector)
292
- page.at_xpath(selector, within: self)
489
+ @runtime.at_xpath(selector, within: self)
293
490
  end
294
491
 
295
492
  #
@@ -300,11 +497,13 @@ module Ferrum
300
497
  #
301
498
  # @return [Node, nil]
302
499
  #
500
+ # @raise [TimeoutError]
501
+ #
303
502
  # @example
304
503
  # page.at_css("form").at_css("input[name='q']") # => Node
305
504
  #
306
505
  def at_css(selector)
307
- page.at_css(selector, within: self)
506
+ @runtime.at_css(selector, within: self)
308
507
  end
309
508
 
310
509
  #
@@ -315,11 +514,13 @@ module Ferrum
315
514
  #
316
515
  # @return [Array<Node>]
317
516
  #
517
+ # @raise [TimeoutError]
518
+ #
318
519
  # @example
319
520
  # page.at_css("ul").xpath(".//li") # => [Node]
320
521
  #
321
522
  def xpath(selector)
322
- page.xpath(selector, within: self)
523
+ @runtime.xpath(selector, within: self)
323
524
  end
324
525
 
325
526
  #
@@ -330,11 +531,13 @@ module Ferrum
330
531
  #
331
532
  # @return [Array<Node>]
332
533
  #
534
+ # @raise [TimeoutError]
535
+ #
333
536
  # @example
334
537
  # page.at_css("ul").css("li") # => [Node]
335
538
  #
336
539
  def css(selector)
337
- page.css(selector, within: self)
540
+ @runtime.css(selector, within: self)
338
541
  end
339
542
 
340
543
  #
@@ -342,6 +545,8 @@ module Ferrum
342
545
  #
343
546
  # @return [String]
344
547
  #
548
+ # @raise [TimeoutError]
549
+ #
345
550
  # @example
346
551
  # page.at_css("a > h3").text # => "rubycdp/ferrum: Ruby Chrome/Chromium driver - GitHub"
347
552
  #
@@ -349,7 +554,19 @@ module Ferrum
349
554
  evaluate("this.textContent")
350
555
  end
351
556
 
352
- # FIXME: clear API for text and inner_text
557
+ #
558
+ # The node's rendered text, i.e. `innerText`. Unlike {#text}, it follows
559
+ # the layout: text hidden by CSS is left out and line breaks are kept.
560
+ #
561
+ # @return [String]
562
+ #
563
+ # @raise [TimeoutError]
564
+ #
565
+ # @example
566
+ # # <p>Hello <span style="display: none">hidden</span>world</p>
567
+ # page.at_css("p").text # => "Hello hiddenworld"
568
+ # page.at_css("p").inner_text # => "Hello world"
569
+ #
353
570
  def inner_text
354
571
  evaluate("this.innerText")
355
572
  end
@@ -360,6 +577,11 @@ module Ferrum
360
577
  #
361
578
  # @return [Object]
362
579
  #
580
+ # @raise [TimeoutError]
581
+ #
582
+ # @example
583
+ # page.at_css("input[name='q']").value # => "ferrum"
584
+ #
363
585
  def value
364
586
  evaluate("this.value")
365
587
  end
@@ -371,13 +593,28 @@ module Ferrum
371
593
  #
372
594
  # @return [Object]
373
595
  #
596
+ # @raise [TimeoutError]
597
+ #
374
598
  # @example
375
599
  # page.at_css("input").property("value") # => "Foo"
376
600
  #
377
601
  def property(name)
378
602
  evaluate("this[name]", name: name)
379
603
  end
380
- alias [] property
604
+
605
+ #
606
+ # @deprecated Will be removed in Ferrum 0.22. Use {#property}.
607
+ #
608
+ # @param [String] name
609
+ #
610
+ # @return [Object]
611
+ #
612
+ # @raise [TimeoutError]
613
+ #
614
+ def [](name)
615
+ Utils::Deprecate.warn("node[name] will be removed in Ferrum 0.22. Use property(name) instead.", user_code: true)
616
+ property(name)
617
+ end
381
618
 
382
619
  #
383
620
  # Returns the value of the given HTML attribute, i.e.
@@ -388,6 +625,8 @@ module Ferrum
388
625
  #
389
626
  # @return [String, nil]
390
627
  #
628
+ # @raise [TimeoutError]
629
+ #
391
630
  # @example
392
631
  # page.at_css("input").attribute("value") # => "Foo"
393
632
  #
@@ -400,6 +639,13 @@ module Ferrum
400
639
  #
401
640
  # @return [Array<Node>]
402
641
  #
642
+ # @raise [JavaScriptError] when the node is not a `select`.
643
+ #
644
+ # @raise [TimeoutError]
645
+ #
646
+ # @example
647
+ # page.at_css("select#title").selected.map(&:text) # => ["Mrs"]
648
+ #
403
649
  def selected
404
650
  evaluate(<<~JS)
405
651
  function() {
@@ -425,6 +671,8 @@ module Ferrum
425
671
  #
426
672
  # @return [self]
427
673
  #
674
+ # @raise [TimeoutError]
675
+ #
428
676
  # @example
429
677
  # page.at_xpath("//*[select]").select(["1"]) # => Node (select)
430
678
  # page.at_xpath("//*[select]").select(["text"], by: :text) # => Node (select)
@@ -465,6 +713,8 @@ module Ferrum
465
713
  #
466
714
  # @return [Object]
467
715
  #
716
+ # @raise [TimeoutError]
717
+ #
468
718
  # @example
469
719
  # page.at_css("input").evaluate("this.value")
470
720
  # page.at_css("input").evaluate("this.value + suffix", suffix: "!")
@@ -481,6 +731,13 @@ module Ferrum
481
731
  # @option (see Frame::Runtime#evaluate)
482
732
  #
483
733
  # @return [RemoteObject, Node, Object]
734
+ # A {Node} for a node and the value itself for a primitive.
735
+ #
736
+ # @raise [TimeoutError]
737
+ #
738
+ # @example
739
+ # page.at_css("form").evaluate_handle("this.dataset") # => RemoteObject
740
+ # page.at_css("form").evaluate_handle("this") # => Node
484
741
  #
485
742
  def evaluate_handle(expression, *positional, **keywords)
486
743
  @runtime.evaluate_in(self, expression, positional, keywords, mode: :handle)
@@ -494,15 +751,17 @@ module Ferrum
494
751
  # @param (see Frame::Runtime#evaluate)
495
752
  # @option (see Frame::Runtime#evaluate)
496
753
  #
497
- # @return [Boolean]
754
+ # @return [self]
498
755
  # Always `true`.
499
756
  #
757
+ # @raise [TimeoutError]
758
+ #
500
759
  # @example
501
760
  # page.at_css("input").execute("this.value = text", text: "hello")
502
761
  #
503
762
  def execute(expression, *positional, **keywords)
504
763
  @runtime.evaluate_in(self, expression, positional, keywords, mode: :none)
505
- true
764
+ self
506
765
  end
507
766
 
508
767
  #
@@ -532,9 +791,52 @@ module Ferrum
532
791
  %(@backend_node_id=#{@backend_node_id} @tag_name=#{@tag_name.inspect}>)
533
792
  end
534
793
 
794
+ #
795
+ # The point a click on the node lands on, in viewport coordinates, without clicking. Waits for the node to stop
796
+ # moving, see {#wait_still}.
797
+ #
798
+ # @param [Numeric] x
799
+ # Horizontal offset from the reference point.
800
+ #
801
+ # @param [Numeric] y
802
+ # Vertical offset from the reference point.
803
+ #
804
+ # @param [:center, :top] position
805
+ # What `x` and `y` are measured from: the node's center or its top-left corner.
806
+ #
807
+ # @return [(Float, Float)]
808
+ #
809
+ # @raise [ArgumentError]
810
+ # When `position` is neither `:center` nor `:top`.
811
+ #
812
+ # @raise [CoordinatesNotFoundError]
813
+ # When the node has no box, e.g. it isn't displayed.
814
+ #
815
+ # @raise [TimeoutError]
816
+ #
817
+ # @example
818
+ # page.at_css("input").pinpoint # => [126.7, 401.4]
819
+ # page.at_css("input").pinpoint(x: 5, y: 5) # => [131.7, 406.4]
820
+ # page.at_css("input").pinpoint(x: 5, y: 5, position: :top) # => [55.2, 395.9]
821
+ #
822
+ def pinpoint(x: 0, y: 0, position: :center)
823
+ raise ArgumentError, "position: should be :center or :top" unless %i[center top].include?(position)
824
+
825
+ corners = to_points(settled_quads.first)
826
+ left, top = position == :top ? corners.first.values_at(:x, :y) : center_of(corners)
827
+ [left + x, top + y]
828
+ rescue CoordinatesNotFoundError
829
+ left, top, width, height = bounding_rect
830
+ raise if (left + (width / 2.0)).zero? && (top + (height / 2.0)).zero?
831
+
832
+ position == :top ? [left + x, top + y] : [left + (width / 2.0) + x, top + (height / 2.0) + y]
833
+ end
834
+
535
835
  #
536
836
  # Finds the x, y coordinates to click or hover on the node.
537
837
  #
838
+ # @deprecated Will be removed in Ferrum 0.22. Use {#pinpoint}.
839
+ #
538
840
  # @param [Integer, nil] x
539
841
  # Horizontal offset from the reference point.
540
842
  #
@@ -545,10 +847,13 @@ module Ferrum
545
847
  # `:top` to offset from the node's top-left corner, `:center` to offset
546
848
  # from its center.
547
849
  #
548
- # @return [(Integer, Integer)]
850
+ # @return [(Float, Float)]
851
+ #
852
+ # @raise [TimeoutError]
549
853
  #
550
854
  def find_position(x: nil, y: nil, position: :top)
551
- points = wait_for_stop_moving.map { |q| to_points(q) }.first
855
+ Utils::Deprecate.warn(FIND_POSITION_DEPRECATED, user_code: true)
856
+ points = settled_quads.map { |q| to_points(q) }.first
552
857
  get_position(points, x, y, position)
553
858
  rescue CoordinatesNotFoundError
554
859
  x, y = bounding_rect_coordinates
@@ -557,7 +862,16 @@ module Ferrum
557
862
  [x, y]
558
863
  end
559
864
 
560
- # Returns a hash of the computed styles for the node
865
+ #
866
+ # The computed CSS styles of the node.
867
+ #
868
+ # @return [Hash{String => String}]
869
+ #
870
+ # @raise [TimeoutError]
871
+ #
872
+ # @example
873
+ # page.at_css("a").computed_style["display"] # => "inline"
874
+ #
561
875
  def computed_style
562
876
  page
563
877
  .command("CSS.getComputedStyleForNode", nodeId: node_id)["computedStyle"]
@@ -568,25 +882,54 @@ module Ferrum
568
882
  # element is ignored by the accessibility tree.
569
883
  #
570
884
  # @return [Accessibility::AXNode, nil]
571
- def axnode
885
+ #
886
+ # @raise [TimeoutError]
887
+ #
888
+ # @example
889
+ # page.at_css("a").ax_node.role # => "link"
890
+ #
891
+ def ax_node
572
892
  page.accessibility.node_for(self)
573
893
  end
574
894
 
895
+ #
896
+ # @deprecated Will be removed in Ferrum 0.22. Use {#ax_node}.
897
+ #
898
+ # @return [Accessibility::AXNode, nil]
899
+ #
900
+ # @raise [TimeoutError]
901
+ #
902
+ def axnode
903
+ Utils::Deprecate.warn("axnode will be removed in Ferrum 0.22. Use ax_node instead.", user_code: true)
904
+ ax_node
905
+ end
906
+
575
907
  #
576
908
  # Removes the node from the DOM.
577
909
  #
578
- # @return [Hash{String => Object}]
910
+ # @return [self]
911
+ #
912
+ # @raise [TimeoutError]
579
913
  #
580
914
  # @example
581
915
  # page.at_css("#ad").remove
582
916
  #
583
917
  def remove
584
918
  page.command("DOM.removeNode", nodeId: node_id)
919
+ self
585
920
  end
586
921
 
587
922
  # Whether the node still exists in the DOM.
588
923
  #
589
924
  # @return [Boolean]
925
+ #
926
+ # @raise [TimeoutError]
927
+ #
928
+ # @example
929
+ # node = page.at_css("#ad")
930
+ # node.remove
931
+ # node.exists? # => false
932
+ #
590
933
  def exists?
591
934
  page.command("DOM.resolveNode", nodeId: node_id)
592
935
  true
@@ -596,10 +939,26 @@ module Ferrum
596
939
 
597
940
  private
598
941
 
942
+ def click_mode(mode)
943
+ button, count, hint = CLICK_MODES.fetch(mode) { raise ArgumentError, "unknown mode: #{mode.inspect}" }
944
+ Utils::Deprecate.warn("mode: will be removed in Ferrum 0.22, #{hint}.", user_code: true)
945
+ [button, count]
946
+ end
947
+
948
+ def center_of(points)
949
+ [points.sum { |p| p[:x] } / 4.0, points.sum { |p| p[:y] } / 4.0]
950
+ end
951
+
952
+ def bounding_rect
953
+ evaluate("function() { const r = this.getBoundingClientRect(); return [r.left, r.top, r.width, r.height]; }")
954
+ end
955
+
599
956
  def bounding_rect_coordinates
600
- evaluate <<~JS
601
- [this.getBoundingClientRect().left + (this.offsetWidth / 2),
602
- this.getBoundingClientRect().top + (this.offsetHeight / 2)]
957
+ evaluate(<<~JS)
958
+ function() {
959
+ const { left, top, width, height } = this.getBoundingClientRect();
960
+ return [left + (width / 2), top + (height / 2)];
961
+ }
603
962
  JS
604
963
  end
605
964
 
@@ -610,6 +969,15 @@ module Ferrum
610
969
  quads
611
970
  end
612
971
 
972
+ def settled_quads(delay: MOVING_WAIT_DELAY, attempts: MOVING_WAIT_ATTEMPTS)
973
+ Utils::Attempt.with_retry(errors: NodeMovingError, max: attempts, wait: 0) do
974
+ previous, current = content_quads_with(delay: delay)
975
+ raise NodeMovingError.new(self, previous, current) if previous != current
976
+
977
+ current
978
+ end
979
+ end
980
+
613
981
  def content_quads_with(delay: MOVING_WAIT_DELAY)
614
982
  previous = content_quads
615
983
  sleep(delay)