Skip to content

Commit 494c4e3

Browse files
committed
[rb] deprecate all unrecognized capabilities for Options classes
This means that all options must be passed in as snake case symbols to constructor or use the provided attr_accessor
1 parent 30d0640 commit 494c4e3

File tree

12 files changed

+62
-85
lines changed

12 files changed

+62
-85
lines changed

rb/lib/selenium/webdriver/chromium/options.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ def process_browser_options(browser_options)
221221
options = browser_options[self.class::KEY]
222222
options['binary'] ||= binary_path if binary_path
223223

224-
check_w3c(options[:w3c]) if options.key?(:w3c)
225-
226224
if @profile
227225
options['args'] ||= []
228226
options['args'] << "--user-data-dir=#{@profile.directory}"
@@ -233,17 +231,6 @@ def process_browser_options(browser_options)
233231
options['extensions'] = @encoded_extensions + @extensions.map { |ext| encode_extension(ext) }
234232
end
235233

236-
def check_w3c(w3c)
237-
if w3c
238-
WebDriver.logger.warn("Setting 'w3c: true' is redundant and will no longer be allowed", id: :w3c)
239-
return
240-
end
241-
242-
raise Error::InvalidArgumentError,
243-
"Setting 'w3c: false' is not allowed.\n" \
244-
'Please update to W3C Syntax: https://www.selenium.dev/blog/2022/legacy-protocol-support/'
245-
end
246-
247234
def binary_path
248235
Chrome.path
249236
end

rb/lib/selenium/webdriver/common/options.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,27 @@ def as_json(*)
112112

113113
w3c_options = process_w3c_options(options)
114114

115-
self.class::CAPABILITIES.each do |capability_alias, capability_name|
116-
capability_value = options.delete(capability_alias)
117-
options[capability_name] = capability_value if !capability_value.nil? && !options.key?(capability_name)
115+
browser_options = self.class::CAPABILITIES.each_with_object({}) do |(capability_alias, capability_name), hash|
116+
from_name = options.delete(capability_name)
117+
from_alias = options.delete(capability_alias)
118+
capability_value = if !from_name.nil? && capability_alias != capability_name
119+
WebDriver.logger.deprecate("#{capability_name} as option",
120+
capability_alias.to_s, id: :option_symbols)
121+
from_name
122+
elsif !from_alias.nil?
123+
from_alias
124+
end
125+
126+
hash[capability_name] = capability_value unless capability_value.nil?
118127
end
119-
browser_options = defined?(self.class::KEY) ? {self.class::KEY => options} : options
128+
129+
unless options.empty?
130+
msg = 'These options are not w3c compliant and will result in failures in a future release'
131+
WebDriver.logger.warn("#{msg}: #{options}")
132+
browser_options.merge!(options)
133+
end
134+
135+
browser_options = {self.class::KEY => browser_options} if defined?(self.class::KEY)
120136

121137
process_browser_options(browser_options)
122138
generate_as_json(w3c_options.merge(browser_options))

rb/spec/unit/selenium/webdriver/chrome/driver_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def expect_request(body: nil, endpoint: nil)
4848
end
4949

5050
it 'accepts provided Options as sole parameter' do
51-
opts = {invalid: 'foobar', args: ['-f']}
51+
opts = {args: ['-f']}
5252
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', 'goog:chromeOptions': opts}}})
5353

5454
expect { described_class.new(options: Options.new(**opts)) }.not_to raise_exception

rb/spec/unit/selenium/webdriver/chrome/options_spec.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,29 +263,24 @@ module Chrome
263263
expect(options.as_json).to eq('browserName' => 'chrome', 'goog:chromeOptions' => {})
264264
end
265265

266-
it 'raises error when w3c is false' do
266+
it 'errors when unrecognized capability is passed' do
267267
expect {
268-
options.add_option(:w3c, false)
268+
options.add_option(:foo, 'bar')
269269
}.to have_deprecated(:add_option)
270-
expect { options.as_json }.to raise_error(Error::InvalidArgumentError)
271-
end
272270

273-
it 'raises error when w3c is true' do
274-
msg = /WARN Selenium \[:w3c\]/
275271
expect {
276-
options.add_option(:w3c, true)
277-
}.to have_deprecated(:add_option)
278-
expect { options.as_json }.to output(msg).to_stdout_from_any_process
272+
options.as_json
273+
}.to output(/WARN Selenium These options are not w3c compliant/).to_stdout_from_any_process
279274
end
280275

281276
it 'returns added options' do
282277
expect {
283-
options.add_option(:foo, 'bar')
278+
options.add_option(:detach, true)
284279
}.to have_deprecated(:add_option)
285280
options.add_option('foo:bar', {foo: 'bar'})
286281
expect(options.as_json).to eq('browserName' => 'chrome',
287282
'foo:bar' => {'foo' => 'bar'},
288-
'goog:chromeOptions' => {'foo' => 'bar'})
283+
'goog:chromeOptions' => {'detach' => true})
289284
end
290285

