123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::Remote::Capabilities

Relationships & Source Files
Inherits: Object
Defined in: rb/lib/selenium/webdriver/remote/capabilities.rb

Overview

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

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(opts = {}) ⇒ Capabilities

Parameters:

  • opts (Hash) (defaults to: {})
  • :browser_name (Hash)

    a customizable set of options

  • :browser_version (Hash)

    a customizable set of options

  • :platform_name (Hash)

    a customizable set of options

  • :accept_insecure_certs (Hash)

    a customizable set of options

  • :proxy (Hash)

    a customizable set of options

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 127

def initialize(opts = {})
  @capabilities = {}
  self.proxy = opts.delete(:proxy) if opts[:proxy]
  @capabilities.merge!(opts)
end

Class Method Details

.always_match(capabilities)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 61

def always_match(capabilities)
  new(always_match: capabilities)
end

.camel_case(str_or_sym)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 101

def camel_case(str_or_sym)
  str_or_sym.to_s.gsub(/_([a-z])/) { Regexp.last_match(1)&.upcase }
end

.first_match(*capabilities)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 65

def first_match(*capabilities)
  new(first_match: capabilities)
end

.json_create(data)

This method is for internal use only.
[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 73

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

.process_timeouts(caps, timeouts) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 107

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

Instance Attribute Details

#implicit_timeout (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 178

def implicit_timeout
  timeouts[:implicit]
end

#implicit_timeout=(timeout) (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 182

def implicit_timeout=(timeout)
  timeouts[:implicit] = timeout
end

#page_load_timeout (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 186

def page_load_timeout
  timeouts[:page_load] || timeouts[:pageLoad]
end

#page_load_timeout=(timeout) (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 190

def page_load_timeout=(timeout)
  timeouts[:page_load] = timeout
end

#proxy (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 155

def proxy
  @capabilities[:proxy]
end

#proxy=(proxy) (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 159

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 (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 194

def script_timeout
  timeouts[:script]
end

#script_timeout=(timeout) (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 198

def script_timeout=(timeout)
  timeouts[:script] = timeout
end

#timeouts (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 170

def timeouts
  @capabilities[:timeouts] ||= {}
end

#timeouts=(timeouts) (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 174

def timeouts=(timeouts)
  @capabilities[:timeouts] = timeouts
end

Instance Method Details

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

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 216

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

  as_json == other.as_json
end

#[](key)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 141

def [](key)
  @capabilities[key]
end

#[]=(key, value)

Allows setting arbitrary capabilities.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 137

def []=(key, value)
  @capabilities[key] = value
end

#as_json

This method is for internal use only.
[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 206

def as_json(*)
  @capabilities.each_with_object({}) do |(key, value), hash|
    hash[convert_key(key)] = process_capabilities(key, value, hash)
  end
end

#convert_key(key) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 245

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) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 256

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

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 222

alias eql? ==

#merge!(other)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 145

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

#process_capabilities(key, value, hash) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 230

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

#to_json

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/remote/capabilities.rb', line 212

def to_json(*)
  JSON.generate as_json
end