Class: Puppeteer::Bidi::Realm

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/bidi/realm.rb,
sig/puppeteer/bidi/realm.rbs

Overview

Base realm abstraction that mirrors Puppeteer's Realm class hierarchy. Provides shared lifecycle management for WaitTask instances. https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/api/Realm.ts

Direct Known Subclasses

FrameRealm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout_settings) ⇒ Realm

Returns a new instance of Realm.

Parameters:



14
15
16
17
18
# File 'lib/puppeteer/bidi/realm.rb', line 14

def initialize(timeout_settings)
  @timeout_settings = timeout_settings
  @task_manager = TaskManager.new
  @disposed = false
end

Instance Attribute Details

#task_managerPuppeteer::Bidi::TaskManager (readonly)

: Puppeteer::Bidi::TaskManager



10
11
12
# File 'lib/puppeteer/bidi/realm.rb', line 10

def task_manager
  @task_manager
end

Instance Method Details

#adopt_handle(handle) ⇒ JSHandle

Adopt a handle from another realm into this realm. Mirrors Puppeteer's BidiRealm#adoptHandle implementation.

Parameters:

Returns:



88
89
90
91
92
# File 'lib/puppeteer/bidi/realm.rb', line 88

def adopt_handle(handle)
  raise ArgumentError, 'handle must be a JSHandle' unless handle.is_a?(JSHandle)

  evaluate_handle('(node) => node', handle)
end

#default_timeoutNumeric

Get the default timeout

Returns:

  • (Numeric)


37
38
39
# File 'lib/puppeteer/bidi/realm.rb', line 37

def default_timeout
  @timeout_settings.timeout
end

#disposevoid

This method returns an undefined value.

Dispose this realm



71
72
73
74
75
76
# File 'lib/puppeteer/bidi/realm.rb', line 71

def dispose
  return if @disposed

  @disposed = true
  @task_manager.terminate_all(Error.new('waitForFunction failed: frame got detached.'))
end

#disposed?Boolean

Check if this realm is disposed

Returns:

  • (Boolean)


80
81
82
# File 'lib/puppeteer/bidi/realm.rb', line 80

def disposed?
  @disposed
end

#ensure_environment_active!void

This method returns an undefined value.

Ensure the environment is still active



112
113
114
115
116
117
# File 'lib/puppeteer/bidi/realm.rb', line 112

def ensure_environment_active!
  env = environment
  return unless env.respond_to?(:detached?)

  raise FrameDetachedError if env.detached?
end

#environmentObject

Get the environment object (must be implemented by subclass)

Returns:

  • (Object)


22
23
24
# File 'lib/puppeteer/bidi/realm.rb', line 22

def environment
  raise NotImplementedError, 'Subclass must expose its environment object'
end

#pagePage

Get the page for this realm

Returns:



28
29
30
31
32
33
# File 'lib/puppeteer/bidi/realm.rb', line 28

def page
  env = environment
  return env.page if env.respond_to?(:page)

  raise NotImplementedError, 'Environment must expose a page reference'
end

#transfer_handle(handle) ⇒ JSHandle

Transfer a handle into this realm, disposing of the original. Mirrors Puppeteer's BidiRealm#transferHandle implementation.

Parameters:

Returns:



98
99
100
101
102
103
104
105
106
# File 'lib/puppeteer/bidi/realm.rb', line 98

def transfer_handle(handle)
  raise ArgumentError, 'handle must be a JSHandle' unless handle.is_a?(JSHandle)

  return handle if handle.realm.equal?(self)

  adopted = adopt_handle(handle)
  handle.dispose
  adopted
end

#wait_for_function(page_function, options = {}, *args, &block) ⇒ void

This method returns an undefined value.

Wait for a function to return a truthy value

Parameters:

  • page_function (String)
  • options (Hash[Symbol, untyped]) (defaults to: {})
  • args (Object)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/puppeteer/bidi/realm.rb', line 47

def wait_for_function(page_function, options = {}, *args, &block)
  ensure_environment_active!

  polling = options[:polling] || 'raf'
  if polling.is_a?(Numeric) && polling < 0
    raise ArgumentError, "Cannot poll with non-positive interval: #{polling}"
  end

  timeout = options.key?(:timeout) ? options[:timeout] : default_timeout
  wait_task_options = {
    polling: polling,
    timeout: timeout,
    root: options[:root]
  }

  result = WaitTask.new(self, wait_task_options, page_function, *args).result

  Async(&block).wait if block

  result.wait
end