Class: Ferrum::Browser::Options::Base

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ferrum/browser/options/base.rb

Overview

Abstract base for browser-specific default option builders. Subclasses (e.g. Chrome, Firefox) declare their own DEFAULT_OPTIONS and PLATFORM_PATH constants and implement #merge_required/#merge_default to produce the final CLI flags used by Command.

Direct Known Subclasses

Chrome, Firefox

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.options ⇒ Base

Singleton instance holding the browser's default options.

Returns:



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

def self.options
  instance
end

.version ⇒ String?

The installed browser's version string, obtained by running its binary with --version.

Returns:

  • (String, nil) —

    The version output, or nil if the binary can't be found/run.



33
34
35
36
37
38
# File 'lib/ferrum/browser/options/base.rb', line 33

def self.version
  out, = Open3.capture2(instance.detect_path, "--version")
  out.strip
rescue Errno::ENOENT
  nil
end

Instance Method Details

#detect_path ⇒ String?

Locates the browser executable on the system.

Returns:

  • (String, nil) —

    Absolute path to the browser binary, or nil if not found.



67
68
69
# File 'lib/ferrum/browser/options/base.rb', line 67

def detect_path
  Binary.find(self.class::PLATFORM_PATH[Utils::Platform.platform_name])
end

#except(*keys) ⇒ Hash{String => Object}

Default CLI flags excluding the given keys.

Parameters:

  • keys (Array<String>) —

    Flag names to exclude.

Returns:

  • (Hash{String => Object})


57
58
59
# File 'lib/ferrum/browser/options/base.rb', line 57

def except(*keys)
  to_h.except(*keys)
end

#merge_default(flags, options) ⇒ Hash{String => Object}

Merges this browser's default CLI flags (see #to_h) into flags, unless options.ignore_default_browser_options is set. Abstract; overridden by Chrome/Firefox.

Parameters:

  • flags (Hash{String => Object}) —

    The flags accumulated so far.

  • options (Options) —

    The user-supplied browser options.

Returns:

  • (Hash{String => Object})

Raises:



107
108
109
# File 'lib/ferrum/browser/options/base.rb', line 107

def merge_default(flags, options)
  raise NotImplementedError
end

#merge_required(flags, options, user_data_dir) ⇒ Hash{String => Object}

Merges the CLI flags that are always required to drive the browser over CDP (e.g. remote debugging port, user data dir) into flags. Abstract; overridden by Chrome/Firefox.

Parameters:

  • flags (Hash{String => Object}) —

    The flags accumulated so far.

  • options (Options) —

    The user-supplied browser options.

  • user_data_dir (String) —

    Path to the browser's user data directory.

Returns:

  • (Hash{String => Object})

Raises:



88
89
90
# File 'lib/ferrum/browser/options/base.rb', line 88

def merge_required(flags, options, user_data_dir)
  raise NotImplementedError
end

#to_h ⇒ Hash{String => Object}

Default CLI flags for the browser.

Returns:

  • (Hash{String => Object})


45
46
47
# File 'lib/ferrum/browser/options/base.rb', line 45

def to_h
  self.class::DEFAULT_OPTIONS
end