291286
it 'converts profile' do
@@ -322,7 +317,6 @@ module Chrome
322317
binary: '/foo/bar',
323318
extensions: ['foo.crx', 'bar.crx'],
324319
encoded_extensions: ['encoded_foobar'],
325-
foo: 'bar',
326320
emulation: {device_name: :mine},
327321
local_state: {
328322
foo: 'bar',
@@ -359,7 +353,6 @@ module Chrome
359353
'nested_one' => {'nested_two' => 'bazbar'}},
360354
'binary' => '/foo/bar',
361355
'extensions' => %w[encoded_foobar encoded_foo encoded_bar],
362-
'foo' => 'bar',
363356
'mobileEmulation' => {'deviceName' => 'mine'},
364357
'localState' => {
365358
'foo' => 'bar',

rb/spec/unit/selenium/webdriver/common/options_spec.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,16 @@
2222
module Selenium
2323
module WebDriver
2424
describe Options do
25-
subject(:options) { described_class.new(local_state: {}) }
25+
subject(:options) { described_class.new('localState' => {}) }
2626

2727
before do
2828
stub_const("#{described_class}::BROWSER", 'foo')
2929
stub_const("#{described_class}::CAPABILITIES", {local_state: 'localState'})
3030
end
3131

3232
describe '#as_json' do
33-
it 'does not override options set via symbol name' do
34-
expect {
35-
options.add_option(:local_state, {foo: 'bar'})
36-
}.to have_deprecated(:add_option)
37-
expect(options.as_json).to include('localState' => {'foo' => 'bar'})
38-
end
39-
40-
it 'does not override options set via string name' do
41-
expect {
42-
options.add_option('localState', {foo: 'bar'})
43-
}.to have_deprecated(:add_option)
44-
expect(options.as_json).to include('localState' => {'foo' => 'bar'})
33+
it 'Using camel case string value is deprecated' do
34+
expect { options.as_json }.to have_deprecated(:option_symbols)
4535
end
4636
end
4737
end # Options

rb/spec/unit/selenium/webdriver/edge/driver_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def expect_request(body: nil, endpoint: nil)
4848
end
4949

5050
it 'accepts provided Options as sole parameter' do
51-
opts = {invalid: 'foobar', args: ['-f']}
51+
opts = {args: ['-f']}
5252
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'MicrosoftEdge', 'ms:edgeOptions': opts}}})
5353

5454
expect { described_class.new(options: Options.new(**opts)) }.not_to raise_exception

rb/spec/unit/selenium/webdriver/edge/options_spec.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,29 +218,24 @@ module Edge
218218
expect(options.as_json).to eq('browserName' => 'MicrosoftEdge', 'ms:edgeOptions' => {})
219219
end
220220

221-
it 'raises error when w3c is false' do
221+
it 'errors when unrecognized capability is passed' do
222222
expect {
223-
options.add_option(:w3c, false)
223+
options.add_option(:foo, 'bar')
224224
}.to have_deprecated(:add_option)
225-
expect { options.as_json }.to raise_error(Error::InvalidArgumentError)
226-
end
227225

228-
it 'raises error when w3c is true' do
229-
msg = /WARN Selenium \[:w3c\]/
230226
expect {
231-
options.add_option(:w3c, true)
232-
}.to have_deprecated(:add_option)
233-
expect { options.as_json }.to output(msg).to_stdout_from_any_process
227+
options.as_json
228+
}.to output(/WARN Selenium These options are not w3c compliant/).to_stdout_from_any_process
234229
end
235230

236231
it 'returns added options' do
237232
expect {
238-
options.add_option(:foo, 'bar')
233+
options.add_option(:detach, true)
239234
}.to have_deprecated(:add_option)
240235
options.add_option('foo:bar', {foo: 'bar'})
241236
expect(options.as_json).to eq('browserName' => 'MicrosoftEdge',
242237
'foo:bar' => {'foo' => 'bar'},
243-
'ms:edgeOptions' => {'foo' => 'bar'})
238+
'ms:edgeOptions' => {'detach' => true})
244239
end
245240

246241
it 'returns a JSON hash' do
@@ -266,7 +261,6 @@ module Edge
266261
binary: '/foo/bar',
267262
extensions: ['foo.crx', 'bar.crx'],
268263
encoded_extensions: ['encoded_foobar'],
269-
foo: 'bar',
270264
emulation: {device_name: :mine},
271265
local_state: {foo: 'bar'},
272266
detach: true,
@@ -299,7 +293,6 @@ module Edge
299293
'key_that_should_not_be_camelcased' => 'baz'},
300294
'binary' => '/foo/bar',
301295
'extensions' => %w[encoded_foobar encoded_foo encoded_bar],
302-
'foo' => 'bar',
303296
'mobileEmulation' => {'deviceName' => 'mine'},
304297
'localState' => {'foo' => 'bar'},
305298
'detach' => true,

