class Selenium::WebDriver::Options
def ==(other)
def ==(other) return false unless other.is_a? self.class as_json == other.as_json end
def add_option(name, value = nil)
def add_option(name, value = nil) name, value = name.first if value.nil? && name.is_a?(Hash) unless name.to_s.include?(':') WebDriver.logger.deprecate('Options#add_option for w3c or browser specific capabilities', 'applicable attribute accessors or pass into constructor', id: :add_option) end @options[name] = value end
def as_json(*)
def as_json(*) options = @options.dup w3c_options = process_w3c_options(options) browser_options = self.class::CAPABILITIES.each_with_object({}) do |(capability_alias, capability_name), hash| from_name = options.delete(capability_name) from_alias = options.delete(capability_alias) capability_value = if !from_name.nil? && capability_alias != capability_name WebDriver.logger.deprecate("#{capability_name} as option", capability_alias.to_s, id: :option_symbols) from_name elsif !from_alias.nil? from_alias end hash[capability_name] = capability_value unless capability_value.nil? end unless options.empty? msg = 'These options are not w3c compliant and will result in failures in a future release' WebDriver.logger.warn("#{msg}: #{options}", id: :w3c_options) browser_options.merge!(options) end 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
def camel_case(str)
def camel_case(str) str.gsub(/_([a-z])/) { Regexp.last_match(1).upcase } end
def camelize?(_key)
def camelize?(_key) true end
def chrome(**opts)
def chrome(**opts) Chrome::Options.new(**opts) end
def convert_json_key(key, camelize: true)
def convert_json_key(key, camelize: true) key = key.to_s if key.is_a?(Symbol) key = camel_case(key) if camelize return key if key.is_a?(String) raise TypeError, "expected String or Symbol, got #{key.inspect}:#{key.class}" end
def edge(**opts)
def edge(**opts) Edge::Options.new(**opts) end
def firefox(**opts)
def firefox(**opts) Firefox::Options.new(**opts) end
def generate_as_json(value, camelize_keys: true)
def generate_as_json(value, camelize_keys: true) if value.is_a?(Hash) process_json_hash(value, camelize_keys) elsif value.respond_to?(:as_json) value.as_json elsif value.is_a?(Array) value.map { |val| generate_as_json(val, camelize_keys: camelize_keys) } elsif value.is_a?(Symbol) value.to_s else value end end
def ie(**opts)
def ie(**opts) IE::Options.new(**opts) end
def initialize(**opts)
def initialize(**opts) self.class.set_capabilities @options = opts @options[:browser_name] = self.class::BROWSER end
def process_browser_options(_browser_options)
def process_browser_options(_browser_options) nil end
def process_json_hash(value, camelize_keys)
def process_json_hash(value, camelize_keys) value.each_with_object({}) do |(key, val), hash| next if val.respond_to?(:empty?) && val.empty? camelize = camelize_keys ? camelize?(key) : false key = convert_json_key(key, camelize: camelize) hash[key] = generate_as_json(val, camelize_keys: camelize) end end
def process_w3c_options(options)
def process_w3c_options(options) w3c_options = options.select { |key, _val| w3c?(key) } w3c_options[:unhandled_prompt_behavior] &&= w3c_options[:unhandled_prompt_behavior]&.to_s&.tr('_', ' ') options.delete_if { |key, _val| w3c?(key) } w3c_options end
def safari(**opts)
def safari(**opts) Safari::Options.new(**opts) end
def set_capabilities
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
def w3c?(key)
def w3c?(key) W3C_OPTIONS.include?(key) || key.to_s.include?(':') end