Class: Puppeteer::BrowserRunner

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

Overview

Defined Under Namespace

Classes: BrowserProcess, LaunchError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DebugPrint

#debug_print, #debug_puts

Constructor Details

#initialize(executable_path, process_arguments, user_data_dir, using_temp_user_data_dir) ⇒ BrowserRunner

Returns a new instance of BrowserRunner.

Parameters:

  • executablePath (string)
  • processArguments (!Array<string>)
  • tempDirectory (string=)
  • executable_path (String)
  • process_arguments (Array[String])
  • user_data_dir (String)
  • using_temp_user_data_dir (Boolean)


10
11
12
13
14
15
16
17
18
# File 'lib/puppeteer/browser_runner.rb', line 10

def initialize(executable_path, process_arguments, user_data_dir, using_temp_user_data_dir)
  @executable_path = executable_path
  @process_arguments = process_arguments
  @user_data_dir = user_data_dir
  @using_temp_user_data_dir = using_temp_user_data_dir
  @proc = nil
  @connection = nil
  @closed = true
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



20
21
22
# File 'lib/puppeteer/browser_runner.rb', line 20

def connection
  @connection
end

#procObject (readonly)

Returns the value of attribute proc.



20
21
22
# File 'lib/puppeteer/browser_runner.rb', line 20

def proc
  @proc
end

Instance Method Details

#closePromise

Returns:

  • (Promise)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/puppeteer/browser_runner.rb', line 125

def close
  return if @closed

  if @using_temp_user_data_dir
    kill
  elsif @connection
    begin
      @connection.send_message('Browser.close')
    rescue
      kill
    end
  end

  @process_closing.call
end

#killPromise

Returns:

  • (Promise)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/puppeteer/browser_runner.rb', line 142

def kill
  # If the process failed to launch (for example if the browser executable path
  # is invalid), then the process does not get a pid assigned. A call to
  # `proc.kill` would error, as the `pid` to-be-killed can not be found.
  @proc&.kill

  # Attempt to remove temporary profile directory to avoid littering.
  begin
    if @using_temp_user_data_dir
      FileUtils.rm_rf(@user_data_dir)
    end
  rescue => err
    debug_puts(err)
  end
end

#setup_connection(use_pipe:, timeout:, slow_mo:, preferred_revision:, protocol_timeout: nil) ⇒ !Promise<!Connection>

Parameters:

  • options (!({usePipe?: boolean, timeout: number, slowMo: number, preferredRevision: string, protocolTimeout: number?}))
  • use_pipe: (Boolean)
  • timeout: (Integer, nil)
  • slow_mo: (Integer, nil)
  • preferred_revision: (String, nil)
  • protocol_timeout: (Numeric, nil) (defaults to: nil)

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/puppeteer/browser_runner.rb', line 161

def setup_connection(use_pipe:, timeout:, slow_mo:, preferred_revision:, protocol_timeout: nil)
  if !use_pipe
    browser_ws_endpoint = wait_for_ws_endpoint(@proc, timeout, preferred_revision)
    transport = Puppeteer::WebSocketTransport.create(browser_ws_endpoint)
    @connection = Puppeteer::Connection.new(
      browser_ws_endpoint,
      transport,
      slow_mo,
      protocol_timeout: protocol_timeout,
    )
  else
    raise NotImplementedError.new('PipeTransport is not yet implemented')
  end

  @connection
end

#start(executable_path: nil, ignore_default_args: nil, handle_SIGINT: nil, handle_SIGTERM: nil, handle_SIGHUP: nil, timeout: nil, dumpio: nil, env: nil, pipe: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • options (!(Launcher.LaunchOptions)=)
  • 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)


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
# File 'lib/puppeteer/browser_runner.rb', line 61

def start(
  executable_path: nil,
  ignore_default_args: nil,
  handle_SIGINT: nil,
  handle_SIGTERM: nil,
  handle_SIGHUP: nil,
  timeout: nil,
  dumpio: nil,
  env: nil,
  pipe: nil
)
  @launch_options = Puppeteer::Launcher::LaunchOptions.new({
    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,
  }.compact)
  @proc = BrowserProcess.new(
    @launch_options.env,
    @executable_path,
    @process_arguments,
  )
  # if (dumpio) {
  #   this.proc.stderr.pipe(process.stderr);
  #   this.proc.stdout.pipe(process.stdout);
  # }
  @closed = false
  @process_closing = -> {
    @proc.dispose
    @closed = true
    if @using_temp_user_data_dir
      FileUtils.rm_rf(@user_data_dir)
    end
  }
  at_exit do
    kill
  end

  if @launch_options.handle_SIGINT?
    trap(:INT) do
      kill
      exit 130
    end
  end

  if @launch_options.handle_SIGTERM?
    trap(:TERM) do
      close
    end
  end

  if @launch_options.handle_SIGHUP? && !Puppeteer.env.windows?
    trap(:HUP) do
      close
    end
  end
end

#wait_for_ws_endpoint(browser_process, timeout, preferred_revision) ⇒ String

Parameters:

Returns:

  • (String)


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
# File 'lib/puppeteer/browser_runner.rb', line 178

private def wait_for_ws_endpoint(browser_process, timeout, preferred_revision)
  lines = []
  wait_for_endpoint = lambda do
    loop do
      line = browser_process.stderr.readline
      /^WebDriver BiDi listening on (ws:\/\/.*)$/.match(line) do |m|
        raise NotImplementedError.new('WebDriver BiDi support is not yet implemented')
      end

      /^DevTools listening on (ws:\/\/.*)$/.match(line) do |m|
        return m[1].gsub(/\r/, '')
      end
      lines << line
    end
  end

  if timeout && timeout > 0
    Puppeteer::AsyncUtils.async_timeout(timeout, wait_for_endpoint).wait
  else
    wait_for_endpoint.call
  end
rescue EOFError
  raise LaunchError.new("\n#{lines.join("\n")}\nTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md")
rescue Async::TimeoutError
  raise Puppeteer::TimeoutError.new("Timed out after #{timeout} ms while trying to connect to the browser! Only Chrome at revision r#{preferred_revision} is guaranteed to work.")
end