Class: Ferrum::Browser::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/browser/options.rb,
lib/ferrum/browser/options/chrome.rb,
sig/ferrum/browser/options.rbs,
sig/ferrum/browser/options/chrome.rbs

Overview

Resolves and normalizes the options hash passed to Browser.new, applying defaults for connection settings (host, port, timeouts), window size, and other launch behavior. Used throughout the browser to look up user-configured settings.

Defined Under Namespace

Classes: Chrome

Constant Summary collapse

BROWSER_PORT =

Returns:

  • (String)
"0"
BROWSER_HOST =

Returns:

  • (String)
"127.0.0.1"
WINDOW_SIZE =

Returns:

  • (::Array[::Integer])
[1024, 768].freeze
BASE_URL_SCHEMES =

Returns:

  • (::Array[String])
%w[http https].freeze
DEFAULT_TIMEOUT =

Returns:

  • (::Float)
ENV.fetch("FERRUM_DEFAULT_TIMEOUT", 5).to_f
PROCESS_TIMEOUT =

Returns:

  • (::Integer)
ENV.fetch("FERRUM_PROCESS_TIMEOUT", 10).to_i
EXTENSIONS_DEPRECATED =

Returns:

  • (String)
"The :extensions option will be removed in Ferrum 1.0. Use :init_scripts instead."
EXTENSIONS_READER_DEPRECATED =

Returns:

  • (String)
"extensions will be removed in Ferrum 1.0. Use init_scripts instead."
ADD_EXTENSION_DEPRECATED =

Returns:

  • (String)
"add_extension will be removed in Ferrum 1.0. Use add_init_script instead."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Options

Returns a new instance of Options.

Parameters:

  • options (Hash[Symbol, untyped], nil) (defaults to: nil)


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
# File 'lib/ferrum/browser/options.rb', line 31

def initialize(options = nil)
  @options = Hash(options&.dup)

  @port = @options.fetch(:port, BROWSER_PORT)
  @host = @options.fetch(:host, BROWSER_HOST)
  @timeout = @options.fetch(:timeout, DEFAULT_TIMEOUT)
  @window_size = @options.fetch(:window_size, WINDOW_SIZE)
  @headless = @options.fetch(:headless, true)
  @incognito = @options.fetch(:incognito, true)
  @dockerize = @options.fetch(:dockerize, false)
  @process_timeout = @options.fetch(:process_timeout, PROCESS_TIMEOUT)
  @slowmo = @options[:slowmo].to_f

  @env = @options[:env]
  @xvfb = @options[:xvfb]
  @save_path = @options[:save_path]
  @browser_path = @options[:browser_path]
  @ws_max_receive_size = @options[:ws_max_receive_size]
  @ssl_context = @options[:ssl_context]
  @ignore_default_browser_options = @options[:ignore_default_browser_options]

  @proxy = validate_proxy(@options[:proxy])
  @logger, @log = parse_logger(@options[:logger])
  @base_url = parse_base_url(@options[:base_url]) if @options[:base_url]
  @url = @options[:url].to_s if @options[:url]
  @ws_url = @options[:ws_url].to_s if @options[:ws_url]
  @init_scripts = parse_init_scripts
  @page_handlers = [].freeze

  @options = @options.merge(window_size: @window_size).freeze
  @browser_options = @options.fetch(:browser_options, {}).freeze
end

Instance Attribute Details

#base_url ⇒ ::Addressable::URI?

Returns the value of attribute base_url.

