Skip to content

Commit

Permalink
[rb] add support for all specified firefox options in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jun 17, 2019
1 parent f97c519 commit e3541d6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 70 deletions.
72 changes: 42 additions & 30 deletions rb/lib/selenium/webdriver/firefox/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ module Selenium
module WebDriver
module Firefox
class Options < WebDriver::Common::Options
attr_reader :args, :prefs, :options, :profile
attr_accessor :binary, :log_level

KEY = 'moz:firefoxOptions'

# see: https://firefox-source-docs.mozilla.org/testing/geckodriver/Capabilities.html
CAPABILITIES = %i[binary args profile log prefs].freeze

(CAPABILITIES + %i[log_level]).each do |key|
define_method key do
@options[key]
end

define_method "#{key}=" do |value|
@options[key] = value
end
end

#
# Create a new Options instance, only for W3C-capable versions of Firefox.
#
Expand All @@ -42,13 +52,15 @@ class Options < WebDriver::Common::Options
# @option opts [Hash] :options A hash for raw options
#

def initialize(**opts)
@args = Set.new(opts.delete(:args) || [])
@binary = opts.delete(:binary)
@profile = process_profile(opts.delete(:profile))
@log_level = opts.delete(:log_level)
@prefs = opts.delete(:prefs) || {}
@options = opts.delete(:options) || {}
def initialize(options: nil, **opts)
@options = if options
WebDriver.logger.deprecate(":options as keyword for initializing #{self.class}",
"values directly in #new constructor")
opts.merge(options)
else
opts
end
process_profile(@options[:profile]) if @options.key?(:profile)
end

#
Expand All @@ -62,7 +74,8 @@ def initialize(**opts)
#

def add_argument(arg)
@args << arg
@options[:args] ||= []
@options[:args] << arg
end

#
Expand Down Expand Up @@ -92,7 +105,8 @@ def add_option(name, value)
#

def add_preference(name, value)
prefs[name] = value
@options[:prefs] ||= {}
@options[:prefs][name] = value
end

#
Expand Down Expand Up @@ -123,38 +137,36 @@ def headless!
#

def profile=(profile)
@profile = process_profile(profile)
process_profile(profile)
end

#
# @api private
#

def as_json(*)
opts = @options
options = @options.dup

opts[:profile] = @profile.encoded if @profile
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
opts = CAPABILITIES.each_with_object({}) do |capability_name, hash|
capability_value = options.delete(capability_name)
hash[capability_name] = capability_value unless capability_value.nil?
end

{KEY => generate_as_json(opts)}
opts[:log] ||= {level: options.delete(:log_level)} if options.key?(:log_level)

{KEY => generate_as_json(opts.merge(options))}
end

private

def process_profile(profile)
return unless profile

case profile
when Profile
profile
when String
Profile.from_name(profile)
else
raise Error::WebDriverError, "don't know how to handle profile: #{profile.inspect}"
end
@options[:profile] = if profile.nil?
nil
elsif profile.is_a? Profile
profile
else
Profile.from_name(profile)
end
end
end # Options
end # Firefox
Expand Down
4 changes: 4 additions & 0 deletions rb/lib/selenium/webdriver/firefox/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ def encoded
Zipper.zip(layout_on_disk)
end

def as_json
encoded
end

private

def assign_default_preferences
Expand Down
65 changes: 25 additions & 40 deletions rb/spec/unit/selenium/webdriver/firefox/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,23 @@ module Firefox
subject(:options) { described_class.new }

describe '#initialize' do
it 'sets passed args' do
opt = Options.new(args: %w[foo bar])
expect(opt.args.to_a).to eq(%w[foo bar])
end

it 'sets passed prefs' do
opt = Options.new(prefs: {foo: 'bar'})
expect(opt.prefs[:foo]).to eq('bar')
end

it 'sets passed binary value' do
opt = Options.new(binary: '/foo/bar')
expect(opt.binary).to eq('/foo/bar')
end

it 'sets passed new profile' do
profile = Profile.new
opt = Options.new(profile: profile)
expect(opt.profile).to eq(profile)
end

it 'sets passed existing profile' do
it 'sets provided parameters' do
profile = Profile.new
expect(Profile).to receive(:from_name).with('foo').and_return(profile)
opt = Options.new(profile: 'foo')
expect(opt.profile).to eq(profile)
end
allow(profile).to receive(:encoded).and_return('encoded_profile')

it 'sets passed log level' do
opt = Options.new(log_level: 'debug')
expect(opt.log_level).to eq('debug')
end
options = described_class.new(args: %w[foo bar],
binary: '/foo/bar',
prefs: {foo: 'bar'},
foo: 'bar',
profile: profile,
log_level: :debug)

it 'sets passed options' do
opt = Options.new(options: {foo: 'bar'})
expect(opt.options[:foo]).to eq('bar')
expect(options.args.to_a).to eq(%w[foo bar])
expect(options.binary).to eq('/foo/bar')
expect(options.prefs[:foo]).to eq('bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
expect(options.profile).to eq(profile)
expect(options.log_level).to eq(:debug)
end
end

Expand All @@ -82,12 +63,16 @@ module Firefox
describe '#profile=' do
it 'sets a new profile' do
profile = Profile.new
allow(profile).to receive(:encoded).and_return('encoded_profile')

options.profile = profile
expect(options.profile).to eq(profile)
end

it 'sets an existing profile' do
profile = Profile.new
allow(profile).to receive(:encoded).and_return('encoded_profile')

expect(Profile).to receive(:from_name).with('foo').and_return(profile)
options.profile = 'foo'
expect(options.profile).to eq(profile)
Expand All @@ -111,7 +96,7 @@ module Firefox
describe '#add_option' do
it 'adds an option' do
options.add_option(:foo, 'bar')
expect(options.options[:foo]).to eq('bar')
expect(options.instance_variable_get('@options')[:foo]).to eq('bar')
end
end

Expand All @@ -125,20 +110,20 @@ module Firefox
describe '#as_json' do
it 'converts to a json hash' do
profile = Profile.new
expect(profile).to receive(:encoded).and_return('foo')
expect(profile).to receive(:encoded).and_return('encoded_profile')

options = Options.new(args: ['foo'],
options = Options.new(args: %w[foo bar],
binary: '/foo/bar',
prefs: {a: 1},
prefs: {foo: 'bar'},
options: {foo: :bar},
profile: profile,
log_level: :debug)

json = options.as_json['moz:firefoxOptions']
expect(json).to eq('args' => ['foo'],
expect(json).to eq('args' => %w[foo bar],
'binary' => '/foo/bar',
'prefs' => {"a" => 1},
'profile' => 'foo',
'prefs' => {'foo' => 'bar'},
'profile' => 'encoded_profile',
'log' => {'level' => 'debug'},
'foo' => 'bar')
end
Expand Down

0 comments on commit e3541d6

Please sign in to comment.