class Selenium::WebDriver::Firefox::Options

Constants

KEY

Attributes

args[R]
binary[RW]
log_level[RW]
options[R]
prefs[R]
profile[R]

Public Class Methods

new(**opts) click to toggle source

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

@example

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

@param [Hash] opts the pre-defined options to create the Firefox::Options with @option opts [String] :binary Path to the Firefox executable to use @option opts [Array<String>] :args List of command-line arguments to use when starting geckodriver @option opts [Profile, String] :profile Encoded profile string or Profile instance @option opts [String, Symbol] :log_level Log level for geckodriver @option opts [Hash] :prefs A hash with each entry consisting of the key of the preference and its value @option opts [Hash] :options A hash for raw options

# File lib/selenium/webdriver/firefox/options.rb, line 45
def initialize(**opts)
  @args = Set.new(opts.delete(:args) || [])
  @binary = opts.delete(:binary)
  @profile = process_profile(opts.delete(:profile))
  @log_level = opts.delete(:log_level)
  @prefs = opts.delete(:prefs) || {}
  @options = opts.delete(:options) || {}
end

Public Instance Methods

add_argument(arg) click to toggle source

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

@example Start geckodriver on a specific host

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

@param [String] arg The command-line argument to add

# File lib/selenium/webdriver/firefox/options.rb, line 64
def add_argument(arg)
  @args << arg
end
add_option(name, value) click to toggle source

Add a new option not yet handled by these bindings.

@example

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

@param [String, Symbol] name Name of the option @param [Boolean, String, Integer] value Value of the option

# File lib/selenium/webdriver/firefox/options.rb, line 79
def add_option(name, value)
  @options[name] = value
end
add_preference(name, value) click to toggle source

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

@example Set the default homepage

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

@param [String] name Key of the preference @param [Boolean, String, Integer] value Value of the preference

# File lib/selenium/webdriver/firefox/options.rb, line 94
def add_preference(name, value)
  prefs[name] = value
end
as_json(*) click to toggle source

@api private

# File lib/selenium/webdriver/firefox/options.rb, line 133
def as_json(*)
  opts = @options

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

  {KEY => generate_as_json(opts)}
end
headless!() click to toggle source

Run Firefox in headless mode.

@example Enable headless mode

options = Selenium::WebDriver::Firefox::Options.new
options.headless!
# File lib/selenium/webdriver/firefox/options.rb, line 106
def headless!
  add_argument '-headless'
end
profile=(profile) click to toggle source

Sets Firefox profile.

@example Set the custom profile

profile = Selenium::WebDriver::Firefox::Profile.new
options = Selenium::WebDriver::Firefox::Options.new
options.profile = profile

@example Use existing profile

options = Selenium::WebDriver::Firefox::Options.new
options.profile = 'myprofile'

@param [Profile, String] profile Profile to be used

# File lib/selenium/webdriver/firefox/options.rb, line 125
def profile=(profile)
  @profile = process_profile(profile)
end

Private Instance Methods

process_profile(profile) click to toggle source
# File lib/selenium/webdriver/firefox/options.rb, line 147
def process_profile(profile)
  return unless profile

  case profile
  when Profile
    profile
  when String
    Profile.from_name(profile)
  else
    raise Error::WebDriverError, "don't know how to handle profile: #{profile.inspect}"
  end
end