Class: Selenium::WebDriver::Remote::W3C::Capabilities Private

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/remote/w3c/capabilities.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Constant Summary collapse

EXTENSION_CAPABILITY_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A[\w-]+:.*\z/
KNOWN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  :browser_name,
  :browser_version,
  :platform_name,
  :accept_insecure_certs,
  :page_load_strategy,
  :proxy,
  :set_window_rect,
  :timeouts,
  :unhandled_prompt_behavior,

  # remote-specific
  :remote_session_id,

  # TODO (alex): deprecate in favor of Firefox::Options?
  :accessibility_checks,
  :device,

  # TODO (alex): deprecate compatibility with OSS-capabilities
  :implicit_timeout,
  :page_load_timeout,
  :script_timeout,
].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



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

def initialize(opts = {})
  @capabilities = opts
  self.proxy = opts.delete(:proxy)
end

Class Method Details

.edge(opts = {}) ⇒ 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.



83
84
85
86
87
88
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 83

def edge(opts = {})
  new({
    browser_name: 'MicrosoftEdge',
    platform: :windows
  }.merge(opts))
end

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

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.



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

def firefox(opts = {})
  opts[:browser_version] = opts.delete(:version) if opts.key?(:version)
  opts[:platform_name] = opts.delete(:platform) if opts.key?(:platform)
  opts[:timeouts] = {}
  opts[:timeouts]['implicit'] = opts.delete(:implicit_timeout) if opts.key?(:implicit_timeout)
  opts[:timeouts]['pageLoad'] = opts.delete(:page_load_timeout) if opts.key?(:page_load_timeout)
  opts[:timeouts]['script'] = opts.delete(:script_timeout) if opts.key?(:script_timeout)
  new({browser_name: 'firefox', marionette: true}.merge(opts))
end

.from_oss(oss_capabilities) ⇒ 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.

Creates W3C compliant capabilities from OSS ones.

Parameters:



143
144
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 143

def from_oss(oss_capabilities)
  w3c_capabilities = new

  # TODO (alex): make capabilities enumerable?
  oss_capabilities = oss_capabilities.__send__(:capabilities) unless oss_capabilities.is_a?(Hash)
  oss_capabilities.each do |name, value|
    next if value.nil?
    next if value.is_a?(String) && value.empty?

    capability_name = name.to_s

    snake_cased_capability_names = KNOWN.map(&:to_s)
    camel_cased_capability_names = snake_cased_capability_names.map(&w3c_capabilities.method(:camel_case))

    next unless snake_cased_capability_names.include?(capability_name) ||
                camel_cased_capability_names.include?(capability_name) ||
                capability_name.match(EXTENSION_CAPABILITY_PATTERN)

    w3c_capabilities[name] = value
  end

  # User can pass :firefox_options or :firefox_profile.
  #
  # TODO (alex): Refactor this whole method into converter class.
  firefox_options = oss_capabilities['firefoxOptions'] || oss_capabilities['firefox_options'] || oss_capabilities[:firefox_options]
  firefox_profile = oss_capabilities['firefox_profile'] || oss_capabilities[:firefox_profile]
  firefox_binary  = oss_capabilities['firefox_binary'] || oss_capabilities[:firefox_binary]

  if firefox_profile && firefox_options
    second_profile = firefox_options['profile'] || firefox_options[:profile]
    if second_profile && firefox_profile != second_profile
      raise Error::WebDriverError, 'You cannot pass 2 different Firefox profiles'
    end
  end

  if firefox_options || firefox_profile || firefox_binary
    options = WebDriver::Firefox::Options.new(firefox_options || {})
    options.binary = firefox_binary if firefox_binary
    options.profile = firefox_profile if firefox_profile
    w3c_capabilities.merge!(options.as_json)
  end

  w3c_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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 106

def json_create(data)
  data = data.dup

  caps = new
  caps.browser_name = data.delete('browserName')
  caps.browser_version = data.delete('browserVersion')
  caps.platform_name = data.delete('platformName')
  caps.accept_insecure_certs = data.delete('acceptInsecureCerts') if data.key?('acceptInsecureCerts')
  caps.page_load_strategy = data.delete('pageLoadStrategy')
  timeouts = data.delete('timeouts')
  caps.implicit_timeout = timeouts['implicit'] if timeouts
  caps.page_load_timeout = timeouts['pageLoad'] if timeouts
  caps.script_timeout = timeouts['script'] if timeouts

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

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

  # Marionette Specific
  caps[:accessibility_checks] = data.delete('moz:accessibilityChecks')
  caps[:profile] = data.delete('moz:profile')
  caps[:rotatable] = data.delete('rotatable')
  caps[:device] = data.delete('device')

  # 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?

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.



273
274
275
276
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 273

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

#[](key) ⇒ 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.



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

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

#[]=(key, value) ⇒ 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.

Allows setting arbitrary capabilities.



209
210
211
# File 'lib/selenium/webdriver/remote/w3c/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.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 242

def as_json(*)
  hash = {}

  @capabilities.each do |key, value|
    case key
    when :platform
      hash['platform'] = value.to_s.upcase
    when :proxy
      if value
        hash['proxy'] = value.as_json
        hash['proxy']['proxyType'] &&= hash['proxy']['proxyType'].downcase
        if hash['proxy']['noProxy'].is_a?(String)
          hash['proxy']['noProxy'] = hash['proxy']['noProxy'].split(', ')
        end
      end
    when String, :firefox_binary
      hash[key.to_s] = value
    when Symbol
      hash[camel_case(key.to_s)] = value
    else
      raise TypeError, "expected String or Symbol, got #{key.inspect}:#{key.class} / #{value.inspect}"
    end
  end

  hash
end

#merge!(other) ⇒ 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.



217
218
219
220
221
222
223
224
225
# File 'lib/selenium/webdriver/remote/w3c/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

#proxy=(proxy) ⇒ 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.



227
228
229
230
231
232
233
234
235
236
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 227

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

#to_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.



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

def to_json(*)
  JSON.generate as_json
end