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

DEFAULTS =
{
  browser_name: '',
  version: '',
  platform: :any,
  javascript_enabled: false,
  css_selectors_enabled: false,
  takes_screenshot: false,
  native_events: false,
  rotatable: false,
  firefox_profile: nil,
  proxy: nil
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Capabilities

Returns a new instance of Capabilities.

Parameters:

  • :browser_name (Hash)

    a customizable set of options

  • :version (Hash)

    a customizable set of options

  • :platform (Hash)

    a customizable set of options

  • :javascript_enabled (Hash)

    a customizable set of options

  • :css_selectors_enabled (Hash)

    a customizable set of options

  • :takes_screenshot (Hash)

    a customizable set of options

  • :native_events (Hash)

    a customizable set of options

  • :proxy (Hash)

    a customizable set of options


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

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

Class Method Details

.chrome(opts = {}) ⇒ Object


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

def chrome(opts = {})
  new({
    browser_name: 'chrome',
    javascript_enabled: true,
    css_selectors_enabled: true
  }.merge(opts))
end

.edge(opts = {}) ⇒ Object


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

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

.firefox(opts = {}) ⇒ Object


90
91
92
93
94
95
96
97
98
# File 'lib/selenium/webdriver/remote/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

.firefox_legacy(opts = {}) ⇒ Object


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

def firefox_legacy(opts = {})
  new({
    browser_name: 'firefox',
    javascript_enabled: true,
    takes_screenshot: true,
    css_selectors_enabled: true
  }.merge(opts))
end

.htmlunit(opts = {}) ⇒ Object


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

def htmlunit(opts = {})
  new({
    browser_name: 'htmlunit'
  }.merge(opts))
end

.htmlunitwithjs(opts = {}) ⇒ Object


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

def htmlunitwithjs(opts = {})
  new({
    browser_name: 'htmlunit',
    javascript_enabled: true
  }.merge(opts))
end

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


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

def internet_explorer(opts = {})
  new({
    browser_name: 'internet explorer',
    platform: :windows,
    takes_screenshot: true,
    css_selectors_enabled: true,
    native_events: true
  }.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.


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 157

def json_create(data)
  data = data.dup

  caps = new
  caps.browser_name          = data.delete('browserName')
  caps.version               = data.delete('version')
  caps.platform              = data.delete('platform').downcase.tr(' ', '_').to_sym if data.key?('platform')
  caps.javascript_enabled    = data.delete('javascriptEnabled')
  caps.css_selectors_enabled = data.delete('cssSelectorsEnabled')
  caps.takes_screenshot      = data.delete('takesScreenshot')
  caps.native_events         = data.delete('nativeEvents')
  caps.rotatable             = data.delete('rotatable')
  caps.proxy                 = Proxy.json_create(data['proxy']) if data.key?('proxy') && !data['proxy'].empty?

  # any remaining pairs will be added as is, with no conversion
  caps.merge!(data)

  caps
end

.phantomjs(opts = {}) ⇒ Object


133
134
135
136
137
138
139
140
141
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 133

def phantomjs(opts = {})
  WebDriver.logger.deprecate 'Selenium support for PhantomJS', 'headless Chrome/Firefox or HTMLUnit'
  new({
    browser_name: 'phantomjs',
    javascript_enabled: true,
    takes_screenshot: true,
    css_selectors_enabled: true
  }.merge(opts))
end

.safari(opts = {}) ⇒ Object


143
144
145
146
147
148
149
150
151
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 143

def safari(opts = {})
  new({
    browser_name: 'safari',
    platform: :mac,
    javascript_enabled: true,
    takes_screenshot: true,
    css_selectors_enabled: true
  }.merge(opts))
end

Instance Method Details

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


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

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

  as_json == other.as_json
end

#[](key) ⇒ Object


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

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

#[]=(key, value) ⇒ Object

Allows setting arbitrary capabilities.


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

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.


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 233

def as_json(*) # rubocop:disable Metrics/CyclomaticComplexity
  hash = {}

  @capabilities.each do |key, value|
    case key
    when :platform
      hash['platform'] = value.to_s.upcase
    when :firefox_profile
      if value
        WebDriver.logger.deprecate(':firefox_profile capabilitiy', 'Selenium::WebDriver::Firefox::Options#profile')
        hash['firefox_profile'] = value.as_json['zip']
      end
    when :proxy
      hash['proxy'] = value.as_json if value
    when String, :firefox_binary
      if key == :firefox_binary && value
        WebDriver.logger.deprecate(':firefox_binary capabilitiy', 'Selenium::WebDriver::Firefox::Options#binary')
      end
      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

#javascript_enabledObject Also known as: javascript_enabled?

Returns javascript_enabled capability. It is true if not set explicitly.


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

def javascript_enabled
  javascript_enabled = @capabilities.fetch(:javascript_enabled)
  javascript_enabled.nil? ? true : javascript_enabled
end

#merge!(other) ⇒ Object


208
209
210
211
212
213
214
215
216
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 208

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


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

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


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

def to_json(*)
  JSON.generate as_json
end