Class: Ferrum::Browser::Options
- Inherits:
-
Object
- Object
- Ferrum::Browser::Options
- 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 =
"0"- BROWSER_HOST =
"127.0.0.1"- WINDOW_SIZE =
[1024, 768].freeze
- BASE_URL_SCHEMES =
%w[http https].freeze
- DEFAULT_TIMEOUT =
ENV.fetch("FERRUM_DEFAULT_TIMEOUT", 5).to_f
- PROCESS_TIMEOUT =
ENV.fetch("FERRUM_PROCESS_TIMEOUT", 10).to_i
- EXTENSIONS_DEPRECATED =
"The :extensions option will be removed in Ferrum 1.0. Use :init_scripts instead."- EXTENSIONS_READER_DEPRECATED =
"extensions will be removed in Ferrum 1.0. Use init_scripts instead."- ADD_EXTENSION_DEPRECATED =
"add_extension will be removed in Ferrum 1.0. Use add_init_script instead."
Instance Attribute Summary collapse
-
#base_url ⇒ ::Addressable::URI?
Returns the value of attribute base_url.
-
#browser_options ⇒ Hash[String, (String | bool | nil)]
readonly
Returns the value of attribute browser_options.
-
#browser_path ⇒ String?
readonly
Returns the value of attribute browser_path.
-
#default_user_agent ⇒ String?
Returns the value of attribute default_user_agent.
-
#dockerize ⇒ Object
readonly
Returns the value of attribute dockerize.
-
#env ⇒ Hash[String, String]?
readonly
Returns the value of attribute env.
-
#headless ⇒ Boolean
readonly
Returns the value of attribute headless.
-
#host ⇒ String
readonly
Returns the value of attribute host.
-
#ignore_default_browser_options ⇒ Boolean?
readonly
Returns the value of attribute ignore_default_browser_options.
-
#incognito ⇒ Boolean
readonly
Returns the value of attribute incognito.
-
#init_scripts ⇒ ::Array[String]
readonly
Returns the value of attribute init_scripts.
-
#log ⇒ Log?
readonly
Returns the value of attribute log.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#page_handlers ⇒ Array<Proc>
readonly
private
The blocks registered with #on_page.
-
#port ⇒ String, ::Integer
readonly
Returns the value of attribute port.
-
#process_timeout ⇒ ::Integer
readonly
Returns the value of attribute process_timeout.
-
#proxy ⇒ Hash[Symbol, (String | ::Integer)]?
readonly
Returns the value of attribute proxy.
-
#save_path ⇒ String?
readonly
Returns the value of attribute save_path.
-
#slowmo ⇒ ::Float
readonly
Returns the value of attribute slowmo.
-
#ssl_context ⇒ ::OpenSSL::SSL::SSLContext?
readonly
Returns the value of attribute ssl_context.
-
#timeout ⇒ ::Numeric
Returns the value of attribute timeout.
-
#url ⇒ String?
readonly
Returns the value of attribute url.
-
#window_size ⇒ ::Array[::Integer]
readonly
Returns the value of attribute window_size.
-
#ws_max_receive_size ⇒ ::Integer?
readonly
Returns the value of attribute ws_max_receive_size.
-
#ws_url ⇒ String?
readonly
Returns the value of attribute ws_url.
-
#xvfb ⇒ Boolean?
readonly
Returns the value of attribute xvfb.
Instance Method Summary collapse
-
#add_extension(extension) ⇒ Array<String>
deprecated
Deprecated.
Use #add_init_script.
-
#add_init_script(script) ⇒ Array<String>
Adds a script to run in every new document, before the document's own scripts.
-
#extensions ⇒ Array<String>
deprecated
Deprecated.
Use #init_scripts.
-
#initialize(options = nil) ⇒ Options
constructor
A new instance of Options.
-
#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.
-
#to_h ⇒ Hash{Symbol => Object}
Raw options hash used to initialize the browser.
-
#validate_proxy(options) ⇒ Hash?
Validates the
:proxyoption, if given.
Constructor Details
#initialize(options = nil) ⇒ Options
Returns a new instance of Options.
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( = nil) @options = Hash(&.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.
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.
24 25 26 |
# File 'lib/ferrum/browser/options.rb', line 24 def @browser_options end |
#browser_path ⇒ String? (readonly)
Returns the value of attribute browser_path.
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.
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.
24 25 26 |
# File 'lib/ferrum/browser/options.rb', line 24 def env @env end |
#headless ⇒ Boolean (readonly)
Returns the value of attribute headless.
24 25 26 |
# File 'lib/ferrum/browser/options.rb', line 24 def headless @headless end |
#host ⇒ String (readonly)
Returns the value of attribute host.
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.
24 25 26 |
# File 'lib/ferrum/browser/options.rb', line 24 def @ignore_default_browser_options end |
#incognito ⇒ Boolean (readonly)
Returns the value of attribute incognito.
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.
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.
24 25 26 |
# File 'lib/ferrum/browser/options.rb', line 24 def log @log end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
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.
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.
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.
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.
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.
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.
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.
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.
29 30 31 |
# File 'lib/ferrum/browser/options.rb', line 29 def timeout @timeout end |
#url ⇒ String? (readonly)
Returns the value of attribute url.
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.
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.
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.
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.
24 25 26 |
# File 'lib/ferrum/browser/options.rb', line 24 def xvfb @xvfb end |
Instance Method Details
#add_extension(extension) ⇒ Array<String>
Use #add_init_script.
Adds a script to run in every new document.
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.
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>
Use #init_scripts.
JS source code of every init script.
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.
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.
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.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/ferrum/browser/options.rb', line 170 def validate_proxy() return unless raise ArgumentError, "proxy options must be a Hash" unless .is_a?(Hash) if [:host].nil? && [:port].nil? raise ArgumentError, "proxy options must be a Hash with at least :host | :port" end end |