puppeteer-bidi 0.0.1.beta1 → 0.0.1.beta3

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 (84) hide show
  1. checksums.yaml +4 -4
  2. data/CLAUDE.md +50 -4
  3. data/README.md +8 -1
  4. data/Steepfile +36 -0
  5. data/lib/puppeteer/bidi/async_utils.rb +1 -0
  6. data/lib/puppeteer/bidi/browser.rb +38 -24
  7. data/lib/puppeteer/bidi/browser_context.rb +13 -4
  8. data/lib/puppeteer/bidi/browser_launcher.rb +1 -0
  9. data/lib/puppeteer/bidi/connection.rb +26 -9
  10. data/lib/puppeteer/bidi/core/browser.rb +15 -14
  11. data/lib/puppeteer/bidi/core/browsing_context.rb +64 -46
  12. data/lib/puppeteer/bidi/core/disposable.rb +10 -3
  13. data/lib/puppeteer/bidi/core/errors.rb +18 -1
  14. data/lib/puppeteer/bidi/core/event_emitter.rb +18 -9
  15. data/lib/puppeteer/bidi/core/navigation.rb +4 -3
  16. data/lib/puppeteer/bidi/core/realm.rb +29 -26
  17. data/lib/puppeteer/bidi/core/request.rb +33 -29
  18. data/lib/puppeteer/bidi/core/session.rb +14 -11
  19. data/lib/puppeteer/bidi/core/user_context.rb +17 -16
  20. data/lib/puppeteer/bidi/core/user_prompt.rb +8 -7
  21. data/lib/puppeteer/bidi/core.rb +1 -0
  22. data/lib/puppeteer/bidi/deserializer.rb +1 -0
  23. data/lib/puppeteer/bidi/element_handle.rb +52 -52
  24. data/lib/puppeteer/bidi/errors.rb +1 -0
  25. data/lib/puppeteer/bidi/file_chooser.rb +1 -0
  26. data/lib/puppeteer/bidi/frame.rb +70 -70
  27. data/lib/puppeteer/bidi/http_response.rb +1 -0
  28. data/lib/puppeteer/bidi/injected_source.rb +1 -0
  29. data/lib/puppeteer/bidi/js_handle.rb +23 -23
  30. data/lib/puppeteer/bidi/keyboard.rb +24 -13
  31. data/lib/puppeteer/bidi/lazy_arg.rb +1 -0
  32. data/lib/puppeteer/bidi/mouse.rb +31 -18
  33. data/lib/puppeteer/bidi/page.rb +87 -87
  34. data/lib/puppeteer/bidi/query_handler.rb +3 -0
  35. data/lib/puppeteer/bidi/realm.rb +70 -6
  36. data/lib/puppeteer/bidi/serializer.rb +1 -0
  37. data/lib/puppeteer/bidi/target.rb +24 -0
  38. data/lib/puppeteer/bidi/task_manager.rb +9 -3
  39. data/lib/puppeteer/bidi/timeout_settings.rb +1 -0
  40. data/lib/puppeteer/bidi/transport.rb +1 -0
  41. data/lib/puppeteer/bidi/version.rb +1 -1
  42. data/lib/puppeteer/bidi/wait_task.rb +1 -0
  43. data/lib/puppeteer/bidi.rb +8 -4
  44. data/sig/_external.rbs +128 -0
  45. data/sig/_supplementary.rbs +20 -0
  46. data/sig/puppeteer/bidi/async_utils.rbs +68 -0
  47. data/sig/puppeteer/bidi/browser.rbs +35 -26
  48. data/sig/puppeteer/bidi/browser_context.rbs +39 -0
  49. data/sig/puppeteer/bidi/browser_launcher.rbs +45 -0
  50. data/sig/puppeteer/bidi/connection.rbs +67 -0
  51. data/sig/puppeteer/bidi/core/browser.rbs +79 -0
  52. data/sig/puppeteer/bidi/core/browsing_context.rbs +210 -0
  53. data/sig/puppeteer/bidi/core/disposable.rbs +42 -0
  54. data/sig/puppeteer/bidi/core/errors.rbs +65 -0
  55. data/sig/puppeteer/bidi/core/event_emitter.rbs +50 -0
  56. data/sig/puppeteer/bidi/core/navigation.rbs +37 -0
  57. data/sig/puppeteer/bidi/core/realm.rbs +132 -0
  58. data/sig/puppeteer/bidi/core/request.rbs +120 -0
  59. data/sig/puppeteer/bidi/core/session.rbs +65 -0
  60. data/sig/puppeteer/bidi/core/user_context.rbs +72 -0
  61. data/sig/puppeteer/bidi/core/user_prompt.rbs +52 -0
  62. data/sig/puppeteer/bidi/core.rbs +9 -0
  63. data/sig/puppeteer/bidi/deserializer.rbs +28 -0
  64. data/sig/puppeteer/bidi/element_handle.rbs +52 -52
  65. data/sig/puppeteer/bidi/errors.rbs +34 -0
  66. data/sig/puppeteer/bidi/file_chooser.rbs +27 -0
  67. data/sig/puppeteer/bidi/frame.rbs +70 -70
  68. data/sig/puppeteer/bidi/http_response.rbs +18 -0
  69. data/sig/puppeteer/bidi/injected_source.rbs +21 -0
  70. data/sig/puppeteer/bidi/js_handle.rbs +23 -23
  71. data/sig/puppeteer/bidi/keyboard.rbs +57 -0
  72. data/sig/puppeteer/bidi/lazy_arg.rbs +15 -0
  73. data/sig/puppeteer/bidi/mouse.rbs +73 -0
  74. data/sig/puppeteer/bidi/page.rbs +87 -87
  75. data/sig/puppeteer/bidi/query_handler.rbs +113 -0
  76. data/sig/puppeteer/bidi/realm.rbs +141 -0
  77. data/sig/puppeteer/bidi/serializer.rbs +31 -0
  78. data/sig/puppeteer/bidi/target.rbs +68 -0
  79. data/sig/puppeteer/bidi/task_manager.rbs +36 -0
  80. data/sig/puppeteer/bidi/timeout_settings.rbs +14 -0
  81. data/sig/puppeteer/bidi/transport.rbs +43 -0
  82. data/sig/puppeteer/bidi/wait_task.rbs +62 -0
  83. data/sig/puppeteer/bidi.rbs +8 -4
  84. metadata +36 -1