Returns:

  • (::Addressable::URI, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def base_url
  @base_url
end

#browser_options ⇒ Hash[String, (String | bool | nil)] (readonly)

Returns the value of attribute browser_options.

Returns:

  • (Hash[String, (String | bool | nil)])


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def browser_options
  @browser_options
end

#browser_path ⇒ String? (readonly)

Returns the value of attribute browser_path.

Returns:

  • (String, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def browser_path
  @browser_path
end

#default_user_agent ⇒ String?

Returns the value of attribute default_user_agent.

Returns:

  • (String, nil)


29
30
31
# File 'lib/ferrum/browser/options.rb', line 29

def default_user_agent
  @default_user_agent
end

#dockerize ⇒ Object (readonly)

Returns the value of attribute dockerize.



24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def dockerize
  @dockerize
end

#env ⇒ Hash[String, String]? (readonly)

Returns the value of attribute env.

Returns:

  • (Hash[String, String], nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def env
  @env
end

#headless ⇒ Boolean (readonly)

Returns the value of attribute headless.

Returns:

  • (Boolean)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def headless
  @headless
end

#host ⇒ String (readonly)

Returns the value of attribute host.

Returns:

  • (String)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def host
  @host
end

#ignore_default_browser_options ⇒ Boolean? (readonly)

Returns the value of attribute ignore_default_browser_options.

Returns:

  • (Boolean, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def ignore_default_browser_options
  @ignore_default_browser_options
end

#incognito ⇒ Boolean (readonly)

Returns the value of attribute incognito.

Returns:

  • (Boolean)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def incognito
  @incognito
end

#init_scripts ⇒ ::Array[String] (readonly)

Returns the value of attribute init_scripts.

Returns:

  • (::Array[String])


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def init_scripts
  @init_scripts
end

#log ⇒ Log? (readonly)

Returns the value of attribute log.

Returns:



24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def log
  @log
end

#logger ⇒ Object (readonly)

Returns the value of attribute logger.

Returns:

  • (Object)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def logger
  @logger
end

#page_handlers ⇒ Array<Proc> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The blocks registered with #on_page.

Returns:

  • (Array<Proc>)


156
157
158
# File 'lib/ferrum/browser/options.rb', line 156

def page_handlers
  @page_handlers
end

#port ⇒ String, ::Integer (readonly)

Returns the value of attribute port.

Returns:

  • (String, ::Integer)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def port
  @port
end

#process_timeout ⇒ ::Integer (readonly)

Returns the value of attribute process_timeout.

Returns:

  • (::Integer)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def process_timeout
  @process_timeout
end

#proxy ⇒ Hash[Symbol, (String | ::Integer)]? (readonly)

Returns the value of attribute proxy.

Returns:

  • (Hash[Symbol, (String | ::Integer)], nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def proxy
  @proxy
end

#save_path ⇒ String? (readonly)

Returns the value of attribute save_path.

Returns:

  • (String, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def save_path
  @save_path
end

#slowmo ⇒ ::Float (readonly)

Returns the value of attribute slowmo.

Returns:

  • (::Float)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def slowmo
  @slowmo
end

#ssl_context ⇒ ::OpenSSL::SSL::SSLContext? (readonly)

Returns the value of attribute ssl_context.

Returns:

  • (::OpenSSL::SSL::SSLContext, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def ssl_context
  @ssl_context
end

#timeout ⇒ ::Numeric

Returns the value of attribute timeout.

Returns:

  • (::Numeric)


29
30
31
# File 'lib/ferrum/browser/options.rb', line 29

def timeout
  @timeout
end

#url ⇒ String? (readonly)

Returns the value of attribute url.

Returns:

  • (String, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def url
  @url
end

#window_size ⇒ ::Array[::Integer] (readonly)

Returns the value of attribute window_size.

Returns:

  • (::Array[::Integer])


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def window_size
  @window_size
end

#ws_max_receive_size ⇒ ::Integer? (readonly)

Returns the value of attribute ws_max_receive_size.

Returns:

  • (::Integer, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def ws_max_receive_size
  @ws_max_receive_size
end

#ws_url ⇒ String? (readonly)

Returns the value of attribute ws_url.

Returns:

  • (String, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def ws_url
  @ws_url
end

#xvfb ⇒ Boolean? (readonly)

Returns the value of attribute xvfb.

Returns:

  • (Boolean, nil)


24
25
26
# File 'lib/ferrum/browser/options.rb', line 24

def xvfb
  @xvfb
end

Instance Method Details

#add_extension(extension) ⇒ Array<String>

Deprecated.

Adds a script to run in every new document.

Parameters:

  • extension (String, Hash) —

    A path to a JS file, or { source: "..." }.

Returns:

  • (Array<String>)

Raises:

  • (Errno::ENOENT) —

    If the file doesn't exist.



110
111
112
113
# File 'lib/ferrum/browser/options.rb', line 110

def add_extension(extension)
  Utils::Deprecate.warn(ADD_EXTENSION_DEPRECATED, user_code: true)
  add_init_script(extension)
end

#add_init_script(script) ⇒ Array<String>

Adds a script to run in every new document, before the document's own scripts. Applies to pages and iframes attached after the call: pages already open keep the scripts they were attached with.

Examples:

browser.add_init_script("/path/to/stub.js")
browser.add_init_script(source: "window.secret = 'top'")

Parameters:

  • script (String, Hash) —

    A path to a JS file, or { source: "..." }.

Returns:

  • (Array<String>) —

    JS source code of every init script.

Raises:

  • (Errno::ENOENT) —

    If the file doesn't exist.



93
94
95
# File 'lib/ferrum/browser/options.rb', line 93

def add_init_script(script)
  @init_scripts = [*@init_scripts, read_init_script(script)].freeze
end

#extensions ⇒ Array<String>

Deprecated.

JS source code of every init script.

Returns:

  • (Array<String>)


122
123
124
125
# File 'lib/ferrum/browser/options.rb', line 122

def extensions
  Utils::Deprecate.warn(EXTENSIONS_READER_DEPRECATED, user_code: true)
  init_scripts
end

#on_page {|page| ... } ⇒ nil

Calls the block with every new page before Ferrum returns it, so the page can be set up before anything runs on it. That covers pages you create and pages other pages open, like popups. Pages already open when you call this aren't passed.

Examples:

browser.on_page { |page| page.on(:dialog, &:accept) }

Yields:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (nil)

Raises:

  • (ArgumentError) —

    If no block is given.



142
143
144
145
146
147
# File 'lib/ferrum/browser/options.rb', line 142

def on_page(&block)
  raise ArgumentError, "on_page requires a block" unless block

  @page_handlers = [*@page_handlers, block].freeze
  nil
end

#to_h ⇒ Hash{Symbol => Object}

Raw options hash used to initialize the browser.

Returns:

  • (Hash{Symbol => Object})


187
188
189
# File 'lib/ferrum/browser/options.rb', line 187

def to_h
  @options
end

#validate_proxy(options) ⇒ Hash?

Validates the :proxy option, if given.

Parameters:

  • options (Hash, nil) —

    The :proxy option as passed to #initialize.

Returns:

  • (Hash, nil) —

    The same options, unchanged.

Raises:

  • (ArgumentError) —

    If options is not a Hash, or is a Hash without :host or :port.



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ferrum/browser/options.rb', line 170

def validate_proxy(options)
  return unless options

  raise ArgumentError, "proxy options must be a Hash" unless options.is_a?(Hash)

  if options[:host].nil? && options[:port].nil?
    raise ArgumentError, "proxy options must be a Hash with at least :host | :port"
  end

  options
end