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/page.rb CHANGED
@@ -13,11 +13,13 @@ require "ferrum/accessibility"
13
13
  require "ferrum/interceptable"
14
14
  require "ferrum/downloads"
15
15
  require "ferrum/page/frames"
16
+ require "ferrum/page/navigation"
16
17
  require "ferrum/page/screencast"
17
18
  require "ferrum/page/screenshot"
18
19
  require "ferrum/page/animation"
19
20
  require "ferrum/page/tracing"
20
21
  require "ferrum/page/stream"
22
+ require "ferrum/page/cookies"
21
23
 
22
24
  module Ferrum
23
25
  #
@@ -28,26 +30,36 @@ module Ferrum
28
30
  # JavaScript evaluation methods are ultimately delegated to from {Browser}.
29
31
  #
30
32
  class Page
31
- GOTO_WAIT = ENV.fetch("FERRUM_GOTO_WAIT", 0.1).to_f
33
+ NAVIGATION_WAIT = Utils::Deprecate.renamed_env(
34
+ "FERRUM_NAVIGATION_WAIT", %w[FERRUM_CLICK_WAIT FERRUM_GOTO_WAIT], 0.1
35
+ )
36
+ WAIT_INTERVAL = 0.05
37
+ WAIT_FOR_NAVIGATION_DEPRECATED = "wait_for_navigation will be removed in Ferrum 0.22. Use " \
38
+ "await_navigation(watch: seconds) { ... } instead."
39
+ WAIT_FOR_RELOAD_DEPRECATED = "wait_for_reload will be removed in Ferrum 0.22. Use await_navigation { ... } " \
40
+ "around the code that reloads the page."
32
41
 
33
42
  extend Forwardable
34
43
 
35
- delegate %i[at_css at_xpath css xpath wait_for_selector
36
- current_url current_title url title body doctype content=
37
- execution_id execution_id! evaluate evaluate_handle execute
38
- evaluate_on evaluate_async evaluate_func isolated_world
44
+ delegate %i[at_css at_xpath css xpath wait_css wait_css! wait_xpath wait_xpath! wait_for_selector
45
+ current_url current_title url title html body doctype content=
46
+ execution_id execution_id! evaluate evaluate_handle execute isolated_world
39
47
  add_script_tag add_style_tag] => :main_frame
48
+ delegate %i[await_download await_download!] => :downloads
40
49
  delegate %i[base_url default_user_agent timeout timeout=] => :@options
41
50
 
42
51
  include Animation
43
52
  include Screencast
44
53
  include Screenshot
45
54
  include Frames
55
+ include Navigation
46
56
  include Stream
47
57
  include Interceptable
48
58
 
49
- attr_accessor :referrer
50
- attr_reader :context_id, :target_id, :event, :tracing
59
+ attr_reader :context_id, :target_id, :tracing
60
+
61
+ # @api private
62
+ attr_reader :idle
51
63
 
52
64
  # Client connection.
53
65
  #
@@ -84,9 +96,9 @@ module Ferrum
84
96
  # @return [Headers]
85
97
  attr_reader :headers
86
98
 
87
- # Cookie store.
99
+ # The cookies the page sees, `context.cookies` has all of the context's.
88
100
  #
89
- # @return [Cookies]
101
+ # @return [Page::Cookies]
90
102
  attr_reader :cookies
91
103
 
92
104
  # Downloads object.
@@ -98,9 +110,12 @@ module Ferrum
98
110
  # `DOM.getDocument`, both invalidate every node id handed out before them.
99
111
  #
100
112
  # @return [Integer]
113
+ #
114
+ # @api private
101
115
  attr_reader :document_version
102
116
 
103
117
  def initialize(client, context_id:, target_id:, proxy: nil)
118
+ @javascript = true
104
119
  @client = client
105
120
  @context_id = context_id
106
121
  @target_id = target_id
@@ -108,7 +123,7 @@ module Ferrum
108
123
 
109
124
  @frames = Concurrent::Map.new
110
125
  @main_frame = Frame.new(nil, self)
111
- @event = Utils::Event.new.tap(&:set)
126
+ @idle = Concurrency::Cell.set(true)
112
127
  @document_version = 0
113
128
  self.proxy = proxy
114
129
 
@@ -126,49 +141,18 @@ module Ferrum
126
141
  prepare_page
127
142
  end
128
143
 
129
- #
130
- # Navigates the page to a URL.
131
- #
132
- # @param [String, nil] url
133
- # The URL to navigate to. The url should include scheme unless you set
134
- # `{Browser#base_url = url}` when configuring.
135
- #
136
- # @example
137
- # page.go_to("https://github.com/")
138
- #
139
- def go_to(url = nil)
140
- options = { url: combine_url!(url) }
141
- options.merge!(referrer: referrer) if referrer
142
- response = command("Page.navigate", wait: GOTO_WAIT, **options)
143
- error_text = response["errorText"] # https://cs.chromium.org/chromium/src/net/base/net_error_list.h
144
- if error_text && error_text != "net::ERR_ABORTED" # Request aborted due to user action or download
145
- raise StatusError.new(options[:url], "Request to #{options[:url]} failed (#{error_text})")
146
- end
147
-
148
- response["frameId"]
149
- rescue TimeoutError
150
- if @options.pending_connection_errors
151
- pendings = network.traffic.select(&:pending?).map(&:url).compact
152
- raise PendingConnectionsError.new(options[:url], Array(pendings))
153
- end
154
- end
155
- alias goto go_to
156
- alias go go_to
157
-
158
144
  #
159
145
  # Closes the page's target and its underlying client connection.
160
146
  #
161
- # @return [Boolean]
147
+ # @return [nil]
162
148
  #
163
149
  # @example
164
150
  # page.close # => true
165
151
  #
166
152
  def close
167
- @headers.clear
168
- client.command("Target.closeTarget", async: true, targetId: @target_id)
153
+ client.notify("Target.closeTarget", targetId: @target_id)
169
154
  close_connection
170
-
171
- true
155
+ nil
172
156
  end
173
157
 
174
158
  #
@@ -195,6 +179,10 @@ module Ferrum
195
179
  # a desktop and leaves touch emulation as is. Turning touch off resets hover and pointer to the detected input
196
180
  # devices, which undoes `--blink-settings` overrides until the next navigation
197
181
  #
182
+ # @return [nil]
183
+ #
184
+ # @raise [TimeoutError]
185
+ #
198
186
  def set_viewport(width: 0, height: 0, scale_factor: 0, mobile: nil)
199
187
  command("Emulation.setTouchEmulationEnabled", enabled: mobile, maxTouchPoints: 1) unless mobile.nil?
200
188
 
@@ -206,6 +194,7 @@ module Ferrum
206
194
  deviceScaleFactor: scale_factor,
207
195
  mobile: mobile || false
208
196
  )
