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

Inherits:
Object
  • Object
show all
Defined in:
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.



199
200
201
202
203
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 199

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

Class Method Details

.always_match(capabilities) ⇒ Object



133
134
135
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 133

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

.camel_case(str_or_sym) ⇒ Object



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

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

.chrome(opts = {}) ⇒ Object



85
86
87
88
89
90
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 85

def chrome(opts = {})
  WebDriver.logger.deprecate('Remote::Capabilities.chrome', 'Options.chrome', id: :caps_browsers)
  new({
    browser_name: 'chrome'
  }.merge(opts))
end

.edge(opts = {}) ⇒ Object Also known as: microsoftedge



92
93
94
95
96
97
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 92

def edge(opts = {})
  WebDriver.logger.deprecate('Remote::Capabilities.edge', 'Options.edge', id: :caps_browsers)
  new({
    browser_name: 'MicrosoftEdge'
  }.merge(opts))
end

.firefox(opts = {}) ⇒ Object Also known as: ff



100
101
102
103
104
105
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 100

def firefox(opts = {})
  WebDriver.logger.deprecate('Remote::Capabilities.firefox', 'Options.firefox', id: :caps_browsers)
  new({
    browser_name: 'firefox'
  }.merge(opts))
end

.first_match(*capabilities) ⇒ Object



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

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

.htmlunit(opts = {}) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 115

def htmlunit(opts = {})
  WebDriver.logger.deprecate('Remote::Capabilities.htmlunit',
                             'as argument in constructor',
                             id: :caps_browsers)
  new({
    browser_name: 'htmlunit'
  }.merge(opts))
end

.internet_explorer(opts = {}) ⇒ Object Also known as: ie



124
125
126
127
128
129
130
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 124

def internet_explorer(opts = {})
  WebDriver.logger.deprecate('Remote::Capabilities.ie', 'Options.ie', id: :caps_browsers)
  new({
    browser_name: 'internet explorer',
    platform_name: :windows
  }.merge(opts))
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.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 145

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

.safari(opts = {}) ⇒ Object



108
109
110
111
112
113
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 108

def safari(opts = {})
  WebDriver.logger.deprecate('Remote::Capabilities.safari', 'Options.safari', id: :caps_browsers)
  new({
    browser_name: Selenium::WebDriver::Safari.technology_preview? ? 'Safari Technology Preview' : 'safari'
  }.merge(opts))
end

Instance Method Details

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



288
289
290
291
292
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 288

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

  as_json == other.as_json
end

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object

Allows setting arbitrary capabilities.



209
210
211
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 209

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.



278
279
280
281
282
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 278

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

#implicit_timeoutObject



250
251
252
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 250

def implicit_timeout
  timeouts[:implicit]
end

#implicit_timeout=(timeout) ⇒ Object



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

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

#merge!(other) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 217

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



258
259
260
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 258

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

#page_load_timeout=(timeout) ⇒ Object



262
263
264
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 262

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

#platformObject



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

def platform
  WebDriver.logger.deprecate('`Capabilities#platform`', '`Capabilities#platform_name`', id: :jwp_caps)
  platform_name
end

#platform=(value) ⇒ Object



75
76
77
78
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 75

def platform=(value)
  WebDriver.logger.deprecate('`Capabilities#platform=`', '`Capabilities#platform_name=`', id: :jwp_caps)
  self.platform_name = value
end

#proxyObject



227
228
229
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 227

def proxy
  @capabilities[:proxy]
end

#proxy=(proxy) ⇒ Object



231
232
233
234
235
236
237
238
239
240
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 231

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



266
267
268
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 266

def script_timeout
  timeouts[:script]
end

#script_timeout=(timeout) ⇒ Object



270
271
272
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 270

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

#timeoutsObject



242
243
244
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 242

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

#timeouts=(timeouts) ⇒ Object



246
247
248
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 246

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

#to_jsonObject



284
285
286
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 284

def to_json(*)
  JSON.generate as_json
end

#versionObject

Backward compatibility



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

def version
  WebDriver.logger.deprecate('`Capabilities#version`', '`Capabilities#browser_version`', id: :jwp_caps)
  browser_version
end

#version=(value) ⇒ Object



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

def version=(value)
  WebDriver.logger.deprecate('`Capabilities#version=`', '`Capabilities#browser_version=`', id: :jwp_caps)
  self.browser_version = value
end