Class: Puppeteer::Browser

Inherits:
Object
  • Object
show all
Includes:
DebugPrint, EventCallbackable, IfPresent
Defined in:
lib/puppeteer/browser.rb

Defined Under Namespace

Classes: CreatePageError, MissingBrowserContextError, MissingTargetError, Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IfPresent

#if_present

Methods included from EventCallbackable

#add_event_listener, #emit_event, #observe_first, #on_event, #remove_event_listener

Methods included from DebugPrint

#debug_print, #debug_puts

Constructor Details

#initialize(product:, connection:, context_ids:, ignore_https_errors:, default_viewport:, process:, close_callback:, target_filter_callback:, is_page_target_callback:) ⇒ Browser



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puppeteer/browser.rb', line 48

def initialize(product:,
               connection:,
               context_ids:,
               ignore_https_errors:,
               default_viewport:,
               process:,
               close_callback:,
               target_filter_callback:,
               is_page_target_callback:)
  @product = product || 'chrome'
  @ignore_https_errors = ignore_https_errors
  @default_viewport = default_viewport
  @process = process
  @connection = connection
  @close_callback = close_callback
  @target_filter_callback = target_filter_callback || method(:default_target_filter_callback)
  @is_page_target_callback = is_page_target_callback || method(:default_is_page_target_callback)
  @default_context = Puppeteer::BrowserContext.new(@connection, self, nil)
  @contexts = {}

  context_ids.each do |context_id|
    @contexts[context_id] = Puppeteer::BrowserContext.new(@connection, self, context_id)
  end

  if @product == 'firefox'
    @target_manager = Puppeteer::FirefoxTargetManager.new(
      connection: connection,
      target_factory: method(:create_target),
      target_filter_callback: @target_filter_callback,
    )
  else
    @target_manager = Puppeteer::ChromeTargetManager.new(
      connection: connection,
      target_factory: method(:create_target),
      target_filter_callback: @target_filter_callback,
    )
  end
end

Instance Attribute Details

#is_page_target_callbackObject (readonly)

Returns the value of attribute is_page_target_callback.



95
96
97
# File 'lib/puppeteer/browser.rb', line 95

def is_page_target_callback
  @is_page_target_callback
end

Class Method Details

