Class: Puppeteer::Puppeteer

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

Overview

rbs_inline: enabled

Defined Under Namespace

Classes: NoViewport

Instance Method Summary collapse

Constructor Details

#initialize(project_root:, preferred_revision:, is_puppeteer_core:) ⇒ Puppeteer

Returns a new instance of Puppeteer.

Parameters:

  • project_root: (String)
  • preferred_revision: (String)
  • is_puppeteer_core: (Boolean)


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

def initialize(project_root:, preferred_revision:, is_puppeteer_core:)
  @project_root = project_root
  @preferred_revision = preferred_revision
  @is_puppeteer_core = is_puppeteer_core
end

Instance Method Details

#clear_custom_query_handlersvoid

This method returns an undefined value.



300
301
302
# File 'lib/puppeteer/puppeteer.rb', line 300

def clear_custom_query_handlers
  query_handler_manager.clear_custom_query_handlers
end

#connect(browser_ws_endpoint: nil, browser_url: nil, transport: nil, channel: nil, ignore_https_errors: nil, network_enabled: true, issues_enabled: true, block_list: nil, allow_list: nil, default_viewport: NoViewport.new, slow_mo: nil, protocol_timeout: nil, &block) ⇒ void

This method returns an undefined value.

Parameters:

  • browser_ws_endpoint: (String, nil) (defaults to: nil)
  • browser_url: (String, nil) (defaults to: nil)
  • transport: (Puppeteer::WebSocketTransport, nil) (defaults to: nil)
  • channel: (String, Symbol, nil) (defaults to: nil)
  • ignore_https_errors: (Boolean, nil) (defaults to: nil)
  • network_enabled: (Boolean, nil) (defaults to: true)
  • issues_enabled: (Boolean, nil) (defaults to: true)
  • block_list: (Array[String], nil) (defaults to: nil)
  • allow_list: (Array[String], nil) (defaults to: nil)
  • default_viewport: (Puppeteer::Viewport, nil) (defaults to: NoViewport.new)
  • slow_mo: (Integer, nil) (defaults to: nil)
  • protocol_timeout: (Integer, nil) (defaults to: nil)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/puppeteer/puppeteer.rb', line 157

def connect(
  browser_ws_endpoint: nil,
  browser_url: nil,
  transport: nil,
  channel: nil,
  ignore_https_errors: nil,
  network_enabled: true,
  issues_enabled: true,
  block_list: nil,
  allow_list: nil,
  default_viewport: NoViewport.new,
  slow_mo: nil,
  protocol_timeout: nil,
  &block
)
  options = {
    browser_ws_endpoint: browser_ws_endpoint,
    browser_url: browser_url,
    transport: transport,
    channel: channel&.to_s,
    ignore_https_errors: ignore_https_errors,
    network_enabled: network_enabled,
    issues_enabled: issues_enabled,
    block_list: block_list,
    allow_list: allow_list,
    slow_mo: slow_mo,
    protocol_timeout: protocol_timeout,
  }.compact
  unless default_viewport.is_a?(NoViewport)
    options[:default_viewport] = default_viewport
  end
  if async_context?
    browser = Puppeteer::BrowserConnector.new(options).connect_to_browser
    if block
      begin
        block.call(browser)
      ensure
        browser.disconnect
      end
    else
      browser
    end
  else
    runner = Puppeteer::ReactorRunner.new
    begin
      browser = runner.sync { Puppeteer::BrowserConnector.new(options).connect_to_browser }
    rescue StandardError
      runner.close
      raise
    end
    proxy = Puppeteer::ReactorRunner::Proxy.new(runner, browser, owns_runner: true)
    if block
      begin
        block.call(proxy)
      ensure
        proxy.disconnect
      end
    else
      proxy
    end
  end
end

#custom_query_handler_namesArray[String]

Returns:

  • (Array[String])


305
306
307
# File 'lib/puppeteer/puppeteer.rb', line 305

def custom_query_handler_names
  query_handler_manager.custom_query_handler_names
end

#default_args(args: nil, user_data_dir: nil, devtools: nil, headless: nil) ⇒ Array[String]

Parameters:

  • args: (Array[String], nil) (defaults to: nil)
  • user_data_dir: (String, nil) (defaults to: nil)
  • devtools: (Boolean, nil) (defaults to: nil)
  • headless: (Boolean, nil) (defaults to: nil)

