Skip to content

Commit 1cdf89c

Browse files
committed
[rb] allow Options#add_option to accept a Hash as well as ordered pairs
1 parent a1c9131 commit 1cdf89c

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def initialize(options: nil, **opts)
9090
# @param [Boolean, String, Integer] value Value of the option
9191
#
9292

93-
def add_option(name, value)
93+
def add_option(name, value = nil)
94+
@options[name.keys.first] = name.values.first if value.nil? && name.is_a?(Hash)
9495
@options[name] = value
9596
end
9697

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ 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?(':')
31+
def add_option(name, value = nil)
32+
key = name.is_a?(Hash) ? name.keys.first : name
33+
raise ArgumentError, 'Safari does not support options that are not namespaced' unless key.to_s.include?(':')
3334

3435
super
3536
end

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,15 @@ module Chrome
177177
end
178178

179179
describe '#add_option' do
180-
it 'adds an option' do
180+
it 'adds an option with ordered pairs' do
181181
options.add_option(:foo, 'bar')
182182
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
183183
end
184+
185+
it 'adds an option with Hash' do
186+
options.add_option(foo: 'bar')
187+
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
188+
end
184189
end
185190

186191
describe '#add_preference' do

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,15 @@ module Edge
133133
end
134134

135135
describe '#add_option' do
136-
it 'adds an option' do
136+
it 'adds an option with ordered pairs' do
137137
options.add_option(:foo, 'bar')
138138
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
139139
end
140+
141+
it 'adds an option with Hash' do
142+
options.add_option(foo: 'bar')
143+
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
144+
end
140145
end
141146

142147
describe '#add_preference' do

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,15 @@ module Firefox
125125
end
126126

127127
describe '#add_option' do
128-
it 'adds an option' do
128+
it 'adds an option with ordered pairs' do
129129
options.add_option(:foo, 'bar')
130130
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
131131
end
132+
133+
it 'adds an option with Hash' do
134+
options.add_option(foo: 'bar')
135+
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
136+
end
132137
end
133138

134139
describe '#add_preference' do

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,14 @@ module IE
102102
end
103103

104104
describe '#add_option' do
105-
it 'adds an option' do
105+
it 'adds an option with ordered pairs' do
106106
options.add_option(:foo, 'bar')
107-
expect(options.options[:foo]).to eq('bar')
107+
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
108+
end
109+
110+
it 'adds an option with Hash' do
111+
options.add_option(foo: 'bar')
112+
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
108113
end
109114
end
110115

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,26 @@ module Safari
5959
end
6060

6161
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')
62+
context 'when namespaced' do
63+
it 'adds an option with ordered pairs' do
64+
options.add_option('safari:foo', 'bar')
65+
expect(options.instance_variable_get('@options')['safari:foo']).to eq('bar')
66+
end
67+
68+
it 'adds an option with Hash' do
69+
options.add_option('safari:foo': 'bar')
70+
expect(options.instance_variable_get('@options')[:'safari:foo']).to eq('bar')
71+
end
6572
end
6673

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)
74+
context 'when not namespaced' do
75+
it 'raises exception with ordered pairs' do
76+
expect { options.add_option('foo', 'bar') }.to raise_exception(ArgumentError)
77+
end
78+
79+
it 'raises exception with Hash' do
80+
expect { options.add_option(foo: 'bar') }.to raise_exception(ArgumentError)
81+
end
7182
end
7283
end
7384

0 commit comments

Comments
 (0)