@@ -15,8 +15,8 @@ module Puppeteer
15
15
  attr_reader :browser_context #: BrowserContext
16
16
  attr_reader :timeout_settings #: TimeoutSettings
17
17
 
18
- # @rbs browser_context: BrowserContext
19
- # @rbs browsing_context: Core::BrowsingContext
18
+ # @rbs browser_context: BrowserContext -- Parent browser context
19
+ # @rbs browsing_context: Core::BrowsingContext -- Associated browsing context
20
20
  # @rbs return: void
21
21
  def initialize(browser_context, browsing_context)
22
22
  @browser_context = browser_context
@@ -29,41 +29,41 @@ module Puppeteer
29
29
  # Following Puppeteer's trustedEmitter pattern
30
30
 
31
31
  # Register an event listener
32
- # @rbs event: Symbol | String
33
- # @rbs &block: (untyped) -> void
32
+ # @rbs event: Symbol | String -- Event name
33
+ # @rbs &block: (untyped) -> void -- Event handler
34
34
  # @rbs return: void
35
35
  def on(event, &block)
36
36
  @emitter.on(event, &block)
37
37
  end
38
38
 
39
39
  # Register a one-time event listener
40
- # @rbs event: Symbol | String
41
- # @rbs &block: (untyped) -> void
40
+ # @rbs event: Symbol | String -- Event name
41
+ # @rbs &block: (untyped) -> void -- Event handler
42
42
  # @rbs return: void
43
43
  def once(event, &block)
44
44
  @emitter.once(event, &block)
45
45
  end
46
46
 
47
47
  # Remove an event listener
48
- # @rbs event: Symbol | String
49
- # @rbs &block: (untyped) -> void
48
+ # @rbs event: Symbol | String -- Event name
49
+ # @rbs &block: (untyped) -> void -- Event handler to remove
50
50
  # @rbs return: void
51
51
  def off(event, &block)
52
52
  @emitter.off(event, &block)
53
53
  end
54
54
 
55
55
  # Emit an event to all registered listeners
56
- # @rbs event: Symbol | String
57
- # @rbs data: untyped
56
+ # @rbs event: Symbol | String -- Event name
57
+ # @rbs data: untyped -- Event data
58
58
  # @rbs return: void
