Skip to content

Commit a1c9131

Browse files
committed
[rb] allow Options#add_option to set top level options if they use w3c compliant namespace
1 parent 47e47d9 commit a1c9131

File tree

7 files changed

+66
-19
lines changed

7 files changed

+66
-19
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,14 @@ def as_json(*)
123123

124124
private
125125

126+
def w3c?(key)
127+
W3C_OPTIONS.include?(key) || key.to_s.include?(':')
128+
end
129+
126130
def process_w3c_options(options)
127-
w3c_options = options.select { |key, _val| W3C_OPTIONS.include?(key) }
131+
w3c_options = options.select { |key, _val| w3c?(key) }
128132
w3c_options[:unhandled_prompt_behavior] &&= w3c_options[:unhandled_prompt_behavior]&.to_s&.tr('_', ' ')
129-
options.delete_if { |key, _val| W3C_OPTIONS.include?(key) }
133+
options.delete_if { |key, _val| w3c?(key) }
130134
w3c_options
131135
end
132136

@@ -173,6 +177,8 @@ def convert_json_key(key, camelize: true)
173177
def camel_case(str)
174178
str.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
175179
end
176-
end # Options
180+
end
181+
182+
# Options
177183
end # WebDriver
178184
end # Selenium

rb/lib/selenium/webdriver/safari/options.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class Options < WebDriver::Options
2828
automatic_profiling: 'safari:automaticProfiling'}.freeze
2929
BROWSER = 'safari'
3030

31+
def add_option(name, value)
32+
raise ArgumentError, 'Safari does not support options that are not namespaced' unless name.to_s.include?(':')
33+
34+
super
35+
end
36+
3137
end # Options
3238
end # Safari
3339
end # WebDriver

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,12 @@ module Chrome
212212
expect(options.as_json).to eq("browserName" => "chrome", "goog:chromeOptions" => {})
213213
end
214214

215-
it 'returns added option' do
215+
it 'returns added options' do
216216
options.add_option(:foo, 'bar')
217-
expect(options.as_json).to eq("browserName" => "chrome", "goog:chromeOptions" => {"foo" => "bar"})
217+
options.add_option('foo:bar', {foo: 'bar'})
218+
expect(options.as_json).to eq("browserName" => "chrome",
219+
"foo:bar" => {"foo" => "bar"},
220+
"goog:chromeOptions" => {"foo" => "bar"})
218221
end
219222

220223
it 'converts profile' do
@@ -260,7 +263,8 @@ module Chrome
260263
exclude_switches: %w[foobar barfoo],
261264
minidump_path: 'linux/only',
262265
perf_logging_prefs: {'enable_network': true},
263-
window_types: %w[normal devtools])
266+
window_types: %w[normal devtools],
267+
'custom:options': {foo: 'bar'})
264268

265269
key = 'goog:chromeOptions'
266270
expect(opts.as_json).to eq('browserName' => 'chrome',
@@ -274,6 +278,7 @@ module Chrome
274278
'pageLoad' => 400000,
275279
'implicit' => 1},
276280
'setWindowRect' => false,
281+
'custom:options' => {'foo' => 'bar'},
277282
key => {'args' => %w[foo bar],
278283
'prefs' => {'foo' => 'bar',
279284
'key_that_should_not_be_camelcased' => 'baz',

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,12 @@ module Edge
168168
expect(options.as_json).to eq("browserName" => "MicrosoftEdge", "ms:edgeOptions" => {})
169169
end
170170

171-
it 'returns added option' do
171+
it 'returns added options' do
172172
options.add_option(:foo, 'bar')
173-
expect(options.as_json).to eq("browserName" => "MicrosoftEdge", "ms:edgeOptions" => {"foo" => "bar"})
173+
options.add_option('foo:bar', {foo: 'bar'})
174+
expect(options.as_json).to eq("browserName" => "MicrosoftEdge",
175+
"foo:bar" => {"foo" => "bar"},
176+
"ms:edgeOptions" => {"foo" => "bar"})
174177
end
175178

176179
it 'returns a JSON hash' do
@@ -202,7 +205,8 @@ module Edge
202205
exclude_switches: %w[foobar barfoo],
203206
minidump_path: 'linux/only',
204207
perf_logging_prefs: {'enable_network': true},
205-
window_types: %w[normal devtools])
208+
window_types: %w[normal devtools],
209+
'custom:options': {foo: 'bar'})
206210

