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

Inherits:
Object
  • Object
show all
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 collapse

KNOWN =
[
  :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,

  # remote-specific (webdriver.remote.sessionid)
  :remote_session_id
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Capabilities

Returns a new instance of 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



127
128
129
130
131
# 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) ⇒ Object



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

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

.camel_case(str_or_sym) ⇒ Object



101
102
103
# 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) ⇒ Object



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

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

.json_create(data) ⇒ Object

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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

Instance Method Details

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



216
217
218
219
220
# 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) ⇒ Object



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

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

#[]=(key, value) ⇒ Object

Allows setting arbitrary capabilities.



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

def []=(key, value)
  @capabilities[key] = 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.



206
207
208
209
210
# 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

#implicit_timeoutObject



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

def implicit_timeout
  timeouts[:implicit]
end

#implicit_timeout=(timeout) ⇒ Object



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

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

#merge!(other) ⇒ Object



145
146
147
148
149
150
151
152
153
# 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

#page_load_timeoutObject



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

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

#page_load_timeout=(timeout) ⇒ Object



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

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

#proxyObject



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

def proxy
  @capabilities[:proxy]
end

#proxy=(proxy) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# 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_timeoutObject



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

def script_timeout
  timeouts[:script]
end

#script_timeout=(timeout) ⇒ Object



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

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

#timeoutsObject



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

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

#timeouts=(timeouts) ⇒ Object



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

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

#to_jsonObject



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

def to_json(*)
  JSON.generate as_json
end