197
+ nil
209
198
  end
210
199
 
211
200
  #
@@ -220,7 +209,9 @@ module Ferrum
220
209
  # mode. When `true`, `width` and `height` are read from
221
210
  # {#document_size} instead of the given arguments.
222
211
  #
223
- # @return [Hash{String => Object}]
212
+ # @return [nil]
213
+ #
214
+ # @raise [TimeoutError]
224
215
  #
225
216
  # @example
226
217
  # page.resize(width: 1024, height: 768)
@@ -241,11 +232,43 @@ module Ferrum
241
232
  end
242
233
 
243
234
  #
244
- # Disables JavaScript execution from the HTML source for the page.
235
+ # Turns the page's own scripts on or off. Scripts skipped while they were off don't run when they are turned back
236
+ # on, reload the page for that. JavaScript evaluated with Ferrum runs either way.
237
+ #
238
+ # @param [Boolean] enabled
239
+ #
240
+ # @raise [TimeoutError]
241
+ #
242
+ # @example
243
+ # page.javascript = false
244
+ # page.go_to("https://github.com/")
245
+ # page.javascript = true
246
+ # page.refresh
247
+ #
248
+ def javascript=(enabled)
249
+ command("Emulation.setScriptExecutionDisabled", value: !enabled)
250
+ @javascript = enabled
251
+ end
252
+
253
+ #
254
+ # Whether the page's own scripts run, see {#javascript=}.
255
+ #
256
+ # @return [Boolean]
257
+ #
258
+ def javascript?
259
+ @javascript
260
+ end
261
+
262
+ #
263
+ # @deprecated Will be removed in Ferrum 0.22. Use `javascript = false`.
245
264
  #
246
- # This doesn't prevent users evaluate JavaScript with Ferrum.
265
+ # @return [Hash]
247
266
  #
267
+ # @raise [TimeoutError]
248
268
  def disable_javascript
269
+ Utils::Deprecate.warn("disable_javascript will be removed in Ferrum 0.22. Use javascript = false instead.",
270
+ user_code: true)
271
+ @javascript = false
249
272
  command("Emulation.setScriptExecutionDisabled", value: true)