207211
key = 'ms:edgeOptions'
208212
expect(opts.as_json).to eq('browserName' => 'MicrosoftEdge',
@@ -216,6 +220,7 @@ module Edge
216220
'pageLoad' => 400000,
217221
'implicit' => 1},
218222
'setWindowRect' => false,
223+
'custom:options' => {'foo' => 'bar'},
219224
key => {'args' => %w[foo bar],
220225
'prefs' => {'foo' => 'bar',
221226
'key_that_should_not_be_camelcased' => 'baz'},

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ module Firefox
143143
expect(options.as_json).to eq("browserName" => "firefox", "moz:firefoxOptions" => {})
144144
end
145145

146-
it 'returns added option' do
146+
it 'returns added options' do
147147
options.add_option(:foo, 'bar')
148-
expect(options.as_json).to eq("browserName" => "firefox", "moz:firefoxOptions" => {"foo" => "bar"})
148+
options.add_option('foo:bar', {foo: 'bar'})
149+
expect(options.as_json).to eq("browserName" => "firefox",
150+
"foo:bar" => {"foo" => "bar"},
151+
"moz:firefoxOptions" => {"foo" => "bar"})
149152
end
150153

151154
it 'converts to a json hash' do
@@ -167,7 +170,8 @@ module Firefox
167170
prefs: {foo: 'bar'},
168171
foo: 'bar',
169172
profile: profile,
170-
log_level: :debug)
173+
log_level: :debug,
174+
'custom:options': {foo: 'bar'})
171175

172176
key = 'moz:firefoxOptions'
173177
expect(opts.as_json).to eq('browserName' => 'firefox',
@@ -181,6 +185,7 @@ module Firefox
181185
'pageLoad' => 400000,
182186
'implicit' => 1},
183187
'setWindowRect' => false,
188+
'custom:options' => {'foo' => 'bar'},
184189
key => {'args' => %w[foo bar],
185190
'binary' => '/foo/bar',
186191
'prefs' => {'foo' => 'bar'},

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ module IE
114114
"se:ieOptions" => {"nativeEvents" => true})
115115
end
116116

117-
it 'returns added option' do
117+
it 'returns added options' do
118118
options.add_option(:foo, 'bar')
119+
options.add_option('foo:bar', {foo: 'bar'})
119120
expect(options.as_json).to eq("browserName" => "internet explorer",
121+
"foo:bar" => {"foo" => "bar"},
120122
"se:ieOptions" => {"nativeEvents" => true, "foo" => "bar"})
121123
end
122124

@@ -148,7 +150,8 @@ module IE
148150
use_per_process_proxy: true,
149151
use_legacy_file_upload_dialog_handling: true,
150152
attach_to_edge_chrome: true,
151-
edge_executable_path: '/path/to/edge')
153+
edge_executable_path: '/path/to/edge',
154+
'custom:options': {foo: 'bar'})
152155

153156
key = 'se:ieOptions'
154157
expect(opts.as_json).to eq('browserName' => 'internet explorer',
@@ -162,6 +165,7 @@ module IE
162165
'pageLoad' => 400000,
163166
'implicit' => 1},
164167
'setWindowRect' => false,
168+
'custom:options' => {'foo' => 'bar'},
165169
key => {'ie.browserCommandLineSwitches' => 'foo bar',
166170
'browserAttachTimeout' => 30000,
167171
'elementScrollBehavior' => 1,

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,28 @@ module Safari
5858
end
5959
end
6060

61+
describe '#add_option' do
62+
it 'adds an option if name spaced' do
63+
options.add_option('safari:foo', 'bar')
64+
expect(options.instance_variable_get('@options')['safari:foo']).to eq('bar')
65+
end
66+
67+
# Note that this only applies to the method, weird things can still happen
68+
# if stuff is passed into the constructor
69+
it 'raises exception if not name spaced' do
70+
expect { options.add_option('foo', 'bar') }.to raise_exception(ArgumentError)
71+
end
72+
end
73+
6174
describe '#as_json' do
6275
it 'returns empty options by default' do
6376
expect(options.as_json).to eq("browserName" => "safari")
6477
end
6578

66-
it 'returns added option' do
67-
options.add_option(:foo, 'bar')
68-
expect(options.as_json).to eq("browserName" => "safari", "foo" => "bar")
79+
it 'returns added options' do
80+
options.add_option('safari:foo', 'bar')
81+
expect(options.as_json).to eq("browserName" => "safari",
82+
"safari:foo" => "bar")
6983
end
7084

7185
it 'returns JSON hash' do
@@ -80,7 +94,8 @@ module Safari
8094
implicit: 1},
8195
set_window_rect: false,
8296
automatic_profiling: false,
83-
automatic_inspection: true)
97+
automatic_inspection: true,
98+
'safari:foo': 'foo')
8499

85100
expect(opts.as_json).to eq('browserName' => 'safari',
86101
'browserVersion' => '12',
@@ -94,7 +109,8 @@ module Safari
94109
'implicit' => 1},
95110
'setWindowRect' => false,
96111
'safari:automaticInspection' => true,
97-
'safari:automaticProfiling' => false)
112+
'safari:automaticProfiling' => false,
113+
'safari:foo' => 'foo')
98114
end
99115
end
100116
end # Options

0 commit comments

Comments
 (0)