59
59
  def emit(event, data = nil)
60
60
  @emitter.emit(event, data)
61
61
  end
62
62
 
63
63
  # Navigate to a URL
64
- # @rbs url: String
65
- # @rbs wait_until: String
66
- # @rbs return: HTTPResponse?
64
+ # @rbs url: String -- URL to navigate to
65
+ # @rbs wait_until: String -- When to consider navigation complete ('load', 'domcontentloaded')
66
+ # @rbs return: HTTPResponse? -- Main response
67
67
  def goto(url, wait_until: 'load')
68
68
  assert_not_closed
69
69
 
@@ -71,20 +71,20 @@ module Puppeteer
71
71
  end
72
72
 
73
73
  # Set page content
74
- # @rbs html: String
75
- # @rbs wait_until: String
74
+ # @rbs html: String -- HTML content to set
75
+ # @rbs wait_until: String -- When to consider content set ('load', 'domcontentloaded')
76
76
  # @rbs return: void
77
77
  def set_content(html, wait_until: 'load')
78
78
  main_frame.set_content(html, wait_until: wait_until)
79
79
  end
80
80
 
81
81
  # Take a screenshot
82
- # @rbs path: String?
83
- # @rbs type: String
84
- # @rbs full_page: bool
85
- # @rbs clip: Hash[Symbol, Numeric]?
86
- # @rbs capture_beyond_viewport: bool
87
- # @rbs return: String
82
+ # @rbs path: String? -- File path to save screenshot
83
+ # @rbs type: String -- Image type ('png' or 'jpeg')
84
+ # @rbs full_page: bool -- Capture full page
85
+ # @rbs clip: Hash[Symbol, Numeric]? -- Clip region
86
+ # @rbs capture_beyond_viewport: bool -- Capture beyond viewport
87
+ # @rbs return: String -- Base64-encoded image data
88
88
  def screenshot(path: nil, type: 'png', full_page: false, clip: nil, capture_beyond_viewport: true)
89
89
  assert_not_closed
90
90
 
@@ -207,82 +207,82 @@ module Puppeteer
207
207
  end
208
208
 
209
209
  # Evaluate JavaScript in the page context
210
- # @rbs script: String
211
- # @rbs *args: untyped
212
- # @rbs return: untyped
210
+ # @rbs script: String -- JavaScript code to evaluate
211
+ # @rbs *args: untyped -- Arguments to pass to the script
212
+ # @rbs return: untyped -- Result of evaluation
213
213
  def evaluate(script, *args)
214
214
  main_frame.evaluate(script, *args)
215
215
  end
216
216
 
217
217
  # Evaluate JavaScript and return a handle to the result
218
- # @rbs script: String
219
- # @rbs *args: untyped
220
- # @rbs return: JSHandle
218
+ # @rbs script: String -- JavaScript code to evaluate
219
+ # @rbs *args: untyped -- Arguments to pass to the script
220
+ # @rbs return: JSHandle -- Handle to the result
221
221
  def evaluate_handle(script, *args)
222
222
  main_frame.evaluate_handle(script, *args)
223
223
  end
224
224
 
225
225
  # Query for an element matching the selector
226
- # @rbs selector: String
227
- # @rbs return: ElementHandle?
226
+ # @rbs selector: String -- Selector to query
227
+ # @rbs return: ElementHandle? -- Matching element or nil
228
228
  def query_selector(selector)
229
229
  main_frame.query_selector(selector)
230
230
  end
231
231
 
232
232
  # Query for all elements matching the selector
233
- # @rbs selector: String
234
- # @rbs return: Array[ElementHandle]
233
+ # @rbs selector: String -- Selector to query
234
+ # @rbs return: Array[ElementHandle] -- All matching elements
235
235
  def query_selector_all(selector)
236
236
  main_frame.query_selector_all(selector)
237
237
  end
238
238
 
239
239
  # Evaluate a function on the first element matching the selector
240
- # @rbs selector: String
241
- # @rbs page_function: String
242
- # @rbs *args: untyped
243
- # @rbs return: untyped
240
+ # @rbs selector: String -- Selector to query
241
+ # @rbs page_function: String -- JavaScript function to evaluate
242
+ # @rbs *args: untyped -- Arguments to pass to the function
243
+ # @rbs return: untyped -- Evaluation result
244
244
  def eval_on_selector(selector, page_function, *args)