Returns:

  • (Array[String])


328
329
330
331
332
333
334
335
336
# File 'lib/puppeteer/puppeteer.rb', line 328

def default_args(args: nil, user_data_dir: nil, devtools: nil, headless: nil)
  options = {
    args: args,
    user_data_dir: user_data_dir,
    devtools: devtools,
    headless: headless,
  }.compact
  launcher.default_args(options)
end

#devicesPuppeteer::Devices

Returns:



314
315
316
# File 'lib/puppeteer/puppeteer.rb', line 314

def devices
  Puppeteer::Devices
end

#executable_path(channel: nil) ⇒ String

Parameters:

  • channel: (String, nil) (defaults to: nil)

Returns:

  • (String)


222
223
224
# File 'lib/puppeteer/puppeteer.rb', line 222

def executable_path(channel: nil)
  launcher.executable_path(channel: channel)
end

#launch(product: nil, channel: nil, executable_path: nil, ignore_default_args: nil, handle_SIGINT: nil, handle_SIGTERM: nil, handle_SIGHUP: nil, timeout: nil, dumpio: nil, env: nil, pipe: nil, args: nil, user_data_dir: nil, devtools: nil, debugging_port: nil, headless: nil, ignore_https_errors: nil, network_enabled: true, issues_enabled: true, block_list: nil, allow_list: nil, enable_extensions: false, default_viewport: NoViewport.new, slow_mo: nil, protocol_timeout: nil, wait_for_initial_page: nil, &block) ⇒ void

This method returns an undefined value.

Parameters:

  • product: (String, nil) (defaults to: nil)
  • channel: (String, Symbol, nil) (defaults to: nil)
  • executable_path: (String, nil) (defaults to: nil)
  • ignore_default_args: (Array[String], nil) (defaults to: nil)
  • handle_SIGINT: (Boolean, nil) (defaults to: nil)
  • handle_SIGTERM: (Boolean, nil) (defaults to: nil)
  • handle_SIGHUP: (Boolean, nil) (defaults to: nil)
  • timeout: (Integer, nil) (defaults to: nil)
  • dumpio: (Boolean, nil) (defaults to: nil)
  • env: (Hash[String, String], nil) (defaults to: nil)
  • pipe: (Boolean, nil) (defaults to: nil)
  • args: (Array[String], nil) (defaults to: nil)
  • user_data_dir: (String, nil) (defaults to: nil)
  • devtools: (Boolean, nil) (defaults to: nil)
  • debugging_port: (Integer, nil) (defaults to: nil)
  • headless: (Boolean, nil) (defaults to: nil)
  • ignore_https_errors: (Boolean, nil) (defaults to: nil)
  • network_enabled: (Boolean, nil) (defaults to: true)
  • issues_enabled: (Boolean, nil) (defaults to: true)
  • block_list: (Array[String], nil) (defaults to: nil)
  • allow_list: (Array[String], nil) (defaults to: nil)
  • enable_extensions: (bool, Array[String], nil) (defaults to: false)
  • default_viewport: (Puppeteer::Viewport, nil) (defaults to: NoViewport.new)
  • slow_mo: (Integer, nil) (defaults to: nil)
  • protocol_timeout: (Integer, nil) (defaults to: nil)
  • wait_for_initial_page: (Boolean, nil) (defaults to: nil)


44
45
46
47
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
# File 'lib/puppeteer/puppeteer.rb', line 44