250
273
  end
251
274
 
@@ -255,6 +278,8 @@ module Ferrum
255
278
  # @return [(Integer, Integer)]
256
279
  # The left, top coordinates of the window.
257
280
  #
281
+ # @raise [TimeoutError]
282
+ #
258
283
  # @example
259
284
  # page.position # => [10, 20]
260
285
  #
@@ -273,6 +298,8 @@ module Ferrum
273
298
  # @option options [Integer] :top
274
299
  # The number of pixels from the top of the screen.
275
300
  #
301
+ # @raise [TimeoutError]
302
+ #
276
303
  # @example
277
304
  # page.position = { left: 10, top: 20 }
278
305
  #
@@ -299,6 +326,8 @@ module Ferrum
299
326
  # @option options [String] :window_state
300
327
  # The window state. Default to normal. Allowed Values: normal, minimized, maximized, fullscreen
301
328
  #
329
+ # @raise [TimeoutError]
330
+ #
302
331
  # @example
303
332
  # page.window_bounds = { left: 10, top: 20, width: 1024, height: 768, window_state: "normal" }
304
333
  #
@@ -307,7 +336,7 @@ module Ferrum
307
336
  window_state = options.delete(:window_state)
308
337
  bounds = { windowState: window_state, **options }.compact
309
338
 
310
- client.command("Browser.setWindowBounds", windowId: window_id, bounds: bounds)
339
+ command("Browser.setWindowBounds", windowId: window_id, bounds: bounds)
311
340
  end
312
341
 
313
342
  #
@@ -315,11 +344,13 @@ module Ferrum
315
344
  #
316
345
  # @return [Hash{String => (Integer, String)}]
317
346
  #
347
+ # @raise [TimeoutError]
348
+ #
318
349
  # @example
319
350
  # page.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" }
320
351
  #
321
352
  def window_bounds
322
- client.command("Browser.getWindowBounds", windowId: window_id).fetch("bounds")
353
+ command("Browser.getWindowBounds", windowId: window_id).fetch("bounds")
323
354
  end
324
355
 
325
356
  #
@@ -327,126 +358,85 @@ module Ferrum
327
358
  #
328
359
  # @return [Integer]
329
360
  #
361
+ # @raise [TimeoutError]
362
+ #
330
363
  # @example
331
364
  # page.window_id # => 1
332
365
  #
333
366
  def window_id
334
- client.command("Browser.getWindowForTarget", targetId: target_id)["windowId"]
367
+ command("Browser.getWindowForTarget", targetId: target_id)["windowId"]
335
368
  end
336
369
 
337
370
  #
338
- # Reloads the current page.
371
+ # Runs the block until it returns a truthy value. The calls in the block
372
+ # share the timeout.
339
373
  #
340
- # @example
341
- # page.go_to("https://github.com/")
342
- # page.refresh
374
+ # @param [Numeric, Deadline, nil] timeout
375
+ # How long to keep trying, the page's `timeout` by default.
343
376
  #
344
- def refresh
345
- command("Page.reload", wait: timeout, slowmoable: true)
346
- end
347
- alias reload refresh
348
-
377
+ # @param [Numeric] interval
378
+ # Seconds to sleep between the attempts.
349
379
  #
350
- # Stop all navigations and loading pending resources on the page.
380
+ # @yield The check to repeat.
351
381
  #
352
- # @example
353
- # page.go_to("https://github.com/")
354
- # page.stop
355
- #
356
- def stop
357
- command("Page.stopLoading", slowmoable: true)
358
- end
359
-
360
- #
361
- # Navigates to the previous URL in the history.
382
+ # @return [Object, nil]
383
+ # What the block returned, `nil` if it didn't return a truthy value in time.
362
384
  #
363
385
  # @example
364
- # page.go_to("https://github.com/")
365
- # page.at_xpath("//a").click
366
- # page.back
386
+ # page.wait_until { page.evaluate("window.ready") } # => true
367
387
  #
368
- def back
369
- history_navigate(delta: -1)
388
+ def wait_until(timeout: nil, interval: WAIT_INTERVAL, &)
389
+ Utils::Poll.call(timeout || self.timeout, interval, &)
370
390
  end
371
391
 
372
392
  #
373
- # Navigates to the next URL in the history.
393
+ # Same as {#wait_until}, but raises if the block doesn't return a truthy
394
+ # value in time.
374
395
  #
