Class: Selenium::WebDriver::Firefox::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/firefox/options.rb

Constant Summary collapse

KEY =
'moz:firefoxOptions'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Options

Create a new Options instance, only for W3C-capable versions of Firefox.

Examples:

options = Selenium::WebDriver::Firefox::Options.new(args: ['--host=127.0.0.1'])
driver = Selenium::WebDriver.for :firefox, options: options

Parameters:

  • opts (Hash)

    the pre-defined options to create the Firefox::Options with

Options Hash (**opts):

  • :binary (String)

    Path to the Firefox executable to use

  • :args (Array<String>)

    List of command-line arguments to use when starting geckodriver

  • :profile (Profile, String)

    Encoded profile string or Profile instance

  • :log_level (String, Symbol)

    Log level for geckodriver

  • :prefs (Hash)

    A hash with each entry consisting of the key of the preference and its value

  • :options (Hash)

    A hash for raw options



45
46
47
48
49
50
51
52
# File 'lib/selenium/webdriver/firefox/options.rb', line 45

def initialize(**opts)
  @args = opts.delete(:args) || []
  @binary = opts.delete(:binary)
  @profile = opts.delete(:profile)
  @log_level = opts.delete(:log_level)
  @prefs = opts.delete(:prefs) || {}
  @options = opts.delete(:options) || {}
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args



24
25
26
# File 'lib/selenium/webdriver/firefox/options.rb', line 24

def args
  @args
end

#binaryObject

Returns the value of attribute binary



25
26
27
# File 'lib/selenium/webdriver/firefox/options.rb', line 25

def binary
  @binary
end

#log_levelObject

Returns the value of attribute log_level



25
26
27
# File 'lib/selenium/webdriver/firefox/options.rb', line 25

def log_level
  @log_level
end

#optionsObject (readonly)

Returns the value of attribute options



24
25
26
# File 'lib/selenium/webdriver/firefox/options.rb', line 24

def options
  @options
end

#prefsObject (readonly)

Returns the value of attribute prefs



24
25
26
# File 'lib/selenium/webdriver/firefox/options.rb', line 24

def prefs
  @prefs
end

#profileObject

Returns the value of attribute profile



24
25
26
# File 'lib/selenium/webdriver/firefox/options.rb', line 24

def profile
  @profile
end

Instance Method Details

#add_argument(arg) ⇒ Object

Add a command-line argument to use when starting Firefox.

Examples:

Start geckodriver on a specific host

options = Selenium::WebDriver::Firefox::Options.new
options.add_argument('--host=127.0.0.1')

Parameters:

  • arg (String)

    The command-line argument to add



64
65
66
# File 'lib/selenium/webdriver/firefox/options.rb', line 64

def add_argument(arg)
  @args << arg
end

#add_option(name, value) ⇒ Object

Add a new option not yet handled by these bindings.

Examples:

options = Selenium::WebDriver::Firefox::Options.new
options.add_option(:foo, 'bar')

Parameters:

  • name (String, Symbol)

    Name of the option

  • value (Boolean, String, Integer)

    Value of the option



79
80
81
# File 'lib/selenium/webdriver/firefox/options.rb', line 79

def add_option(name, value)
  @options[name] = value
end

#add_preference(name, value) ⇒ Object

Add a preference that is only applied to the user profile in use.

Examples:

Set the default homepage

options = Selenium::WebDriver::Firefox::Options.new
options.add_preference('browser.startup.homepage', 'http://www.seleniumhq.com/')

Parameters:

  • name (String)

    Key of the preference

  • value (Boolean, String, Integer)

    Value of the preference



94
95
96
# File 'lib/selenium/webdriver/firefox/options.rb', line 94

def add_preference(name, value)
  prefs[name] = value
end

#as_jsonObject

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.



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/selenium/webdriver/firefox/options.rb', line 125

def as_json(*)
  opts = @options

  opts[:profile] = @profile.encoded if @profile
  opts[:args] = @args if @args.any?
  opts[:binary] = @binary if @binary
  opts[:prefs] = @prefs unless @prefs.empty?
  opts[:log] = {level: @log_level} if @log_level

  {KEY => opts}
end