Class: Ferrum::Browser::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/browser/command.rb

Overview

Builds the OS-level command used to spawn the browser process: resolves the executable path and merges the browser-specific required/default flags (see Options::Base) with any user-supplied ones into a single argument list.

Constant Summary collapse

NOT_FOUND =
"Could not find an executable for the browser. Try to make " \
"it available on the PATH or set environment variable for " \
"example BROWSER_PATH=\"/usr/bin/chrome\""

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults, options, user_data_dir) ⇒ Command

Returns a new instance of Command.



33
34
35
36
37
38
39
40
41
42
# File 'lib/ferrum/browser/command.rb', line 33

def initialize(defaults, options, user_data_dir)
  @flags = {}
  @defaults = defaults
  @options = options
  @user_data_dir = user_data_dir
  @path = options.browser_path || ENV.fetch("BROWSER_PATH", nil) || defaults.detect_path
  raise BinaryNotFoundError, NOT_FOUND unless @path

  merge_options
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



31
32
33
# File 'lib/ferrum/browser/command.rb', line 31

def defaults
  @defaults
end

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/ferrum/browser/command.rb', line 31

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/ferrum/browser/command.rb', line 31

def path
  @path
end

Class Method Details

.build(options, user_data_dir) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ferrum/browser/command.rb', line 18

def self.build(options, user_data_dir)
  defaults = case options.browser_name
             when :firefox
               Options::Firefox.options
             when :chrome, :opera, :edge, nil
               Options::Chrome.options
             else
               raise NotImplementedError, "not supported browser"
             end

  new(defaults, options, user_data_dir)
end

Instance Method Details

#to_aArray<String>

Command line arguments for spawning the browser process.

Returns:

  • (Array<String>)

    The browser path followed by its CLI flags.



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

def to_a
  [path] + @flags.map { |k, v| v.nil? ? "--#{k}" : "--#{k}=#{v}" }
end

#to_sString

String representation of the command used to spawn the browser process.

Returns:

  • (String)


66
67
68
# File 'lib/ferrum/browser/command.rb', line 66

def to_s
  to_a.join(" \\ \n  ")
end

#xvfb?Boolean

Whether the browser should be launched under Xvfb.

Returns:

  • (Boolean)


47
48
49
# File 'lib/ferrum/browser/command.rb', line 47

def xvfb?
  !!options.xvfb
end