375
- # @example
376
- # page.go_to("https://github.com/")
377
- # page.at_xpath("//a").click
378
- # page.back
379
- # page.forward
396
+ # @param [Hash] options
397
+ # The options of {#wait_until}.
380
398
  #
381
- def forward
382
- history_navigate(delta: 1)
383
- end
384
-
399
+ # @yield The check to repeat.
385
400
  #
386
- # Blocks until the page reloads or the timeout is reached.
401
+ # @return [Object]
387
402
  #
388
- # @param [Numeric] timeout
389
- # Maximum time in seconds to wait for a reload event.
403
+ # @raise [TimeoutError]
404
+ # If the block doesn't return a truthy value in time.
390
405
  #
391
- # @example
392
- # page.wait_for_reload
393
- #
394
- def wait_for_reload(timeout = 1)
395
- @event.reset if @event.set?
396
- @event.wait(timeout)
397
- @event.set
406
+ def wait_until!(**options, &)
407
+ wait_until(**options, &) || raise(TimeoutError)
398
408
  end
399
409
 
400
410
  #
401
- # Runs the block and, if it starts a navigation of the main frame within
402
- # `wait` seconds, waits for that navigation to finish, e.g. after a click
403
- # on a link made from JavaScript.
404
- #
405
- # @param [Numeric] wait
406
- # How many seconds to watch for a navigation after the block returns.
411
+ # Turns bypassing the page's Content Security Policy on or off. It applies from the next navigation.
407
412
  #
408
- # @yield The code that may start a navigation.
413
+ # @param [Boolean] enabled
409
414
  #
410
- # @return [Boolean]
411
- # `false` when a navigation started but didn't finish within the page's
412
- # `timeout`.
415
+ # @raise [TimeoutError]
413
416
  #
414
417
  # @example
415
- # page.wait_for_navigation { page.at_css("a").evaluate("this.click()") } # => true
416
- #
417
- def wait_for_navigation(wait = Mouse::CLICK_WAIT)
418
- iteration = @event.reset
419
- yield
420
- @event.wait(wait)
421
- iteration == @event.iteration || @event.wait(timeout)
422
- end
423
-
424
- #
425
- # Waits for a navigation the block starts or raises {Ferrum::TimeoutError}.
426
- # Accepts the same arguments as {#wait_for_navigation}.
427
- #
428
- # @raise [Ferrum::TimeoutError]
418
+ # page.bypass_csp = true
419
+ # page.go_to("https://github.com/ruby-concurrency/concurrent-ruby/blob/master/docs-source/promises.in.md")
420
+ # page.add_script_tag(content: "window.__injected = 42")
421
+ # page.evaluate("window.__injected") # => 42
429
422
  #
430
- def wait_for_navigation!(...)
431
- raise TimeoutError unless wait_for_navigation(...)
423
+ def bypass_csp=(enabled)
424
+ command("Page.setBypassCSP", enabled: enabled)
432
425
  end
433
426
 
434
427
  #
435
- # Enables/disables CSP bypass.
428
+ # @deprecated Will be removed in Ferrum 0.22. Use `bypass_csp = true`.
436
429
  #
437
430
  # @param [Boolean] enabled
438
431
  #
439
432
  # @return [Boolean]
440
433
  #
441
- # @example
442
- # page.bypass_csp # => true
443
- # page.go_to("https://github.com/ruby-concurrency/concurrent-ruby/blob/master/docs-source/promises.in.md")
444
- # page.refresh
445
- # page.add_script_tag(content: "window.__injected = 42")
446
- # page.evaluate("window.__injected") # => 42
434
+ # @raise [TimeoutError]
447
435
  #
448
436
  def bypass_csp(enabled: true)
449
- command("Page.setBypassCSP", enabled: enabled)
437
+ Utils::Deprecate.warn("bypass_csp(enabled:) will be removed in Ferrum 0.22. Use bypass_csp = true instead.",
438
+ user_code: true)
439
+ self.bypass_csp = enabled
450
440
  enabled
451
441
  end
452
442
 
@@ -465,6 +455,8 @@ module Ferrum
465
455
  # @return [String]
466
456
  # The script identifier, for {#remove_script_on_new_document}.
467
457
  #
458
+ # @raise [TimeoutError]
459
+ #
468
460
  # @example
469
461
  # page.evaluate_on_new_document <<~JS
470
462
  # Object.defineProperty(navigator, "languages", {
