class Selenium::WebDriver::Remote::Capabilities

Specification of the desired and/or actual capabilities of the browser that the server is being asked to create.

Constants

KNOWN

Attributes

capabilities[R]

Public Class Methods

always_match(capabilities) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 112
def always_match(capabilities)
  new(always_match: capabilities)
end
camel_case(str_or_sym) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 151
def camel_case(str_or_sym)
  str_or_sym.to_s.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
end
chrome(opts = {}) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 72
def chrome(opts = {})
  new({
    browser_name: 'chrome'
  }.merge(opts))
end
edge(opts = {}) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 78
def edge(opts = {})
  new({
    browser_name: 'MicrosoftEdge'
  }.merge(opts))
end
Also aliased as: microsoftedge
ff(opts = {})
Alias for: firefox
firefox(opts = {}) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 85
def firefox(opts = {})
  new({
    browser_name: 'firefox'
  }.merge(opts))
end
Also aliased as: ff
first_match(*capabilities) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 116
def first_match(*capabilities)
  new(first_match: capabilities)
end
htmlunit(opts = {}) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 98
def htmlunit(opts = {})
  new({
    browser_name: 'htmlunit'
  }.merge(opts))
end
ie(opts = {})
Alias for: internet_explorer
internet_explorer(opts = {}) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 104
def internet_explorer(opts = {})
  new({
    browser_name: 'internet explorer',
    platform_name: :windows
  }.merge(opts))
end
Also aliased as: ie
json_create(data) click to toggle source

@api private

# File lib/selenium/webdriver/remote/capabilities.rb, line 124
def json_create(data)
  data = data.dup
  caps = new

  process_timeouts(caps, data.delete('timeouts'))

  if data.key?('proxy')
    proxy = data.delete('proxy')
    caps.proxy = Proxy.json_create(proxy) unless proxy.nil? || proxy.empty?
  end

  # Remote Server Specific
  if data.key?('webdriver.remote.sessionid')
    caps[:remote_session_id] = data.delete('webdriver.remote.sessionid')
  end

  KNOWN.each do |cap|
    data_value = camel_case(cap)
    caps[cap] = data.delete(data_value) if data.key?(data_value)
  end

  # any remaining pairs will be added as is, with no conversion
  caps.merge!(data)

  caps
end
microsoftedge(opts = {})
Alias for: edge
new(opts = {}) click to toggle source

@param [Hash] opts @option :browser_name [String] required browser name @option :browser_version [String] required browser version number @option :platform_name [Symbol] one of :any, :win, :mac, or :x @option :accept_insecure_certs [Boolean] does the driver accept insecure SSL certifications? @option :proxy [Selenium::WebDriver::Proxy, Hash] proxy configuration

@api public

# File lib/selenium/webdriver/remote/capabilities.rb, line 177
def initialize(opts = {})
  @capabilities = {}
  self.proxy = opts.delete(:proxy) if opts[:proxy]
  @capabilities.merge!(opts)
end
safari(opts = {}) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 92
def safari(opts = {})
  new({
    browser_name: Selenium::WebDriver::Safari.technology_preview? ? "Safari Technology Preview" : 'safari'
  }.merge(opts))
end

Private Class Methods

process_timeouts(caps, timeouts) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 157
def process_timeouts(caps, timeouts)
  return if timeouts.nil?

  caps.implicit_timeout = timeouts['implicit']
  caps.page_load_timeout = timeouts['pageLoad']
  caps.script_timeout = timeouts['script']
end

Public Instance Methods

==(other) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 266
def ==(other)
  return false unless other.is_a? self.class

  as_json == other.as_json
end
Also aliased as: eql?
[](key) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 191
def [](key)
  @capabilities[key]
end
[]=(key, value) click to toggle source

Allows setting arbitrary capabilities.

# File lib/selenium/webdriver/remote/capabilities.rb, line 187
def []=(key, value)
  @capabilities[key] = value
end
as_json(*) click to toggle source

@api private

# File lib/selenium/webdriver/remote/capabilities.rb, line 256
def as_json(*)
  @capabilities.each_with_object({}) do |(key, value), hash|
    hash[convert_key(key)] = process_capabilities(key, value, hash)
  end
end
eql?(other)
Alias for: ==
implicit_timeout() click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 228
def implicit_timeout
  timeouts[:implicit]
end
implicit_timeout=(timeout) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 232
def implicit_timeout=(timeout)
  timeouts[:implicit] = timeout
end
merge!(other) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 195
def merge!(other)
  if other.respond_to?(:capabilities, true) && other.capabilities.is_a?(Hash)
    @capabilities.merge! other.capabilities
  elsif other.is_a? Hash
    @capabilities.merge! other
  else
    raise ArgumentError, 'argument should be a Hash or implement #capabilities'
  end
end
page_load_timeout() click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 236
def page_load_timeout
  timeouts[:page_load] || timeouts[:pageLoad]
end
page_load_timeout=(timeout) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 240
def page_load_timeout=(timeout)
  timeouts[:page_load] = timeout
end
proxy() click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 205
def proxy
  @capabilities[:proxy]
end
proxy=(proxy) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 209
def proxy=(proxy)
  case proxy
  when Hash
    @capabilities[:proxy] = Proxy.new(proxy)
  when Proxy, nil
    @capabilities[:proxy] = proxy
  else
    raise TypeError, "expected Hash or #{Proxy.name}, got #{proxy.inspect}:#{proxy.class}"
  end
end
script_timeout() click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 244
def script_timeout
  timeouts[:script]
end
script_timeout=(timeout) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 248
def script_timeout=(timeout)
  timeouts[:script] = timeout
end
timeouts() click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 220
def timeouts
  @capabilities[:timeouts] ||= {}
end
timeouts=(timeouts) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 224
def timeouts=(timeouts)
  @capabilities[:timeouts] = timeouts
end
to_json(*) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 262
def to_json(*)
  JSON.generate as_json
end

Private Instance Methods

convert_key(key) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 295
def convert_key(key)
  case key
  when String
    key.to_s
  when Symbol
    self.class.camel_case(key)
  else
    raise TypeError, "expected String or Symbol, got #{key.inspect}:#{key.class}"
  end
end
convert_value(key, value) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 306
def convert_value(key, value)
  case key
  when :platform
    value.to_s.upcase
  when :proxy
    value&.as_json
  when :unhandled_prompt_behavior
    value.is_a?(Symbol) ? value.to_s.tr('_', ' ') : value
  else
    value
  end
end
process_capabilities(key, value, hash) click to toggle source
# File lib/selenium/webdriver/remote/capabilities.rb, line 280
def process_capabilities(key, value, hash)
  case value
  when Array
    value.map { |v| process_capabilities(key, v, hash) }
  when Hash
    value.each_with_object({}) do |(k, v), h|
      h[convert_key(k)] = process_capabilities(k, v, h)
    end
  when Capabilities, Options
    value.as_json
  else
    convert_value(key, value)
  end
end