Class: Puppeteer::Bidi::Core::Session
- Inherits:
-
EventEmitter
- Object
- EventEmitter
- Puppeteer::Bidi::Core::Session
- 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
-
#browser ⇒ Object
Returns the value of attribute browser.
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.from(connection:, capabilities:) ⇒ Async::Task[Session]
Create a new session from an existing connection.
Instance Method Summary collapse
-
#add_intercepts(events, contexts = nil) ⇒ Async::Task[untyped]
Add intercepts (same as subscribe but for interception events).
-
#async_send_command(method, params = {}) ⇒ Async::Task[Hash[String, untyped]]
Send a BiDi command through this session.
- #dispose_session(reason) ⇒ Object
-
#end_session ⇒ Object
End the session.
-
#ended? ⇒ Object
(also: #disposed?)
Check if the session has ended.
-
#initialize(connection, info) ⇒ Session
constructor
A new instance of Session.
- #initialize_session ⇒ Object
- #perform_dispose ⇒ Object
- #setup_event_forwarding ⇒ Object
-
#subscribe(events, contexts = nil) ⇒ Async::Task[untyped]
Subscribe to BiDi events.
Methods included from Disposable::DisposableMixin
Methods inherited from EventEmitter
#dispose, #emit, #off, #on, #once, #remove_all_listeners
Constructor Details
#initialize(connection, info) ⇒ Session
Returns a new instance of Session.
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
#browser ⇒ Object
Returns the value of attribute browser.
26 27 28 |
# File 'lib/puppeteer/bidi/core/session.rb', line 26 def browser @browser end |
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
25 26 27 |
# File 'lib/puppeteer/bidi/core/session.rb', line 25 def capabilities @capabilities end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
25 26 27 |
# File 'lib/puppeteer/bidi/core/session.rb', line 25 def connection @connection end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
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
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)
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
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
112 113 114 115 |
# File 'lib/puppeteer/bidi/core/session.rb', line 112 def dispose_session(reason) @reason = reason dispose end |
#end_session ⇒ Object
End the session
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
42 43 44 |
# File 'lib/puppeteer/bidi/core/session.rb', line 42 def ended? !@reason.nil? end |
#initialize_session ⇒ 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_dispose ⇒ 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_forwarding ⇒ 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
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 |