rb/spec/unit/selenium/webdriver/firefox/driver_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def expect_request(body: nil, endpoint: nil)
5151
end
5252

5353
it 'accepts provided Options as sole parameter' do
54-
opts = {invalid: 'foobar', args: ['-f']}
54+
opts = {args: ['-f']}
5555
expect_request(body: {capabilities: {alwaysMatch: {acceptInsecureCerts: true,
5656
browserName: 'firefox',
5757
'moz:firefoxOptions': opts,

rb/spec/unit/selenium/webdriver/firefox/options_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,14 @@ module Firefox
212212

213213
it 'returns added options' do
214214
expect {
215-
options.add_option(foo: 'bar')
215+
options.add_option(:args, %w[foo bar])
216216
}.to have_deprecated(:add_option)
217217
options.add_option('foo:bar', {foo: 'bar'})
218218
expect(options.as_json).to eq('acceptInsecureCerts' => true,
219219
'browserName' => 'firefox',
220220
'foo:bar' => {'foo' => 'bar'},
221221
'moz:debuggerAddress' => true,
222-
'moz:firefoxOptions' => {'foo' => 'bar'})
222+
'moz:firefoxOptions' => {'args' => %w[foo bar]})
223223
end
224224

225225
it 'converts to a json hash' do
@@ -240,7 +240,6 @@ module Firefox
240240
binary: '/foo/bar',
241241
prefs: {foo: 'bar'},
242242
env: {'FOO' => 'bar'},
243-
foo: 'bar',
244243
profile: profile,
245244
log_level: :debug,
246245
android_package: 'package',
@@ -269,7 +268,6 @@ module Firefox
269268
'env' => {'FOO' => 'bar'},
270269
'profile' => 'encoded_profile',
271270
'log' => {'level' => 'debug'},
272-
'foo' => 'bar',
273271
'androidPackage' => 'package',
274272
'androidActivity' => 'activity',
275273
'androidDeviceSerial' => '123',

rb/spec/unit/selenium/webdriver/ie/driver_spec.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ def expect_request(body: nil, endpoint: nil)
4949
end
5050

5151
it 'accepts provided Options as sole parameter' do
52-
opts = {invalid: 'foobar', args: ['-f']}
52+
opts = {args: ['-f']}
5353
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'internet explorer',
54-
'se:ieOptions': {invalid: 'foobar',
55-
nativeEvents: true,
54+
'se:ieOptions': {nativeEvents: true,
5655
'ie.browserCommandLineSwitches': '-f'}}}})
5756

5857
expect { described_class.new(options: Options.new(**opts)) }.not_to raise_exception
@@ -118,10 +117,10 @@ def as_json(*)
118117
end
119118

120119
it 'with Options instance' do
121-
browser_opts = {start_page: 'http://selenium.dev'}
122-
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'internet explorer',
123-
'se:ieOptions': {startPage: 'http://selenium.dev',
124-
nativeEvents: true}}}})
120+
browser_opts = {initial_browser_url: 'http://selenium.dev'}
121+
expect_request(body: {capabilities: {alwaysMatch: {'browserName' => 'internet explorer',
122+
'se:ieOptions' => {'initialBrowserUrl' => 'http://selenium.dev',
123+
'nativeEvents' => true}}}})
125124

126125
expect { described_class.new(capabilities: [Options.new(**browser_opts)]) }.not_to raise_exception
127126
end
@@ -143,12 +142,12 @@ def as_json(*)
143142

144143
it 'with Options instance, Capabilities instance and instance of a custom object responding to #as_json' do
145144
capabilities = Remote::Capabilities.new(browser_name: 'internet explorer', invalid: 'foobar')
146-
options = Options.new(start_page: 'http://selenium.dev')
147-
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'internet explorer',
148-
invalid: 'foobar',
149-
'se:ieOptions': {startPage: 'http://selenium.dev',
150-
nativeEvents: true},
151-
'company:key': 'value'}}})
145+
options = Options.new(initial_browser_url: 'http://selenium.dev')
146+
expect_request(body: {capabilities: {alwaysMatch: {'browserName' => 'internet explorer',
147+
'invalid' => 'foobar',
148+
'se:ieOptions' => {'initialBrowserUrl' => 'http://selenium.dev',
149+
'nativeEvents' => true},
150+
'company:key' => 'value'}}})
152151

153152
expect {
154153
described_class.new(capabilities: [capabilities, options, as_json_object.new])

0 commit comments

Comments
 (0)