Class: Puppeteer::Bidi::Core::Session

Inherits:
EventEmitter show all
Includes:
Disposable::DisposableMixin
Defined in:
lib/puppeteer/bidi/core/session.rb,
sig/puppeteer/bidi/core/session.rbs

Overview

Session represents a BiDi session with the browser It wraps a Connection and provides session-specific functionality

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Disposable::DisposableMixin

#dispose

Methods inherited from EventEmitter

#dispose, #emit, #off, #on, #once, #remove_all_listeners

Constructor Details

#initialize(connection, info) ⇒ Session

Returns a new instance of Session.

Parameters:

  • connection (Object)
  • info (Object)


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppeteer/bidi/core/session.rb', line 28

def initialize(connection, info)
  super()
  @connection = connection
  @info = info
  @id = info['sessionId']
  @capabilities = info['capabilities']
  @reason = nil
  @disposables = Disposable::DisposableStack.new

  # Forward BiDi events from connection to session
  setup_event_forwarding
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.

Returns:

  • (Object)


26
27
28
# File 'lib/puppeteer/bidi/core/session.rb', line 26

def browser
  @browser
end

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.

Returns:

  • (Object)


25
26
27
# File 'lib/puppeteer/bidi/core/session.rb', line 25

def capabilities
  @capabilities
end

#connectionObject (readonly)

Returns the value of attribute connection.

Returns:

  • (Object)


25
26
27
# File 'lib/puppeteer/bidi/core/session.rb', line 25

def connection
  @connection
end

#idObject (readonly)

Returns the value of attribute id.

Returns:

  • (Object)


25
26
27
# File 'lib/puppeteer/bidi/core/session.rb', line 25

def id
  @id
end

Class Method Details

.from(connection:, capabilities:) ⇒ Async::Task[Session]

Create a new session from an existing connection

Parameters:

Returns:



16
17
18
19
20
21
22
23
# File 'lib/puppeteer/bidi/core/session.rb', line 16

def self.from(connection:, capabilities:)
  Async do
    result = connection.async_send_command('session.new', { capabilities: capabilities }).wait
    session = new(connection, result)
    session.send(:initialize_session).wait
    session
  end
end

Instance Method Details

#add_intercepts(events, contexts = nil) ⇒ Async::Task[untyped]

Add intercepts (same as subscribe but for interception events)

Parameters:

  • events (Array[String])
  • contexts (Array[String], nil) (defaults to: nil)

Returns:



72
73
74
# File 'lib/puppeteer/bidi/core/session.rb', line 72

def add_intercepts(events, contexts = nil)
  subscribe(events, contexts)
end

#async_send_command(method, params = {}) ⇒ Async::Task[Hash[String, untyped]]

Send a BiDi command through this session

Parameters:

  • method (String)
  • params (Hash[String | Symbol, untyped]) (defaults to: {})

Returns:



52
53
54
55
# File 'lib/puppeteer/bidi/core/session.rb', line 52

def async_send_command(method, params = {})
  raise SessionEndedError, @reason if ended?
  @connection.async_send_command(method, params)
end

#dispose_session(reason) ⇒ Object

Parameters:

  • reason (Object)

Returns:

  • (Object)


112
113
114
115
# File 'lib/puppeteer/bidi/core/session.rb', line 112

def dispose_session(reason)
  @reason = reason
  dispose
end

#end_sessionObject

End the session

Returns:

  • (Object)


77
78
79
80
81
82
83
84
85
# File 'lib/puppeteer/bidi/core/session.rb', line 77

def end_session
  return if ended?

  begin
    async_send_command('session.end', {}).wait
  ensure
    dispose_session('Session already ended.')
  end
end

#ended?Object Also known as: disposed?

Check if the session has ended

Returns:

  • (Object)


42
43
44
# File 'lib/puppeteer/bidi/core/session.rb', line 42

def ended?
  !@reason.nil?
end

#initialize_sessionObject

Returns:

  • (Object)


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppeteer/bidi/core/session.rb', line 98

def initialize_session
  # Subscribe to BiDi modules
  # Based on Puppeteer's subscribeModules: browsingContext, network, log, script, input
  subscribe_modules = %w[
    browsingContext
    network
    log
    script
    input
  ]

  subscribe(subscribe_modules)
end

#perform_disposeObject

Returns:

  • (Object)


89
90
91
92
93
94
# File 'lib/puppeteer/bidi/core/session.rb', line 89

def perform_dispose
  @reason ||= 'Session already destroyed, probably because the connection broke.'
  emit(:ended, @reason)
  @disposables.dispose
  super
end

#setup_event_forwardingObject

Returns:

  • (Object)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/puppeteer/bidi/core/session.rb', line 117

def setup_event_forwarding
  # Forward all BiDi events from connection to this session
  # The existing Connection class uses #on method for event handling
  # We need to set up listeners for all possible BiDi events

  # For now, we'll use a workaround: store the connection's event listeners
  # and forward to our EventEmitter

  # List of common BiDi events to forward
  bidi_events = [
    'browsingContext.contextCreated',
    'browsingContext.contextDestroyed',
    'browsingContext.navigationStarted',
    'browsingContext.navigationCommitted',
    'browsingContext.fragmentNavigated',
    'browsingContext.domContentLoaded',
    'browsingContext.load',
    'browsingContext.historyUpdated',
    'browsingContext.userPromptOpened',
    'browsingContext.userPromptClosed',
    'network.beforeRequestSent',
    'network.responseStarted',
    'network.responseCompleted',
    'network.fetchError',
    'network.authRequired',
    'script.realmCreated',
    'script.realmDestroyed',
    'script.message',
    'log.entryAdded',
    'input.fileDialogOpened',
  ]

  bidi_events.each do |event_name|
    @connection.on(event_name) do |params|
      emit(event_name.to_sym, params)
    end
  end
end

#subscribe(events, contexts = nil) ⇒ Async::Task[untyped]

Subscribe to BiDi events

Parameters:

  • events (Array[String])
  • contexts (Array[String], nil) (defaults to: nil)

Returns:



61
62
63
64
65
66
# File 'lib/puppeteer/bidi/core/session.rb', line 61

def subscribe(events, contexts = nil)
  raise SessionEndedError, @reason if ended?
  params = { events: events }
  params[:contexts] = contexts if contexts
  async_send_command('session.subscribe', params)
end