@@ -482,61 +474,62 @@ module Ferrum
482
474
  #
483
475
  # @param [String] identifier
484
476
  #
485
- # @return [true]
477
+ # @return [nil]
478
+ #
479
+ # @raise [TimeoutError]
486
480
  #
487
481
  def remove_script_on_new_document(identifier)
488
482
  command("Page.removeScriptToEvaluateOnNewDocument", identifier: identifier)
489
- true
483
+ nil
490
484
  end
491
485
 
492
486
  #
493
487
  # Activates (focuses) the target for the given page.
494
488
  # When you have multiple tabs you work with, and you need to switch a given one.
495
489
  #
496
- # @return [Boolean]
490
+ # @return [nil]
491
+ #
492
+ # @raise [TimeoutError]
497
493
  #
498
494
  # @example
499
495
  # page.activate # => true
500
496
  #
501
497
  def activate
502
498
  command("Target.activateTarget", targetId: target_id)
503
- true
499
+ nil
504
500
  end
505
501
 
506
502
  #
507
- # Sends a CDP command to the browser and optionally waits for network
508
- # activity on the main frame to settle before returning.
503
+ # Sends a CDP command to the browser. To wait for a navigation it starts,
504
+ # wrap it in {#await_navigation}.
509
505
  #
510
506
  # @param [String] method
511
507
  # The CDP method name, e.g. `"Page.navigate"`.
512
508
  #
513
- # @param [Numeric] wait
514
- # How many seconds to wait for a network event on the main frame after
515
- # the command is sent. `0` disables waiting.
516
- #
517
509
  # @param [Boolean] slowmoable
518
510
  # Whether to sleep for `Browser::Options#slowmo` seconds before sending
519
511
  # the command.
520
512
  #
521
- # @param [Numeric, nil] timeout
513
+ # @param [Numeric, Deadline, nil] timeout
522
514
  # Overrides the timeout this command's response is bound by. Defaults
523
515
  # to the page's `timeout`. Callers with their own budget (e.g. `#pdf`/
524
516
  # `#screenshot`) pass it explicitly.
525
517
  #
526
518
  # @return [Hash{String => Object}]
527
519
  #
520
+ # @raise [TimeoutError]
521
+ #
522
+ # @raise [ArgumentError]
523
+ # When given the removed `wait:` or `async:`, which would otherwise be sent to the browser as parameters.
524
+ #
528
525
  # @example
529
- # page.command("Page.navigate", url: "https://github.com/")
526
+ # page.command("Runtime.evaluate", expression: "1 + 1")
530
527
  #
531
- def command(method, wait: 0, slowmoable: false, timeout: nil, **params)
532
- if wait.positive?
533
- result = nil
534
- wait_for_navigation!(wait) { result = command(method, slowmoable:, timeout:, **params) }
535
- return result
536
- end
528
+ def command(method, slowmoable: false, timeout: nil, **params)
529
+ raise ArgumentError, "wait: is removed, wrap #{method} in await_navigation instead" if params.key?(:wait)
537
530
 
538
531
  sleep(@options.slowmo) if slowmoable && @options.slowmo.positive?
539
- client.command(method, timeout: timeout || self.timeout, **params)
532
+ client.command(method, timeout: Deadline.wrap(timeout || self.timeout), **params)
540
533
  end
541
534
 
542
535
  # Subscribes to a CDP event, or to `:dialog`, `:request`, `:response`,
@@ -546,13 +539,10 @@ module Ferrum
546
539
  #
547
540
  # @return [Integer]
548
541
  # The subscription id, used to unsubscribe via {#off}.
549
- def on(name, &block)
542
+ def on(name, &)
550
543
  return super unless name == :dialog
551
544
 
552
- client.on("Page.javascriptDialogOpening") do |params, index, total|
553
- dialog = Dialog.new(self, params)
554
- block.call(dialog, index, total)
555
- end
545
+ chain(:dialog).add(&)
556
546
  end
557
547
 
558
548
  # Unsubscribes a listener previously registered via {#on}.
@@ -566,12 +556,14 @@ module Ferrum
566
556
  def off(name, id)
567
557
  return super unless name == :dialog
568
558
 
569
- client.off("Page.javascriptDialogOpening", id)
559
+ chain(:dialog).remove(id)
570
560
  end
571
561
 
572
562
  # Whether the page is configured to use a proxy.
573
563
  #
574
564
  # @return [Boolean]