245
245
  main_frame.eval_on_selector(selector, page_function, *args)
246
246
  end
247
247
 
248
248
  # Evaluate a function on all elements matching the selector
249
- # @rbs selector: String
250
- # @rbs page_function: String
251
- # @rbs *args: untyped
252
- # @rbs return: untyped
249
+ # @rbs selector: String -- Selector to query
250
+ # @rbs page_function: String -- JavaScript function to evaluate
251
+ # @rbs *args: untyped -- Arguments to pass to the function
252
+ # @rbs return: untyped -- Evaluation result
253
253
  def eval_on_selector_all(selector, page_function, *args)
254
254
  main_frame.eval_on_selector_all(selector, page_function, *args)
255
255
  end
256
256
 
257
257
  # Click an element matching the selector
258
- # @rbs selector: String
259
- # @rbs button: String
260
- # @rbs count: Integer
261
- # @rbs delay: Numeric?
262
- # @rbs offset: Hash[Symbol, Numeric]?
258
+ # @rbs selector: String -- Selector to click
259
+ # @rbs button: String -- Mouse button ('left', 'right', 'middle')
260
+ # @rbs count: Integer -- Number of clicks
261
+ # @rbs delay: Numeric? -- Delay between clicks in ms
262
+ # @rbs offset: Hash[Symbol, Numeric]? -- Click offset from element center
263
263
  # @rbs return: void
264
264
  def click(selector, button: Mouse::LEFT, count: 1, delay: nil, offset: nil)
265
265
  main_frame.click(selector, button: button, count: count, delay: delay, offset: offset)
266
266
  end
267
267
 
268
268
  # Type text into an element matching the selector
269
- # @rbs selector: String
270
- # @rbs text: String
271
- # @rbs delay: Numeric
269
+ # @rbs selector: String -- Selector to type into
270
+ # @rbs text: String -- Text to type
271
+ # @rbs delay: Numeric -- Delay between key presses in ms
272
272
  # @rbs return: void
273
273
  def type(selector, text, delay: 0)
274
274
  main_frame.type(selector, text, delay: delay)
275
275
  end
276
276
 
277
277
  # Hover over an element matching the selector
278
- # @rbs selector: String
278
+ # @rbs selector: String -- Selector to hover
279
279
  # @rbs return: void
280
280
  def hover(selector)
281
281
  main_frame.hover(selector)
282
282
  end
283
283
 
284
284
  # Focus an element matching the selector
285
- # @rbs selector: String
285
+ # @rbs selector: String -- Selector to focus
286
286
  # @rbs return: void
287
287
  def focus(selector)
288
288
  handle = main_frame.query_selector(selector)
@@ -296,13 +296,13 @@ module Puppeteer
296
296
  end
297
297
 
298
298
  # Get the page title
299
- # @rbs return: String
299
+ # @rbs return: String -- Page title
300
300
  def title
301
301
  evaluate('document.title')
302
302
  end
303
303
 
304
304
  # Get the page URL
305
- # @rbs return: String
305
+ # @rbs return: String -- Page URL
306
306
  def url
307
307
  @browsing_context.url
308
308
  end
@@ -316,19 +316,19 @@ module Puppeteer
316
316
  end
317
317
 
318
318
  # Check if page is closed
319
- # @rbs return: bool
319
+ # @rbs return: bool -- Whether page is closed
320
320
  def closed?
321
321
  @browsing_context.closed?
322
322
  end
323
323
 
324
324
  # Get the main frame
325
- # @rbs return: Frame
325
+ # @rbs return: Frame -- Main frame
326
326
  def main_frame
327
327
  @main_frame ||= Frame.from(self, @browsing_context)
328
328
  end
329
329
 
330
330
  # Get the focused frame
331
- # @rbs return: Frame
331
+ # @rbs return: Frame -- Focused frame
332
332
  def focused_frame
333
333
  assert_not_closed
334
334
 
@@ -368,46 +368,46 @@ module Puppeteer
368
368
 
369
369
  # Get all frames (main frame + all nested child frames)
370
370
  # Following Puppeteer's pattern of returning all frames recursively
371
- # @rbs return: Array[Frame]
371
+ # @rbs return: Array[Frame] -- All frames
372
372
  def frames
373
373
  collect_frames(main_frame)
374
374
  end
375
375
 
