Class: Puppeteer::BrowserContext

Inherits:
Object
  • Object
show all
Includes:
EventCallbackable
Defined in:
lib/puppeteer/browser_context.rb

Constant Summary collapse

WEB_PERMISSION_TO_PROTOCOL =
{
  'geolocation' => 'geolocation',
  'midi' => 'midi',
  'notifications' => 'notifications',
  # TODO: push isn't a valid type?
  # 'push' => 'push',
  'camera' => 'videoCapture',
  'microphone' => 'audioCapture',
  'background-sync' => 'backgroundSync',
  'ambient-light-sensor' => 'sensors',
  'accelerometer' => 'sensors',
  'gyroscope' => 'sensors',
  'magnetometer' => 'sensors',
  'accessibility-events' => 'accessibilityEvents',
  'clipboard-read' => 'clipboardReadWrite',
  'clipboard-write' => 'clipboardReadWrite',
  'payment-handler' => 'paymentHandler',
  'idle-detection' => 'idleDetection',
  # chrome-specific permissions we have.
  'midi-sysex' => 'midiSysex',
}.freeze

Instance Method Summary collapse

Methods included from EventCallbackable

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

Constructor Details

#initialize(connection, browser, context_id) ⇒ BrowserContext

Returns a new instance of BrowserContext.

Parameters:



8
9
10
11
12
# File 'lib/puppeteer/browser_context.rb', line 8

def initialize(connection, browser, context_id)
  @connection = connection
  @browser = browser
  @id = context_id
end

Instance Method Details

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

Parameters:



49
# File 'lib/puppeteer/browser_context.rb', line 49

define_async_method :async_wait_for_target

#browserBrowser

Returns:



109
110
111
# File 'lib/puppeteer/browser_context.rb', line 109

def browser
  @browser
end

#clear_permission_overridesObject



95
96
97
98
99
100
101
# File 'lib/puppeteer/browser_context.rb', line 95

def clear_permission_overrides
  if @id
    @connection.send_message('Browser.resetPermissions', browserContextId: @id)
  else
    @connection.send_message('Browser.resetPermissions')
  end
end

#closeObject



113
114
115
116
117
118
# File 'lib/puppeteer/browser_context.rb', line 113

def close
  unless @id
    raise 'Non-incognito profiles cannot be closed!'
  end
  @browser.dispose_context(@id)
end

#incognito?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/puppeteer/browser_context.rb', line 56

def incognito?
  !!@id
end

#new_pageFuture<Puppeteer::Page>

Returns:



104
105
106
# File 'lib/puppeteer/browser_context.rb', line 104

def new_page
  @browser.create_page_in_context(@id)
end

#on(event_name, &block) ⇒ Object

Parameters:

  • event_name (Symbol)

    either of :disconnected, :targetcreated, :targetchanged, :targetdestroyed



15
16
17
18
19
20
21
# File 'lib/puppeteer/browser_context.rb', line 15

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

  super(event_name.to_s, &block)
end

#once(event_name, &block) ⇒ Object

Parameters:

  • event_name (Symbol)


24
25
26
27
28
29
30
# File 'lib/puppeteer/browser_context.rb', line 24

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

  super(event_name.to_s, &block)
end

#override_permissions(origin, permissions) ⇒ Object

Parameters:

  • origin (String)
  • permissions (Array<String>)


84
85
86
87
88
89
90
91
92
93
# File 'lib/puppeteer/browser_context.rb', line 84

def override_permissions(origin, permissions)
  protocol_permissions = permissions.map do |permission|
    WEB_PERMISSION_TO_PROTOCOL[permission] or raise ArgumentError.new("Unknown permission: #{permission}")
  end
  @connection.send_message('Browser.grantPermissions', {
    origin: origin,
    browserContextId: @id,
    permissions: protocol_permissions,
  }.compact)
end

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

Returns:



52
53
54
# File 'lib/puppeteer/browser_context.rb', line 52

def pages
  targets.select { |target| target.type == 'page' }.map(&:page).reject { |page| !page }
end

#targets!Array<!Target>

Returns target.

Returns:

  • (!Array<!Target>)

    target



33
34
35
# File 'lib/puppeteer/browser_context.rb', line 33

def targets
  @browser.targets.select { |target| target.browser_context == self }
end

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

Parameters:

Returns:



39
40
41
42
43
44
# File 'lib/puppeteer/browser_context.rb', line 39

def wait_for_target(predicate:, timeout: nil)
  @browser.wait_for_target(
    predicate: ->(target) { target.browser_context == self && predicate.call(target) },
    timeout: timeout,
  )
end