565
+ #
566
+ # @api private
575
567
  def use_proxy?
576
568
  @proxy_host && @proxy_port
577
569
  end
@@ -579,6 +571,8 @@ module Ferrum
579
571
  # Whether the page is configured to use a proxy that requires authentication.
580
572
  #
581
573
  # @return [Boolean]
574
+ #
575
+ # @api private
582
576
  def use_authorized_proxy?
583
577
  use_proxy? && @proxy_user && @proxy_password
584
578
  end
@@ -592,9 +586,12 @@ module Ferrum
592
586
  # @return [Integer, Boolean]
593
587
  # The root node id, or `true` when sent asynchronously.
594
588
  #
589
+ # @raise [TimeoutError]
590
+ #
591
+ # @api private
595
592
  def document_node_id(async: false)
596
593
  result = if async
597
- client.command("DOM.getDocument", async: true, depth: 0)
594
+ client.notify("DOM.getDocument", depth: 0)
598
595
  else
599
596
  command("DOM.getDocument", depth: 0).dig("root", "nodeId")
600
597
  end
@@ -619,13 +616,21 @@ module Ferrum
619
616
  end
620
617
  end
621
618
 
622
- on(:dialog) do |dialog, _index, total|
623
- if total == 1
624
- warn "Dialog was shown but you didn't provide `on(:dialog)` callback, accepting it by default. " \
625
- "Please take a look at https://github.com/rubycdp/ferrum#dialogs"
626
- dialog.accept
627
- end
628
- end
619
+ chain(:dialog)
620
+ end
621
+
622
+ def build_chain(name)
623
+ return super unless name == :dialog
624
+
625
+ Chain.new(client, "Page.javascriptDialogOpening",
626
+ build: ->(params) { Dialog.new(self, params) },
627
+ default: method(:accept_dialog), persistent: true)
628
+ end
629
+
630
+ def accept_dialog(dialog)
631
+ warn "Dialog was shown but no `on(:dialog)` callback handled it, accepting it by default. " \
632
+ "Please take a look at https://github.com/rubycdp/ferrum#dialogs"
633
+ dialog.accept
629
634
  end
630
635
 
631
636
  def prepare_page
@@ -640,9 +645,7 @@ module Ferrum
640
645
  if use_authorized_proxy?
641
646
  network.authorize(user: @proxy_user,
642
647
  password: @proxy_password,
643
- type: :proxy) do |request, _index, total|
644
- request.continue if total == 1
645
- end
648
+ type: :proxy) { |_request| nil }
646
649
  end
647
650
 
648
651
  downloads.set_behavior(save_path: @options.save_path) if @options.save_path
@@ -659,31 +662,6 @@ module Ferrum
659
662
  document_node_id
660
663
  end
661
664
 
662
- def history_navigate(delta:)
663
- history = command("Page.getNavigationHistory")
664
- index, entries = history.values_at("currentIndex", "entries")
665
- entry = entries[index + delta]
666
-
667
- return unless entry
668
-
669
- # Potential wait because of network event
670
- command("Page.navigateToHistoryEntry",
671
- wait: Mouse::CLICK_WAIT,
672
- slowmoable: true,
673
- entryId: entry["id"])
674
- end
675
-
676
- def combine_url!(url_or_path)
677
- url = Addressable::URI.parse(url_or_path)
678
- nil_or_relative = url.nil? || url.relative?
679
-
680
- if nil_or_relative && !@options.base_url
681
- raise "Set :base_url browser's option or use absolute url in `go_to`, you passed: #{url_or_path}"
682
- end
683
-
684
- (nil_or_relative ? @options.base_url.join(url.to_s) : url).to_s
685
- end
686
-
687
665
  def proxy=(options)
688
666
  @proxy_host = options&.[](:host) || @options.proxy&.[](:host)
689
667
  @proxy_port = options&.[](:port) || @options.proxy&.[](:port)
@@ -50,6 +50,8 @@ module Ferrum
50
50
  #
51
51
  # @return [Object]
52
52
  #
53
+ # @raise [TimeoutError]
54
+ #
53
55
  def value
54
56
  @runtime.evaluate_in(self, "this", [], {})
55
57
  end
@@ -59,6 +61,8 @@ module Ferrum
59
61
  #
60
62
  # @return [void]
61
63
  #
64
+ # @raise [TimeoutError]
65
+ #
62
66
  def release
63
67
  @page.command("Runtime.releaseObject", objectId: @remote_id)
64
68
  nil