Class: Ferrum::Browser
- Inherits:
-
Object
- Object
- Ferrum::Browser
- Extended by:
- Forwardable
- Defined in:
- lib/ferrum/browser.rb,
lib/ferrum/browser/xvfb.rb,
lib/ferrum/browser/binary.rb,
lib/ferrum/browser/command.rb,
lib/ferrum/browser/options.rb,
lib/ferrum/browser/process.rb,
lib/ferrum/browser/version_info.rb,
lib/ferrum/browser/options/chrome.rb,
lib/ferrum/browser/process/killer.rb,
lib/ferrum/browser/process/output.rb,
sig/ferrum/browser.rbs,
sig/ferrum/browser/xvfb.rbs,
sig/ferrum/browser/binary.rbs,
sig/ferrum/browser/command.rbs,
sig/ferrum/browser/options.rbs,
sig/ferrum/browser/process.rbs,
sig/ferrum/browser/version_info.rbs,
sig/ferrum/browser/options/chrome.rbs,
sig/ferrum/browser/process/killer.rbs,
sig/ferrum/browser/process/output.rbs
Overview
A Browser can host multiple Contexts (like incognito profiles)
and multiple Pages within each of them, reachable through #contexts
and #create_page.
The main entry point of the library. Instantiating it spawns (or connects
to) a browser process and opens a CDP connection to it, exposing a single
default Page that most of the top-level methods (go_to, at_css,
screenshot, etc.) are delegated to for convenience.
Defined Under Namespace
Modules: Binary Classes: Command, Options, Process, VersionInfo, Xvfb
Constant Summary collapse
- RESET_DEPRECATED =
"browser.reset will be removed in Ferrum 1.0. Use browser.contexts.reset instead."
Instance Attribute Summary collapse
-
#client ⇒ ::Ferrum::Client
readonly
Returns the value of attribute client.
-
#contexts ⇒ Contexts
readonly
Returns the value of attribute contexts.
-
#options ⇒ Browser::Options
readonly
Returns the value of attribute options.
-
#process ⇒ Browser::Process
readonly
Returns the value of attribute process.
Instance Method Summary collapse
- #add_init_script ⇒ ::Array[String]
- #base_url ⇒ String?
- #base_url= ⇒ String
-
#close ⇒ nil
Shuts the browser down gracefully with the CDP
Browser.closecommand, a remote browser included. -
#command(method, timeout: nil, **params) ⇒ Hash
Sends a CDP command to the browser itself, not to the default page like most other
Browsermethods, #on included. -
#crash ⇒ Hash[String, untyped]
Crashes browser.
-
#create_page(new_context: false, new_window: false, proxy: nil) {|arg0| ... } ⇒ Ferrum::Page
Creates a new page.
-
#debug(bind = nil) ⇒ void
Opens headless session in the browser devtools frontend.
- #default_user_agent ⇒ String?
- #default_user_agent= ⇒ String
-
#disconnect ⇒ nil
Closes the client connection, leaving the browser running, whether Ferrum launched it or connected to it.
- #init_scripts ⇒ ::Array[String]
-
#initialize(options = nil) ⇒ Browser
constructor
Initializes the browser.
-
#quit(wait: true) ⇒ Thread?
Closes the client connection and terminates the browser process Ferrum launched.
-
#reset ⇒ nil
deprecated
Deprecated.
Use Contexts#reset via
browser.contexts.reset. -
#restart ⇒ void
Restarts the browser process, keeping the same options.
-
#shutdown(wait: true) ⇒ Thread?
Shuts the browser down for good: asks it to exit with #close, closes the connection and, for a browser Ferrum launched, lets the process exit on its own before killing it and removing its user data directory.
- #timeout ⇒ ::Numeric
- #timeout= ⇒ ::Numeric
-
#version ⇒ VersionInfo
Gets the version information from the browser.
Constructor Details
Instance Attribute Details
#client ⇒ ::Ferrum::Client (readonly)
Returns the value of attribute client.
50 51 52 |
# File 'lib/ferrum/browser.rb', line 50 def client @client end |
#contexts ⇒ Contexts (readonly)
Returns the value of attribute contexts.
50 51 52 |
# File 'lib/ferrum/browser.rb', line 50 def contexts @contexts end |
#options ⇒ Browser::Options (readonly)
Returns the value of attribute options.
50 51 52 |
# File 'lib/ferrum/browser.rb', line 50 def @options end |
#process ⇒ Browser::Process (readonly)
Returns the value of attribute process.
50 51 52 |
# File 'lib/ferrum/browser.rb', line 50 def process @process end |
Instance Method Details
#add_init_script ⇒ ::Array[String]
36 |
# File 'sig/ferrum/browser.rbs', line 36
def add_init_script: (String | Hash[Symbol, String] script) -> ::Array[String]
|
#base_url ⇒ String?
19 |
# File 'sig/ferrum/browser.rbs', line 19
def base_url: () -> String?
|
#base_url= ⇒ String
21 |
# File 'sig/ferrum/browser.rbs', line 21
def base_url=: (String) -> String
|
#close ⇒ nil
Shuts the browser down gracefully with the CDP Browser.close command,
a remote browser included. Only the browser exits: the connection and a
launched process are still to be cleaned up, with #disconnect and
browser.process.stop, or with #quit.
319 320 321 322 |
# File 'lib/ferrum/browser.rb', line 319 def close command("Browser.close") nil end |
#command(method, timeout: nil, **params) ⇒ Hash
Sends a CDP command to the browser itself, not to the default page like
most other Browser methods, #on included. Use page.command for page
domains such as Page, Runtime or DOM.
343 344 345 |
# File 'lib/ferrum/browser.rb', line 343 def command(method, timeout: nil, **params) client.command(method, timeout: timeout || self.timeout, **params) end |
#crash ⇒ Hash[String, untyped]
Crashes browser.
307 308 309 |
# File 'lib/ferrum/browser.rb', line 307 def crash command("Browser.crash") end |
#create_page(new_context:, new_window:, proxy:) ⇒ Page #create_page(new_context:, new_window:, proxy:) ⇒ T
Creates a new page.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ferrum/browser.rb', line 174 def create_page(new_context: false, new_window: false, proxy: nil) page = if new_context || proxy params = {} if proxy .validate_proxy(proxy) params.merge!(proxyServer: "#{proxy[:host]}:#{proxy[:port]}") params.merge!(proxyBypassList: proxy[:bypass]) if proxy[:bypass] end context = contexts.create(**params) context.create_page(new_window: new_window, proxy: proxy) else default_context.create_page(new_window: new_window) end block_given? ? yield(page) : page ensure if block_given? page&.close context&.dispose if new_context end end |
#debug(bind = nil) ⇒ void
This method returns an undefined value.
Opens headless session in the browser devtools frontend.
367 368 369 370 371 372 373 374 375 376 |
# File 'lib/ferrum/browser.rb', line 367 def debug(bind = nil) ::Process.spawn(process.path, debug_url) bind ||= binding if bind.respond_to?(:pry) Pry.start(bind) else bind.irb end end |
#default_user_agent ⇒ String?
27 |
# File 'sig/ferrum/browser.rbs', line 27
def default_user_agent: () -> String?
|
#default_user_agent= ⇒ String
29 |
# File 'sig/ferrum/browser.rbs', line 29
def default_user_agent=: (String) -> String
|
#disconnect ⇒ nil
293 294 295 296 297 298 299 300 301 302 |
# File 'lib/ferrum/browser.rb', line 293 def disconnect return unless @client begin @client.close ensure @client = @contexts = nil end nil end |
#init_scripts ⇒ ::Array[String]
34 |
# File 'sig/ferrum/browser.rbs', line 34
def init_scripts: () -> ::Array[String]
|
#quit(wait: true) ⇒ Thread?
Closes the client connection and terminates the browser process Ferrum
launched. A remote browser, connected to with :url or :ws_url, is
only disconnected from and keeps running; see #close to shut it down.
Also stops a browser left running by #disconnect.
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/ferrum/browser.rb', line 235 def quit(wait: true) return if @client.nil? && @process.nil? begin disconnect ensure thread = @process&.stop(wait: wait) @process = nil end thread end |
#reset ⇒ nil
Use Contexts#reset via browser.contexts.reset.
Closes browser tabs opened by the Browser instance.
207 208 209 210 211 |
# File 'lib/ferrum/browser.rb', line 207 def reset Utils::Deprecate.warn(RESET_DEPRECATED, user_code: true) contexts.reset nil end |
#restart ⇒ void
This method returns an undefined value.
Restarts the browser process, keeping the same options.
216 217 218 219 |
# File 'lib/ferrum/browser.rb', line 216 def restart quit start end |
#shutdown(wait: true) ⇒ Thread?
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/ferrum/browser.rb', line 264 def shutdown(wait: true) return if @client.nil? && @process.nil? begin close_browser if @client ensure begin disconnect ensure thread = @process&.stop(wait: wait, grace: Process::Killer::KILL_TIMEOUT) @process = nil end end thread end |
#timeout ⇒ ::Numeric
23 |
# File 'sig/ferrum/browser.rbs', line 23
def timeout: () -> ::Numeric
|
#timeout= ⇒ ::Numeric
25 |
# File 'sig/ferrum/browser.rbs', line 25
def timeout=: (::Numeric) -> ::Numeric
|
#version ⇒ VersionInfo
Gets the version information from the browser.
356 357 358 |
# File 'lib/ferrum/browser.rb', line 356 def version VersionInfo.new(command("Browser.getVersion")) end |