376
376
  # Get the mouse instance
377
- # @rbs return: Mouse
377
+ # @rbs return: Mouse -- Mouse instance
378
378
  def mouse
379
379
  @mouse ||= Mouse.new(@browsing_context)
380
380
  end
381
381
 
382
382
  # Get the keyboard instance
383
- # @rbs return: Keyboard
383
+ # @rbs return: Keyboard -- Keyboard instance
384
384
  def keyboard
385
385
  @keyboard ||= Keyboard.new(self, @browsing_context)
386
386
  end
387
387
 
388
388
  # Wait for a function to return a truthy value
389
- # @rbs page_function: String
390
- # @rbs options: Hash[Symbol, untyped]
391
- # @rbs *args: untyped
392
- # @rbs &block: ((JSHandle) -> void)?
393
- # @rbs return: JSHandle
389
+ # @rbs page_function: String -- JavaScript function to evaluate
390
+ # @rbs options: Hash[Symbol, untyped] -- Wait options (timeout, polling)
391
+ # @rbs *args: untyped -- Arguments to pass to the function
392
+ # @rbs &block: ((JSHandle) -> void)? -- Optional block called with result
393
+ # @rbs return: JSHandle -- Handle to the truthy result
394
394
  def wait_for_function(page_function, options = {}, *args, &block)
395
395
  main_frame.wait_for_function(page_function, options, *args, &block)
396
396
  end
397
397
 
398
398
  # Wait for an element matching the selector to appear in the page
399
- # @rbs selector: String
400
- # @rbs visible: bool?
401
- # @rbs hidden: bool?
402
- # @rbs timeout: Numeric?
403
- # @rbs &block: ((ElementHandle?) -> void)?
404
- # @rbs return: ElementHandle?
399
+ # @rbs selector: String -- Selector to wait for
400
+ # @rbs visible: bool? -- Wait for element to be visible
401
+ # @rbs hidden: bool? -- Wait for element to be hidden
402
+ # @rbs timeout: Numeric? -- Wait timeout in ms
403
+ # @rbs &block: ((ElementHandle?) -> void)? -- Optional block called with element
404
+ # @rbs return: ElementHandle? -- Element or nil if hidden
405
405
  def wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil, &block)
406
406
  main_frame.wait_for_selector(selector, visible: visible, hidden: hidden, timeout: timeout, &block)
407
407
  end
408
408
 
409
409
  # Set the default timeout for waiting operations (e.g., waitForFunction).
410
- # @rbs timeout: Numeric
410
+ # @rbs timeout: Numeric -- Timeout in ms
411
411
  # @rbs return: void
412
412
  def set_default_timeout(timeout)
413
413
  raise ArgumentError, 'timeout must be a non-negative number' unless timeout.is_a?(Numeric) && timeout >= 0
@@ -416,24 +416,24 @@ module Puppeteer
416
416
  end
417
417
 
418
418
  # Get the current default timeout in milliseconds.
419
- # @rbs return: Numeric
419
+ # @rbs return: Numeric -- Timeout in ms
420
420
  def default_timeout
421
421
  @timeout_settings.timeout
422
422
  end
423
423
 
424
424
  # Wait for navigation to complete
425
- # @rbs timeout: Numeric
426
- # @rbs wait_until: String
427
- # @rbs &block: (-> void)?
428
- # @rbs return: HTTPResponse?
425
+ # @rbs timeout: Numeric -- Navigation timeout in ms
426
+ # @rbs wait_until: String -- When to consider navigation complete
427
+ # @rbs &block: (-> void)? -- Optional block to trigger navigation
428
+ # @rbs return: HTTPResponse? -- Response or nil
429
429
  def wait_for_navigation(timeout: 30000, wait_until: 'load', &block)
430
430
  main_frame.wait_for_navigation(timeout: timeout, wait_until: wait_until, &block)
431
431
  end
432
432
 
433
433
  # Wait for a file chooser to be opened
434
- # @rbs timeout: Numeric?
435
- # @rbs &block: (-> void)?
436
- # @rbs return: FileChooser
434
+ # @rbs timeout: Numeric? -- Wait timeout in ms
435
+ # @rbs &block: (-> void)? -- Optional block to trigger file chooser
436
+ # @rbs return: FileChooser -- File chooser instance
437
437
  def wait_for_file_chooser(timeout: nil, &block)