.create(product:, connection:, context_ids:, ignore_https_errors:, default_viewport:, process:, close_callback:, target_filter_callback:, is_page_target_callback:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppeteer/browser.rb', line 17

def self.create(product:,
                connection:,
                context_ids:,
                ignore_https_errors:,
                default_viewport:,
                process:,
                close_callback:,
                target_filter_callback:,
                is_page_target_callback:)
  browser = Puppeteer::Browser.new(
    product: product,
    connection: connection,
    context_ids: context_ids,
    ignore_https_errors: ignore_https_errors,
    default_viewport: default_viewport,
    process: process,
    close_callback: close_callback,
    target_filter_callback: target_filter_callback,
    is_page_target_callback: is_page_target_callback,
  )
  browser.send(:attach)
  browser
end

Instance Method Details

#async_wait_for_target(predicate: , timeout: nil) ⇒ Object



320
# File 'lib/puppeteer/browser.rb', line 320

define_async_method :async_wait_for_target

#browser_contextsObject



161
162
163
# File 'lib/puppeteer/browser.rb', line 161

def browser_contexts
  [@default_context].concat(@contexts.values)
end

#closeObject



337
338
339
340
# File 'lib/puppeteer/browser.rb', line 337

def close
  @close_callback.call
  disconnect
end

#connected?Boolean



347
348
349
# File 'lib/puppeteer/browser.rb', line 347

def connected?
  !@connection.closed?
end

#create_incognito_browser_contextPuppeteer::BrowserContext



155
156
157
158
159
# File 'lib/puppeteer/browser.rb', line 155

def create_incognito_browser_context
  result = @connection.send_message('Target.createBrowserContext')
  browser_context_id = result['browserContextId']
  @contexts[browser_context_id] = Puppeteer::BrowserContext.new(@connection, self, browser_context_id)
end

#create_page_in_context(context_id) ⇒ !Promise<!Puppeteer.Page>



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/puppeteer/browser.rb', line 250

def create_page_in_context(context_id)
  create_target_params = {
    url: 'about:blank',
    browserContextId: context_id,
  }.compact
  result = @connection.send_message('Target.createTarget', **create_target_params)
  target_id = result['targetId']
  target = @target_manager.available_targets[target_id]
  unless target
    raise MissingTargetError.new("Missing target for page (id = #{target_id})")
  end
  unless target.initialized_promise.value!
    raise CreatePageError.new("Failed to create target for page (id = #{target_id})")
  end
  page = target.page
  unless page
    raise CreatePageError.new("Failed to create a page for context (id = #{context_id})")
  end
  page
end

#create_target(target_info, session) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/puppeteer/browser.rb', line 181

def create_target(target_info, session)
  browser_context_id = target_info.browser_context_id
  context =
    if browser_context_id && @contexts.has_key?(browser_context_id)
      @contexts[browser_context_id]
    else
      @default_context
    end

  unless context
    raise MissingBrowserContextError.new('Missing browser context')
  end

  Puppeteer::Target.new(
    target_info: target_info,
    session: session,
    browser_context: context,
    target_manager: @target_manager,
    session_factory: -> (auto_attach_emulated) { @connection.create_session(target_info, auto_attach_emulated: auto_attach_emulated) },
    ignore_https_errors: @ignore_https_errors,
    default_viewport: @default_viewport,
    is_page_target_callback: @is_page_target_callback,
  )
end

#default_browser_contextPuppeteer::BrowserContext



166
167
168
# File 'lib/puppeteer/browser.rb', line 166

def default_browser_context
  @default_context
end

#disconnectObject



342
343
344
345
# File 'lib/puppeteer/browser.rb', line 342

def disconnect
  @target_manager.dispose
  @connection.dispose
end

#dispose_context(context_id) ⇒ Object



171
172
173
174
175
# File 'lib/puppeteer/browser.rb', line 171

def dispose_context(context_id)
  return unless context_id
  @connection.send_message('Target.disposeBrowserContext', browserContextId: context_id)
  @contexts.delete(context_id)
end

#new_pageObject



241
242
243
# File 'lib/puppeteer/browser.rb', line 241

def new_page
  @default_context.new_page
end

#on(event_name, &block) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/puppeteer/browser.rb', line 98

def on(event_name, &block)
  unless BrowserEmittedEvents.values.include?(event_name.to_s)
    raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{BrowserEmittedEvents.values.to_a.join(", ")}")
  end

  super(event_name.to_s, &block)
end

#once(event_name, &block) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/puppeteer/browser.rb', line 107

def once(event_name, &block)
  unless BrowserEmittedEvents.values.include?(event_name.to_s)
    raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{BrowserEmittedEvents.values.to_a.join(", ")}")
  end

  super(event_name.to_s, &block)
end

#pages!Promise<!Array<!Puppeteer.Page>>



323
324
325
# File 'lib/puppeteer/browser.rb', line 323

def pages
  browser_contexts.flat_map(&:pages)
end

#processPuppeteer::BrowserRunner::BrowserProcess



146
147
148
# File 'lib/puppeteer/browser.rb', line 146

def process
  @process
end

#targetObject

The target associated with the browser.



279
280
281
# File 'lib/puppeteer/browser.rb', line 279

def target
  targets.find { |target| target.type == 'browser' } or raise 'Browser target is not found'
end

#targetsObject

All active targets inside the Browser. In case of multiple browser contexts, returns an array with all the targets in all browser contexts.



273
274
275
# File 'lib/puppeteer/browser.rb', line 273

def targets
  @target_manager.available_targets.values.select { |target| target.initialized? }
end

#user_agentString



333
334
335
# File 'lib/puppeteer/browser.rb', line 333

def user_agent
  Version.fetch(@connection).user_agent
end

#versionString



328
329
330
# File 'lib/puppeteer/browser.rb', line 328

def version
  Version.fetch(@connection).product
end

#wait_for_target(predicate:, timeout: nil) ⇒ Puppeteer::Target



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/puppeteer/browser.rb', line 290

def wait_for_target(predicate:, timeout: nil)
  timeout_helper = Puppeteer::TimeoutHelper.new('target', timeout_ms: timeout, default_timeout_ms: 30000)
  existing_target = targets.find { |target| predicate.call(target) }
  return existing_target if existing_target

  event_listening_ids = []
  target_promise = resolvable_future
  event_listening_ids << add_event_listener(BrowserEmittedEvents::TargetCreated) do |target|
    if predicate.call(target)
      target_promise.fulfill(target)
    end
  end
  event_listening_ids << add_event_listener(BrowserEmittedEvents::TargetChanged) do |target|
    if predicate.call(target)
      target_promise.fulfill(target)
    end
  end

  begin
    timeout_helper.with_timeout do
      target_promise.value!
    end
  ensure
    remove_event_listener(*event_listening_ids)
  end
end

#ws_endpointString



237
238
239
# File 'lib/puppeteer/browser.rb', line 237

def ws_endpoint
  @connection.url
end