def launch(
  product: nil,
  channel: nil,
  executable_path: nil,
  ignore_default_args: nil,
  handle_SIGINT: nil,
  handle_SIGTERM: nil,
  handle_SIGHUP: nil,
  timeout: nil,
  dumpio: nil,
  env: nil,
  pipe: nil,
  args: nil,
  user_data_dir: nil,
  devtools: nil,
  debugging_port: nil,
  headless: nil,
  ignore_https_errors: nil,
  network_enabled: true,
  issues_enabled: true,
  block_list: nil,
  allow_list: nil,
  enable_extensions: false,
  default_viewport: NoViewport.new,
  slow_mo: nil,
  protocol_timeout: nil,
  wait_for_initial_page: nil,
  &block
)
  product = product.to_s if product
  if product && product != 'chrome'
    raise ArgumentError.new("Unsupported product: #{product}. Only 'chrome' is supported.")
  end

  options = {
    channel: channel&.to_s,
    executable_path: executable_path,
    ignore_default_args: ignore_default_args,
    handle_SIGINT: handle_SIGINT,
    handle_SIGTERM: handle_SIGTERM,
    handle_SIGHUP: handle_SIGHUP,
    timeout: timeout,
    dumpio: dumpio,
    env: env,
    pipe: pipe,
    args: args,
    user_data_dir: user_data_dir,
    devtools: devtools,
    debugging_port: debugging_port,
    headless: headless,
    ignore_https_errors: ignore_https_errors,
    network_enabled: network_enabled,
    issues_enabled: issues_enabled,
    block_list: block_list,
    allow_list: allow_list,
    enable_extensions: enable_extensions,
    default_viewport: default_viewport,
    slow_mo: slow_mo,
    protocol_timeout: protocol_timeout,
    wait_for_initial_page: wait_for_initial_page,
  }
  if default_viewport.is_a?(NoViewport)
    options.delete(:default_viewport)
  end
  options.delete(:wait_for_initial_page) if wait_for_initial_page.nil?

  @product_name = product
  if async_context?
    browser = launcher.launch(options)
    if block
      begin
        block.call(browser)
      ensure
        browser.close
      end
    else
      browser
    end
  else
    runner = Puppeteer::ReactorRunner.new
    begin
      browser = runner.sync { launcher.launch(options) }
    rescue StandardError
      runner.close
      raise
    end
    proxy = Puppeteer::ReactorRunner::Proxy.new(runner, browser, owns_runner: true)
    if block
      begin
        block.call(proxy)
      ensure
        proxy.close
      end
    else
      proxy
    end
  end
end

#network_conditionsPuppeteer::NetworkConditions



319
320
321
# File 'lib/puppeteer/puppeteer.rb', line 319

def network_conditions
  Puppeteer::NetworkConditions
end

#register_custom_query_handler(name:, query_one: nil, query_all: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • name: (String)
  • query_one: (String, nil) (defaults to: nil)
  • query_all: (String, nil) (defaults to: nil)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/puppeteer/puppeteer.rb', line 246

def register_custom_query_handler(name:, query_one: nil, query_all: nil)
  unless name =~ /\A[a-zA-Z]+\z/
    raise ArgumentError.new("Custom query handler names may only contain [a-zA-Z]")
  end

  if query_one.nil? && query_all.nil?
    raise ArgumentError.new('At least one query method must be implemented.')
  end

  handler = Puppeteer::CustomQueryHandler.new(query_one: query_one, query_all: query_all)
  handler_name = name.to_sym
  if query_handler_manager.query_handlers.key?(handler_name)
    raise ArgumentError.new("A query handler named #{name} already exists")
  end

  query_handler_manager.query_handlers[handler_name] = handler
end

#unregister_custom_query_handler(name:) ⇒ void

This method returns an undefined value.

Parameters:

  • name: (String)


295
296
297
# File 'lib/puppeteer/puppeteer.rb', line 295

def unregister_custom_query_handler(name:)
  query_handler_manager.unregister_custom_query_handler(name)
end

#with_custom_query_handler(name:, query_one: nil, query_all: nil, &block) ⇒ void

This method returns an undefined value.

Parameters:

  • name: (String)
  • query_one: (String, nil) (defaults to: nil)
  • query_all: (String, nil) (defaults to: nil)


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/puppeteer/puppeteer.rb', line 268

def with_custom_query_handler(name:, query_one: nil, query_all: nil, &block)
  unless name =~ /\A[a-zA-Z]+\z/
    raise ArgumentError.new("Custom query handler names may only contain [a-zA-Z]")
  end

  if query_one.nil? && query_all.nil?
    raise ArgumentError.new('At least one query method must be implemented.')
  end

  handler_name = name.to_sym

  handler = Puppeteer::CustomQueryHandler.new(query_one: query_one, query_all: query_all)
  original = query_handler_manager.query_handlers.delete(handler_name)
  query_handler_manager.query_handlers[handler_name] = handler
  begin
    block.call
  ensure
    if original
      query_handler_manager.query_handlers[handler_name] = original
    else
      query_handler_manager.query_handlers.delete(handler_name)
    end
  end
end