Skip to content

Commit

Permalink
Use Set for cli args in browser options to guarantee uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Feb 13, 2018
1 parent 5c7d155 commit 2300e36
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 35 deletions.
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require 'fileutils'
require 'date'
require 'json'
require 'set'

require 'selenium/webdriver/common'
require 'selenium/webdriver/atoms'
Expand Down
6 changes: 2 additions & 4 deletions rb/lib/selenium/webdriver/chrome/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Options
#

def initialize(**opts)
@args = opts.delete(:args) || []
@args = Set.new(opts.delete(:args) || [])
@binary = opts.delete(:binary) || Chrome.path
@prefs = opts.delete(:prefs) || {}
@extensions = opts.delete(:extensions) || []
Expand Down Expand Up @@ -89,8 +89,6 @@ def add_encoded_extension(encoded)
#

def add_argument(arg)
return if @args.include?(arg)

@args << arg
end

Expand Down Expand Up @@ -173,7 +171,7 @@ def as_json(*)

opts = @options
opts[:binary] = @binary if @binary
opts[:args] = @args if @args.any?
opts[:args] = @args.to_a if @args.any?
opts[:extensions] = extensions if extensions.any?
opts[:mobileEmulation] = @emulation unless @emulation.empty?
opts[:prefs] = @prefs unless @prefs.empty?
Expand Down
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/firefox/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Options
#

def initialize(**opts)
@args = opts.delete(:args) || []
@args = Set.new(opts.delete(:args) || [])
@binary = opts.delete(:binary)
@profile = opts.delete(:profile)
@log_level = opts.delete(:log_level)
Expand Down Expand Up @@ -136,7 +136,7 @@ def as_json(*)
opts = @options

opts[:profile] = @profile.encoded if @profile
opts[:args] = @args if @args.any?
opts[:args] = @args.to_a if @args.any?
opts[:binary] = @binary if @binary
opts[:prefs] = @prefs unless @prefs.empty?
opts[:log] = {level: @log_level} if @log_level
Expand Down
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/ie/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Options
#

def initialize(**opts)
@args = opts.delete(:args) || []
@args = Set.new(opts.delete(:args) || [])
@options = opts
@options[:native_events] ||= true
end
Expand Down Expand Up @@ -125,7 +125,7 @@ def as_json(*)
capability_value = @options.delete(capability_alias)
opts[capability_name] = capability_value unless capability_value.nil?
end
opts['ie.browserCommandLineSwitches'] = @args.join(' ') if @args.any?
opts['ie.browserCommandLineSwitches'] = @args.to_a.join(' ') if @args.any?
opts.merge!(@options)

{KEY => opts}
Expand Down
27 changes: 5 additions & 22 deletions rb/spec/unit/selenium/webdriver/chrome/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Chrome
describe '#initialize' do
it 'sets passed args' do
opt = Options.new(args: %w[foo bar])
expect(opt.args).to eq(%w[foo bar])
expect(opt.args.to_a).to eq(%w[foo bar])
end

it 'sets passed prefs' do
Expand Down Expand Up @@ -91,36 +91,19 @@ module Chrome
describe '#add_argument' do
it 'adds a command-line argument' do
subject.add_argument('foo')
expect(subject.args).to include('foo')
end

context 'multiple arguments' do
it 'should add only unique arguments' do
subject.add_argument('foo')
subject.add_argument('bar')

expect(subject.args).to eq(%w[foo bar])
end

it 'should not add the same argument more than once' do
subject.add_argument('foo')
subject.add_argument('foo')

expect(subject.args).to eq(['foo'])
end
expect(subject.args.to_a).to eq(['foo'])
end
end

describe '#headless!' do
it 'should add necessary command-line arguments' do
subject.headless!
if WebDriver::Platform.windows?
expect(subject.args).to eql(%w[--headless --disable-gpu])
expect(subject.args.to_a).to eql(%w[--headless --disable-gpu])
else
expect(subject.args).to eql(['--headless'])
expect(subject.args.to_a).to eql(['--headless'])
end
end

end

describe '#add_option' do
Expand Down Expand Up @@ -173,7 +156,7 @@ module Chrome
options: {foo: :bar},
emulation: {c: 3})
json = opts.as_json
expect(json[:args]).to include('foo')
expect(json[:args]).to eq(['foo'])
expect(json[:binary]).to eq('/foo/bar')
expect(json[:prefs]).to include(a: 1)
expect(json[:extensions]).to include('bar')
Expand Down
6 changes: 3 additions & 3 deletions rb/spec/unit/selenium/webdriver/firefox/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Firefox
describe '#initialize' do
it 'sets passed args' do
opt = Options.new(args: %w[foo bar])
expect(opt.args).to eq(%w[foo bar])
expect(opt.args.to_a).to eq(%w[foo bar])
end

it 'sets passed prefs' do
Expand Down Expand Up @@ -85,7 +85,7 @@ module Firefox
describe '#add_argument' do
it 'adds a command-line argument' do
subject.add_argument('foo')
expect(subject.args).to include('foo')
expect(subject.args.to_a).to eq(['foo'])
end
end

Expand Down Expand Up @@ -116,7 +116,7 @@ module Firefox
log_level: :debug)
json = opts.as_json

expect(json['moz:firefoxOptions'][:args]).to include('foo')
expect(json['moz:firefoxOptions'][:args]).to eq(['foo'])
expect(json['moz:firefoxOptions'][:binary]).to eq('/foo/bar')
expect(json['moz:firefoxOptions'][:prefs]).to include(a: 1)
expect(json['moz:firefoxOptions'][:foo]).to eq(:bar)
Expand Down
4 changes: 2 additions & 2 deletions rb/spec/unit/selenium/webdriver/ie/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module IE
describe '#initialize' do
it 'sets passed args' do
opt = Options.new(args: %w[foo bar])
expect(opt.args).to eq(%w[foo bar])
expect(opt.args.to_a).to eq(%w[foo bar])
end

it 'sets passed browser_attach_timeout' do
Expand Down Expand Up @@ -107,7 +107,7 @@ module IE
describe '#add_argument' do
it 'adds a command-line argument' do
subject.add_argument('foo')
expect(subject.args).to include('foo')
expect(subject.args.to_a).to eq(['foo'])
end
end

Expand Down

0 comments on commit 2300e36

Please sign in to comment.