438
438
  assert_not_closed
439
439
 
@@ -485,9 +485,9 @@ module Puppeteer
485
485
 
486
486
  # Wait for network to be idle (no more than concurrency connections for idle_time)
487
487
  # Based on Puppeteer's waitForNetworkIdle implementation
488
- # @rbs idle_time: Numeric
489
- # @rbs timeout: Numeric
490
- # @rbs concurrency: Integer
488
+ # @rbs idle_time: Numeric -- Time to wait for idle in ms
489
+ # @rbs timeout: Numeric -- Wait timeout in ms
490
+ # @rbs concurrency: Integer -- Max allowed inflight requests
491
491
  # @rbs return: void
492
492
  def wait_for_network_idle(idle_time: 500, timeout: 30000, concurrency: 0)
493
493
  assert_not_closed
@@ -554,8 +554,8 @@ module Puppeteer
554
554
  end
555
555
 
556
556
  # Set viewport size
557
- # @rbs width: Integer
558
- # @rbs height: Integer
557
+ # @rbs width: Integer -- Viewport width in pixels
558
+ # @rbs height: Integer -- Viewport height in pixels
559
559
  # @rbs return: void
560
560
  def set_viewport(width:, height:)
561
561
  @viewport = { width: width, height: height }
@@ -568,7 +568,7 @@ module Puppeteer
568
568
  end
569
569
 
570
570
  # Get current viewport size
571
- # @rbs return: Hash[Symbol, Integer]?
571
+ # @rbs return: Hash[Symbol, Integer]? -- Viewport dimensions
572
572
  def viewport
573
573
  @viewport
574
574
  end
@@ -577,7 +577,7 @@ module Puppeteer
577
577
  alias default_timeout= set_default_timeout
578
578
 
579
579
  # Set JavaScript enabled state
580
- # @rbs enabled: bool
580
+ # @rbs enabled: bool -- Whether JavaScript is enabled
581
581
  # @rbs return: void
582
582
  def set_javascript_enabled(enabled)
583
583
  assert_not_closed
@@ -585,7 +585,7 @@ module Puppeteer
585
585
  end
586
586
 
587
587
  # Check if JavaScript is enabled
588
- # @rbs return: bool
588
+ # @rbs return: bool -- Whether JavaScript is enabled
589
589
  def javascript_enabled?
590
590
  @browsing_context.javascript_enabled?
591
591
  end
@@ -593,8 +593,8 @@ module Puppeteer
593
593
  private
594
594
 
595
595
  # Recursively collect all frames starting from the given frame
596
- # @rbs frame: Frame
597
- # @rbs return: Array[Frame]
596
+ # @rbs frame: Frame -- Starting frame
597
+ # @rbs return: Array[Frame] -- All frames in subtree
598
598
  def collect_frames(frame)
599
599
  result = [frame]
600
600
  frame.child_frames.each do |child|
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
1
4
  require 'singleton'
2
5
 
3
6
  module Puppeteer
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  module Puppeteer
4
5
  module Bidi
@@ -6,18 +7,24 @@ module Puppeteer
6
7
  # Provides shared lifecycle management for WaitTask instances.
7
8
  # https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/api/Realm.ts
8
9
  class Realm
9
- attr_reader :task_manager
10
+ attr_reader :task_manager #: Puppeteer::Bidi::TaskManager
10
11
 
12
+ # @rbs timeout_settings: TimeoutSettings -- Timeout settings
13
+ # @rbs return: void
11
14
  def initialize(timeout_settings)
12
15
  @timeout_settings = timeout_settings
13
16
  @task_manager = TaskManager.new
14
17
  @disposed = false
15
18
  end
16
19
 
20
+ # Get the environment object (must be implemented by subclass)
21
+ # @rbs return: untyped -- Environment object
17
22
  def environment
18
23
  raise NotImplementedError, 'Subclass must expose its environment object'
19
24
  end
20
25
 
26
+ # Get the page for this realm
27
+ # @rbs return: Page -- Page instance
21
28
  def page
22
29
  env = environment
23
30
  return env.page if env.respond_to?(:page)
@@ -25,10 +32,18 @@ module Puppeteer
25
32
  raise NotImplementedError, 'Environment must expose a page reference'
26
33
  end
27
34
 
35
+ # Get the default timeout
36
+ # @rbs return: Numeric -- Default timeout in ms
28
37
  def default_timeout
