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

Firefox-specific options:

Parameters:

  • a customizable set of options

  • a customizable set of options

  • a customizable set of options

  • a customizable set of options

  • a customizable set of options

  • a customizable set of options

  • a customizable set of options

  • a customizable set of options

  • a customizable set of options

API:

  • public



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

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

Class Method Details

.chrome(opts = {}) ⇒ Object



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

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

.edge(opts = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 70

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

.firefox(opts = {}) ⇒ Object



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

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

.htmlunit(opts = {}) ⇒ Object



89
90
91
92
93
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 89

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

.htmlunitwithjs(opts = {}) ⇒ Object



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

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

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



102
103
104
105
106
107
108
109
110
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 102

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.

API:

  • private



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 136

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



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

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

.safari(opts = {}) ⇒ Object



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

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?



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

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

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object

Allows setting arbitrary capabilities.



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

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.

API:

  • private



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

def as_json(*)
  hash = {}

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



191
192
193
194
195
196
197
198
199
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 191

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



201
202
203
204
205
206
207
208
209
210
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 201

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



239
240
241
# File 'lib/selenium/webdriver/remote/capabilities.rb', line 239

def to_json(*)
  JSON.generate as_json
end