Class: Selenium::WebDriver::Options

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

Constant Summary collapse

W3C_OPTIONS =
%i[browser_name browser_version platform_name accept_insecure_certs page_load_strategy proxy
set_window_rect timeouts unhandled_prompt_behavior strict_file_interactability
web_socket_url].freeze
GRID_OPTIONS =
%i[enable_downloads].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Options

Returns a new instance of Options.



71
72
73
74
75
76
# File 'rb/lib/selenium/webdriver/common/options.rb', line 71

def initialize(**opts)
  self.class.set_capabilities

  @options = opts
  @options[:browser_name] = self.class::BROWSER
end

Class Attribute Details

.driver_pathObject (readonly)

Returns the value of attribute driver_path.



30
31
32
# File 'rb/lib/selenium/webdriver/common/options.rb', line 30

def driver_path
  @driver_path
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



69
70
71
# File 'rb/lib/selenium/webdriver/common/options.rb', line 69

def options
  @options
end

Class Method Details

.chrome(**opts) ⇒ Object



32
33
34
# File 'rb/lib/selenium/webdriver/common/options.rb', line 32

def chrome(**opts)
  Chrome::Options.new(**opts)
end

.edge(**opts) ⇒ Object Also known as: microsoftedge



45
46
47
# File 'rb/lib/selenium/webdriver/common/options.rb', line 45

def edge(**opts)
  Edge::Options.new(**opts)
end

.firefox(**opts) ⇒ Object



36
37
38
# File 'rb/lib/selenium/webdriver/common/options.rb', line 36

def firefox(**opts)
  Firefox::Options.new(**opts)
end

.ie(**opts) ⇒ Object Also known as: internet_explorer



40
41
42
# File 'rb/lib/selenium/webdriver/common/options.rb', line 40

def ie(**opts)
  IE::Options.new(**opts)
end

.safari(**opts) ⇒ Object



50
51
52
# File 'rb/lib/selenium/webdriver/common/options.rb', line 50

def safari(**opts)
  Safari::Options.new(**opts)
end

.set_capabilitiesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'rb/lib/selenium/webdriver/common/options.rb', line 54

def set_capabilities
  (W3C_OPTIONS + self::CAPABILITIES.keys).each do |key|
    next if method_defined? key

    define_method key do
      @options[key]
    end

    define_method :"#{key}=" do |value|
      @options[key] = value
    end
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



94
95
96
97
98
# File 'rb/lib/selenium/webdriver/common/options.rb', line 94

def ==(other)
  return false unless other.is_a? self.class

  as_json == other.as_json
end

#add_option(name, value = nil) ⇒ Object

Add a new option not yet handled by bindings.

Examples:

Leave Chrome open when chromedriver is killed

options = Selenium::WebDriver::Chrome::Options.new
options.add_option(:detach, true)

Parameters:

  • name (String, Symbol)

    Name of the option

  • value (Boolean, String, Integer) (defaults to: nil)

    Value of the option



89
90
91
92
# File 'rb/lib/selenium/webdriver/common/options.rb', line 89

def add_option(name, value = nil)
  name, value = name.first if value.nil? && name.is_a?(Hash)
  @options[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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'rb/lib/selenium/webdriver/common/options.rb', line 106

def as_json(*)
  options = @options.dup

  downloads = options.delete(:enable_downloads)
  options['se:downloadsEnabled'] = downloads unless downloads.nil?
  w3c_options = process_w3c_options(options)

  browser_options = self.class::CAPABILITIES.each_with_object({}) do |(capability_alias, capability_name), hash|
    capability_value = options.delete(capability_alias)
    hash[capability_name] = capability_value unless capability_value.nil?
  end

  raise Error::WebDriverError, "These options are not w3c compliant: #{options}" unless options.empty?

  browser_options = {self.class::KEY => browser_options} if defined?(self.class::KEY)

  process_browser_options(browser_options)
  generate_as_json(w3c_options.merge(browser_options))
end