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

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

Overview

TODO - uncomment when Mozilla fixes this: bugzilla.mozilla.org/show_bug.cgi?id=1326397

Constant Summary collapse

KNOWN =

TODO (alex): compare with spec

[
  :browser_name,
  :browser_version,
  :platform_name,
  :platform_version,
  :accept_insecure_certs,
  :page_load_strategy,
  :proxy,
  :remote_session_id,
  :accessibility_checks,
  :device,
  :implicit_timeout,
  :page_load_timeout,
  :script_timeout,
].freeze
BROWSER_SPECIFIC =
['moz:firefoxOptions'].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

  • :platform_version (Hash)

    a customizable set of options

  • :accept_insecure_certs (Hash)

    a customizable set of options

  • :proxy (Hash)

    a customizable set of options



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

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

Class Method Details

.edge(opts = {}) ⇒ Object



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

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

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



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

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]['page load'] = 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(capabilities) ⇒ Object

Creates W3C compliant capabilities from OSS ones.

Parameters:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 146

def from_oss(capabilities)
  w3c_capabilties = new
  # TODO (alex): make capabilities enumerable?
  oss_capabilties = capabilities.__send__(:capabilities)
  oss_capabilties.each do |name, value|
    next if value.nil?
    next if value.is_a?(String) && value.empty?

    if w3c_capabilties.respond_to?("#{name}=")
      w3c_capabilties.__send__("#{name}=", value)
    elsif BROWSER_SPECIFIC.include?(name)
      w3c_capabilties.merge!(name => value)
    end

  end

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



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
137
138
139
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 108

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.platform_version = data.delete('platformVersion')
  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?



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

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

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object

Allows setting arbitrary capabilities.



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

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.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 220

def as_json(*)
  hash = {}

  @capabilities.each do |key, value|
    case key
    when :platform
      hash['platform'] = value.to_s.upcase
    when :proxy
      hash['proxy'] = value.as_json if value
    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



195
196
197
198
199
200
201
202
203
# File 'lib/selenium/webdriver/remote/w3c/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

#proxy=(proxy) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/selenium/webdriver/remote/w3c/capabilities.rb', line 205

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



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

def to_json(*)
  JSON.generate as_json
end