29
38
  @timeout_settings.timeout
30
39
  end
31
40
 
41
+ # Wait for a function to return a truthy value
42
+ # @rbs page_function: String -- JavaScript function to evaluate
43
+ # @rbs options: Hash[Symbol, untyped] -- Wait options (timeout, polling, root)
44
+ # @rbs *args: untyped -- Arguments to pass to the function
45
+ # @rbs &block: ((JSHandle) -> void)? -- Optional block called with result
46
+ # @rbs return: JSHandle -- Handle to the truthy result
32
47
  def wait_for_function(page_function, options = {}, *args, &block)
33
48
  ensure_environment_active!
34
49
 
@@ -51,6 +66,8 @@ module Puppeteer
51
66
  result.wait
52
67
  end
53
68
 
69
+ # Dispose this realm
70
+ # @rbs return: void
54
71
  def dispose
55
72
  return if @disposed
56
73
 
@@ -58,14 +75,16 @@ module Puppeteer
58
75
  @task_manager.terminate_all(Error.new('waitForFunction failed: frame got detached.'))
59
76
  end
60
77
 
78
+ # Check if this realm is disposed
79
+ # @rbs return: bool -- Whether realm is disposed
61
80
  def disposed?
62
81
  @disposed
63
82
  end
64
83
 
65
84
  # Adopt a handle from another realm into this realm.
66
85
  # Mirrors Puppeteer's BidiRealm#adoptHandle implementation.
67
- # @param handle [JSHandle] The handle to adopt
68
- # @return [JSHandle] Handle that belongs to this realm
86
+ # @rbs handle: JSHandle -- The handle to adopt
87
+ # @rbs return: JSHandle -- Handle that belongs to this realm
69
88
  def adopt_handle(handle)
70
89
  raise ArgumentError, 'handle must be a JSHandle' unless handle.is_a?(JSHandle)
71
90
 
@@ -74,8 +93,8 @@ module Puppeteer
74
93
 
75
94
  # Transfer a handle into this realm, disposing of the original.
76
95
  # Mirrors Puppeteer's BidiRealm#transferHandle implementation.
77
- # @param handle [JSHandle] Handle that may belong to another realm
78
- # @return [JSHandle] Handle adopted into this realm
96
+ # @rbs handle: JSHandle -- Handle that may belong to another realm
97
+ # @rbs return: JSHandle -- Handle adopted into this realm
79
98
  def transfer_handle(handle)
80
99
  raise ArgumentError, 'handle must be a JSHandle' unless handle.is_a?(JSHandle)
81
100
 
@@ -88,6 +107,8 @@ module Puppeteer
88
107
 
89
108
  private
90
109
 
110
+ # Ensure the environment is still active
111
+ # @rbs return: void
91
112
  def ensure_environment_active!
92
113
  env = environment
93
114
  return unless env.respond_to?(:detached?)
@@ -99,8 +120,11 @@ module Puppeteer
99
120
  # Concrete realm that wraps the default window realm for a Frame.
100
121
  # https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/bidi/Realm.ts
101
122
  class FrameRealm < Realm
102
- attr_reader :core_realm
123
+ attr_reader :core_realm #: Core::WindowRealm
103
124
 
125
+ # @rbs frame: Frame -- Frame instance
126
+ # @rbs core_realm: Core::WindowRealm -- Core window realm
127
+ # @rbs return: void
104
128
  def initialize(frame, core_realm)
105
129
  @frame = frame
106
130
  @core_realm = core_realm
@@ -111,14 +135,22 @@ module Puppeteer
111
135
  setup_core_realm_callbacks
112
136
  end
113
137
 
138
+ # Get the frame environment
139
+ # @rbs return: Frame -- Frame instance
114
140
  def environment
115
141
  @frame
116
142
  end
117
143
 
144
+ # Get the page
145
+ # @rbs return: Page -- Page instance
118
146
  def page
119
147
  @frame.page
120
148
  end
121
149
 
150
+ # Evaluate JavaScript in this realm
151
+ # @rbs script: String -- JavaScript code to evaluate
152
+ # @rbs *args: untyped -- Arguments to pass to the script
153
+ # @rbs return: untyped -- Evaluation result
122
154
  def evaluate(script, *args)
123
155
  ensure_environment_active!
124
156
 
