Class: Puppeteer::Launcher::Chrome

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/launcher/chrome.rb

Defined Under Namespace

Classes: DefaultArgs

Constant Summary collapse

CHROMIUM_CHANNELS =
{
  windows: {
    'chrome' => "#{ENV['PROGRAMFILES']}\\Google\\Chrome\\Application\\chrome.exe",
    'chrome-beta' => "#{ENV['PROGRAMFILES']}\\Google\\Chrome Beta\\Application\\chrome.exe",
    'chrome-canary' => "#{ENV['PROGRAMFILES']}\\Google\\Chrome SxS\\Application\\chrome.exe",
    'chrome-dev' => "#{ENV['PROGRAMFILES']}\\Google\\Chrome Dev\\Application\\chrome.exe",
    'msedge' => "#{ENV['PROGRAMFILES(X86)']}\\Microsoft\\Edge\\Application\\msedge.exe",
  },
  darwin: {
    'chrome' => '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    'chrome-beta' => '/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta',
    'chrome-canary' => '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
    'chrome-dev' => '/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome Dev',
    'msedge' => '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
  },
  linux: {
    'chrome' => '/opt/google/chrome/chrome',
    'chrome-beta' => '/opt/google/chrome-beta/chrome',
    'chrome-dev' => '/opt/google/chrome-unstable/chrome',
  },
}.freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Chrome.



6
7
8
9
10
# File 'lib/puppeteer/launcher/chrome.rb', line 6

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

#connect(options = {}) ⇒ Puppeteer::Browser

Returns:



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
# File 'lib/puppeteer/launcher/chrome.rb', line 162

def connect(options = {})
  @browser_options = BrowserOptions.new(options)
  browser_ws_endpoint = options[:browser_ws_endpoint]
  browser_url = options[:browser_url]
  transport = options[:transport]

  connection =
    if browser_ws_endpoint && browser_url.nil? && transport.nil?
      connect_with_browser_ws_endpoint(browser_ws_endpoint)
    elsif browser_ws_endpoint.nil? && browser_url && transport.nil?
      connect_with_browser_url(browser_url)
    elsif browser_ws_endpoint.nil? && browser_url.nil? && transport
      connect_with_transport(transport)
    else
      raise ArgumentError.new("Exactly one of browserWSEndpoint, browserURL or transport must be passed to puppeteer.connect")
    end

  result = connection.send_message('Target.getBrowserContexts')
  browser_context_ids = result['browserContextIds']

  Puppeteer::Browser.create(
    connection: connection,
    context_ids: browser_context_ids,
    ignore_https_errors: @browser_options.ignore_https_errors?,
    default_viewport: @browser_options.default_viewport,
    process: nil,
    close_callback: -> { connection.send_message('Browser.close') },
  )
end

#default_args(options = nil) ⇒ DefaultArgs

Returns:



157
158
159
# File 'lib/puppeteer/launcher/chrome.rb', line 157

def default_args(options = nil)
  DefaultArgs.new(ChromeArgOptions.new(options || {}))
end

#executable_path(channel: nil) ⇒ string

Returns:

  • (string)


215
216
217
218
219
220
221
# File 'lib/puppeteer/launcher/chrome.rb', line 215

def executable_path(channel: nil)
  if channel
    executable_path_for_channel(channel.to_s)
  else
    executable_path_for_channel('chrome')
  end
end

#launch(options = {}) ⇒ !Promise<!Browser>

Parameters:

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/puppeteer/launcher/chrome.rb', line 14

def launch(options = {})
  @chrome_arg_options = ChromeArgOptions.new(options)
  @launch_options = LaunchOptions.new(options)
  @browser_options = BrowserOptions.new(options)

  chrome_arguments =
    if !@launch_options.ignore_default_args
      default_args(options).to_a
    elsif @launch_options.ignore_default_args.is_a?(Enumerable)
      default_args(options).reject do |arg|
        @launch_options.ignore_default_args.include?(arg)
      end.to_a
    else
      @chrome_arg_options.args.dup
    end

  #
  # let temporaryUserDataDir = null;

  if chrome_arguments.none? { |arg| arg.start_with?('--remote-debugging-') }
    if @launch_options.pipe?
      chrome_arguments << '--remote-debugging-pipe'
    else
      chrome_arguments << '--remote-debugging-port=0'
    end
  end

  temporary_user_data_dir = nil
  if chrome_arguments.none? { |arg| arg.start_with?('--user-data-dir') }
    temporary_user_data_dir = Dir.mktmpdir('puppeteer_dev_chrome_profile-')
    chrome_arguments << "--user-data-dir=#{temporary_user_data_dir}"
  end

  chrome_executable =
    if @launch_options.channel
      executable_path_for_channel(@launch_options.channel.to_s)
    else
      @launch_options.executable_path || executable_path_for_channel('chrome')
    end
  use_pipe = chrome_arguments.include?('--remote-debugging-pipe')
  runner = Puppeteer::BrowserRunner.new(chrome_executable, chrome_arguments, temporary_user_data_dir)
  runner.start(
    handle_SIGHUP: @launch_options.handle_SIGHUP?,
    handle_SIGTERM: @launch_options.handle_SIGTERM?,
    handle_SIGINT: @launch_options.handle_SIGINT?,
    dumpio: @launch_options.dumpio?,
    env: @launch_options.env,
    pipe: use_pipe,
  )

  begin
    connection = runner.setup_connection(
      use_pipe: use_pipe,
      timeout: @launch_options.timeout,
      slow_mo: @browser_options.slow_mo,
      preferred_revision: @preferred_revision,
    )

    browser = Puppeteer::Browser.create(
      connection: connection,
      context_ids: [],
      ignore_https_errors: @browser_options.ignore_https_errors?,
      default_viewport: @browser_options.default_viewport,
      process: runner.proc,
      close_callback: -> { runner.close },
    )

    browser.wait_for_target(predicate: ->(target) { target.type == 'page' })

    browser
  rescue
    runner.kill
    raise
  end
end

#productObject



268
269
270
# File 'lib/puppeteer/launcher/chrome.rb', line 268

def product
  'chrome'
end