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:

  • 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



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

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.



22
23
24
# File 'lib/selenium/webdriver/firefox/options.rb', line 22

def args
  @args
end

#binaryObject

Returns the value of attribute binary.



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

def binary
  @binary
end

#log_levelObject

Returns the value of attribute log_level.



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

def log_level
  @log_level
end

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/selenium/webdriver/firefox/options.rb', line 22

def options
  @options
end

#prefsObject (readonly)

Returns the value of attribute prefs.



22
23
24
# File 'lib/selenium/webdriver/firefox/options.rb', line 22

def prefs
  @prefs
end

#profileObject

Returns the value of attribute profile.



22
23
24
# File 'lib/selenium/webdriver/firefox/options.rb', line 22

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:

  • The command-line argument to add



62
63
64
# File 'lib/selenium/webdriver/firefox/options.rb', line 62

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 of the option

  • Value of the option



77
78
79
# File 'lib/selenium/webdriver/firefox/options.rb', line 77

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:

  • Key of the preference

  • Value of the preference



92
93
94
# File 'lib/selenium/webdriver/firefox/options.rb', line 92

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.

API:

  • private



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/selenium/webdriver/firefox/options.rb', line 135

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

#headless!Object

Run Firefox in headless mode.

Examples:

Enable headless mode

options = Selenium::WebDriver::Firefox::Options.new
options.headless!


104
105
106
# File 'lib/selenium/webdriver/firefox/options.rb', line 104

def headless!
  add_argument '-headless'
end