Skip to content

Commit

Permalink
[rb] allow Options#add_option to accept a Hash as well as ordered pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 15, 2021
1 parent a1c9131 commit 1cdf89c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
3 changes: 2 additions & 1 deletion rb/lib/selenium/webdriver/common/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def initialize(options: nil, **opts)
# @param [Boolean, String, Integer] value Value of the option
#

def add_option(name, value)
def add_option(name, value = nil)
@options[name.keys.first] = name.values.first if value.nil? && name.is_a?(Hash)
@options[name] = value
end

Expand Down
5 changes: 3 additions & 2 deletions rb/lib/selenium/webdriver/safari/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class Options < WebDriver::Options
automatic_profiling: 'safari:automaticProfiling'}.freeze
BROWSER = 'safari'

def add_option(name, value)
raise ArgumentError, 'Safari does not support options that are not namespaced' unless name.to_s.include?(':')
def add_option(name, value = nil)
key = name.is_a?(Hash) ? name.keys.first : name
raise ArgumentError, 'Safari does not support options that are not namespaced' unless key.to_s.include?(':')

super
end
Expand Down
7 changes: 6 additions & 1 deletion rb/spec/unit/selenium/webdriver/chrome/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,15 @@ module Chrome
end

describe '#add_option' do
it 'adds an option' do
it 'adds an option with ordered pairs' do
options.add_option(:foo, 'bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end

it 'adds an option with Hash' do
options.add_option(foo: 'bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end
end

describe '#add_preference' do
Expand Down
7 changes: 6 additions & 1 deletion rb/spec/unit/selenium/webdriver/edge/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,15 @@ module Edge
end

describe '#add_option' do
it 'adds an option' do
it 'adds an option with ordered pairs' do
options.add_option(:foo, 'bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end

it 'adds an option with Hash' do
options.add_option(foo: 'bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end
end

describe '#add_preference' do
Expand Down
7 changes: 6 additions & 1 deletion rb/spec/unit/selenium/webdriver/firefox/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ module Firefox
end

describe '#add_option' do
it 'adds an option' do
it 'adds an option with ordered pairs' do
options.add_option(:foo, 'bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end

it 'adds an option with Hash' do
options.add_option(foo: 'bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end
end

describe '#add_preference' do
Expand Down
9 changes: 7 additions & 2 deletions rb/spec/unit/selenium/webdriver/ie/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,14 @@ module IE
end

describe '#add_option' do
it 'adds an option' do
it 'adds an option with ordered pairs' do
options.add_option(:foo, 'bar')
expect(options.options[:foo]).to eq('bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end

it 'adds an option with Hash' do
options.add_option(foo: 'bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end
end

Expand Down
25 changes: 18 additions & 7 deletions rb/spec/unit/selenium/webdriver/safari/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,26 @@ module Safari
end

describe '#add_option' do
it 'adds an option if name spaced' do
options.add_option('safari:foo', 'bar')
expect(options.instance_variable_get('@options')['safari:foo']).to eq('bar')
context 'when namespaced' do
it 'adds an option with ordered pairs' do
options.add_option('safari:foo', 'bar')
expect(options.instance_variable_get('@options')['safari:foo']).to eq('bar')
end

it 'adds an option with Hash' do
options.add_option('safari:foo': 'bar')
expect(options.instance_variable_get('@options')[:'safari:foo']).to eq('bar')
end
end

# Note that this only applies to the method, weird things can still happen
# if stuff is passed into the constructor
it 'raises exception if not name spaced' do
expect { options.add_option('foo', 'bar') }.to raise_exception(ArgumentError)
context 'when not namespaced' do
it 'raises exception with ordered pairs' do
expect { options.add_option('foo', 'bar') }.to raise_exception(ArgumentError)
end

it 'raises exception with Hash' do
expect { options.add_option(foo: 'bar') }.to raise_exception(ArgumentError)
end
end
end

Expand Down

0 comments on commit 1cdf89c

Please sign in to comment.