Class: Selenium::WebDriver::Chrome::Options

Inherits:
Options
  • Object
show all
Defined in:
build/rb/lib/selenium/webdriver/chrome/options.rb

Direct Known Subclasses

EdgeChrome::Options

Constant Summary collapse

KEY =
'goog:chromeOptions'
BROWSER =
'chrome'
CAPABILITIES =
{args: 'args',
binary: 'binary',
extensions: 'extensions',
local_state: 'localState',
prefs: 'prefs',
detach: 'detach',
debugger_address: 'debuggerAddress',
exclude_switches: 'excludeSwitches',
minidump_path: 'minidumpPath',
emulation: 'mobileEmulation',
perf_logging_prefs: 'perfLoggingPrefs',
window_types: 'windowTypes'}.freeze

Constants inherited from Options

Options::W3C_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Options

#options

Instance Method Summary collapse

Methods inherited from Options

#add_option, #as_json

Constructor Details

#initialize(profile: nil, encoded_extensions: nil, **opts) ⇒ Options

Create a new Options instance.

Examples:

options = Selenium::WebDriver::Chrome::Options.new(args: ['start-maximized', 'user-data-dir=/tmp/temp_profile'])
driver = Selenium::WebDriver.for(:chrome, options: options)

Parameters:

  • :profile (Profile)

    An instance of a Chrome::Profile Class

  • :encoded_extensions (Array)

    List of extensions that do not need to be Base64 encoded

  • opts (Hash)

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

Options Hash (**opts):

  • :args (Array<String>)

    List of command-line arguments to use when starting Chrome

  • :binary (String)

    Path to the Chrome executable to use

  • :prefs (Hash)

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

  • :extensions (Array<String>)

    A list of paths to (.crx) Chrome extensions to install on startup

  • :options (Hash)

    A hash for raw options

  • :emulation (Hash)

    A hash for raw emulation options

  • :local_state (Hash)

    A hash for the Local State file in the user data folder

  • :detach (Boolean)

    whether browser is closed when the driver is sent the quit command

  • :debugger_address (String)

    address of a Chrome debugger server to connect to

  • :exclude_switches (Array<String>)

    command line switches to exclude

  • :minidump_path (String)

    Directory to store Chrome minidumps (linux only)

  • :perf_logging_prefs (Hash)

    A hash for performance logging preferences

  • :window_types (Array<String>)

    A list of window types to appear in the list of window handles



77
78
79
80
81
82
83
# File 'build/rb/lib/selenium/webdriver/chrome/options.rb', line 77

def initialize(profile: nil, encoded_extensions: nil, **opts)
  super(**opts)

  @profile = profile
  @options[:encoded_extensions] = encoded_extensions if encoded_extensions
  @options[:extensions]&.each(&method(:validate_extension))
end

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



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

def profile
  @profile
end

Instance Method Details

#add_argument(arg) ⇒ Object

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

Examples:

Start Chrome maximized

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('start-maximized')

Parameters:

  • arg (String)

    The command-line argument to add



127
128
129
130
# File 'build/rb/lib/selenium/webdriver/chrome/options.rb', line 127

def add_argument(arg)
  @options[:args] ||= []
  @options[:args] << arg
end

#add_emulation(**opts) ⇒ Object

Add emulation device information

see: chromedriver.chromium.org/mobile-emulation

Examples:

Start Chrome in mobile emulation mode by device name

options = Selenium::WebDriver::Chrome::Options.new
options.add_emulation(device_name: 'iPhone 6')

Start Chrome in mobile emulation mode by device metrics

options = Selenium::WebDriver::Chrome::Options.new
options.add_emulation(device_metrics: {width: 400, height: 800, pixelRatio: 1, touch: true})

Parameters:

  • opts (Hash)

    the pre-defined options for adding mobile emulation values

Options Hash (**opts):

  • :device_name (String)

    A valid device name from the Chrome DevTools Emulation panel

  • :device_metrics (Hash)

    Hash containing width, height, pixelRatio, touch

  • :user_agent (String)

    Full user agent



179
180
181
# File 'build/rb/lib/selenium/webdriver/chrome/options.rb', line 179

def add_emulation(**opts)
  @options[:emulation] = opts
end

#add_encoded_extension(encoded) ⇒ Object Also known as: encoded_extension=

Add an extension by Base64-encoded string.

Examples:

options = Selenium::WebDriver::Chrome::Options.new
options.add_encoded_extension(encoded_string)

Parameters:

  • encoded (String)

    The Base64-encoded string of the .crx file



111
112
113
114
# File 'build/rb/lib/selenium/webdriver/chrome/options.rb', line 111

def add_encoded_extension(encoded)
  @options[:encoded_extensions] ||= []
  @options[:encoded_extensions] << encoded
end

#add_extension(path) ⇒ Object

Add an extension by local path.

Examples:

options = Selenium::WebDriver::Chrome::Options.new
options.add_extension('/path/to/extension.crx')

Parameters:

  • path (String)

    The local path to the .crx file



95
96
97
98
99
# File 'build/rb/lib/selenium/webdriver/chrome/options.rb', line 95

def add_extension(path)
  validate_extension(path)
  @options[:extensions] ||= []
  @options[:extensions] << path
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::Chrome::Options.new
options.add_preference('homepage', 'http://www.seleniumhq.com/')

Parameters:

  • name (String)

    Key of the preference

  • value (Boolean, String, Integer)

    Value of the preference



143
144
145
146
# File 'build/rb/lib/selenium/webdriver/chrome/options.rb', line 143

def add_preference(name, value)
  @options[:prefs] ||= {}
  @options[:prefs][name] = value
end

#headless!Object

Run Chrome in headless mode.

Examples:

Enable headless mode

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


156
157
158
# File 'build/rb/lib/selenium/webdriver/chrome/options.rb', line 156

def headless!
  add_argument '--headless'
end