@@ -129,6 +161,10 @@ module Puppeteer
129
161
  Deserializer.deserialize(actual_result)
130
162
  end
131
163
 
164
+ # Evaluate JavaScript and return a handle to the result
165
+ # @rbs script: String -- JavaScript code to evaluate
166
+ # @rbs *args: untyped -- Arguments to pass to the script
167
+ # @rbs return: JSHandle -- Handle to the result
132
168
  def evaluate_handle(script, *args)
133
169
  ensure_environment_active!
134
170
 
@@ -138,6 +174,11 @@ module Puppeteer
138
174
  JSHandle.from(result['result'], @core_realm)
139
175
  end
140
176
 
177
+ # Call a function in the realm
178
+ # @rbs function_declaration: String -- Function source code
179
+ # @rbs await_promise: bool -- Whether to await promise results
180
+ # @rbs **options: untyped -- Additional options
181
+ # @rbs return: Hash[String, untyped] -- Evaluation result
141
182
  def call_function(function_declaration, await_promise, **options)
142
183
  ensure_environment_active!
143
184
 
@@ -147,6 +188,8 @@ module Puppeteer
147
188
  result
148
189
  end
149
190
 
191
+ # Get the Puppeteer utility handle
192
+ # @rbs return: JSHandle -- Puppeteer utility handle
150
193
  def puppeteer_util
151
194
  return @puppeteer_util_handle if @puppeteer_util_handle
152
195
 
@@ -154,10 +197,14 @@ module Puppeteer
154
197
  @puppeteer_util_handle = evaluate_handle(script)
155
198
  end
156
199
 
200
+ # Get or create lazy arg for Puppeteer utility
201
+ # @rbs return: LazyArg -- Lazy argument for Puppeteer utility
157
202
  def puppeteer_util_lazy_arg
158
203
  @puppeteer_util_lazy_arg ||= LazyArg.create { puppeteer_util }
159
204
  end
160
205
 
206
+ # Reset the Puppeteer utility handle
207
+ # @rbs return: void
161
208
  def reset_puppeteer_util_handle!
162
209
  handle = @puppeteer_util_handle
163
210
  @puppeteer_util_handle = nil
@@ -171,12 +218,20 @@ module Puppeteer
171
218
  end
172
219
  end
173
220
 
221
+ # Wait for a function to return a truthy value
222
+ # @rbs page_function: String -- JavaScript function to evaluate
223
+ # @rbs options: Hash[Symbol, untyped] -- Wait options
224
+ # @rbs *args: untyped -- Arguments to pass to the function
225
+ # @rbs &block: ((JSHandle) -> void)? -- Optional block called with result
226
+ # @rbs return: JSHandle -- Handle to the truthy result
174
227
  def wait_for_function(page_function, options = {}, *args, &block)
175
228
  raise FrameDetachedError if @frame.detached?
176
229
 
177
230
  super
178
231
  end
179
232
 
233
+ # Dispose this realm
234
+ # @rbs return: void
180
235
  def dispose
181
236
  reset_puppeteer_util_handle!
182
237
  super
@@ -184,6 +239,10 @@ module Puppeteer
184
239
 
185
240
  private
186
241
 
242
+ # Execute script with core realm
243
+ # @rbs script: String -- JavaScript code
244
+ # @rbs args: Array[untyped] -- Arguments
245
+ # @rbs return: Async::Task[Hash[String, untyped]] -- Evaluation result
187
246
  def execute_with_core(script, args)
188
247
  script_trimmed = script.strip
189
248
 
@@ -203,6 +262,9 @@ module Puppeteer
203
262
  end
204
263
  end
205
264
 
265
+ # Handle evaluation exception from result
266
+ # @rbs result: Hash[String, untyped] -- Evaluation result containing exception
267
+ # @rbs return: void
206
268
  def handle_evaluation_exception(result)
207
269
  exception_details = result['exceptionDetails']
208
270
  return unless exception_details
@@ -220,6 +282,8 @@ module Puppeteer
220
282
  raise error_message
221
283
  end
222
284
 
285
+ # Set up core realm event callbacks
286
+ # @rbs return: void
223
287
  def setup_core_realm_callbacks
224
288
  @core_realm.environment = @frame if @core_realm.respond_to?(:environment=)
225
289
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  require 'json'
4
5
  require 'puppeteer/bidi/lazy_arg'