Skip to content

Commit

Permalink
[rb] add support for all specified edge options in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jun 17, 2019
1 parent 00a708f commit f97c519
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
46 changes: 32 additions & 14 deletions rb/lib/selenium/webdriver/edge_html/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@
module Selenium
module WebDriver
module EdgeHtml
class Options
attr_accessor :in_private, :start_page
attr_reader :extension_paths
class Options < WebDriver::Common::Options
# see https://docs.microsoft.com/en-us/microsoft-edge/webdriver#capabilities
CAPABILITIES = {in_private: 'ms:inPrivate',
extension_paths: 'ms:extensionPaths',
start_page: 'ms:startPage'}.freeze

CAPABILITIES.each_key do |key|
define_method key do
@options[key]
end

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

#
# Create a new Options instance for Edge.
Expand All @@ -40,9 +52,8 @@ class Options
#

def initialize(**opts)
@in_private = opts.delete(:in_private) || false
@extension_paths = opts.delete(:extension_paths) || []
@start_page = opts.delete(:start_page)
@options = opts
@options[:extensions]&.each(&method(:validate_extension))
end

#
Expand All @@ -56,23 +67,30 @@ def initialize(**opts)
#

def add_extension_path(path)
raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.directory?(path)

@extension_paths << path
validate_extension(path)
@options[:extension_paths] ||= []
@options[:extension_paths] << path
end

#
# @api private
#

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

opts['ms:inPrivate'] = true if @in_private
opts['ms:extensionPaths'] = @extension_paths if @extension_paths.any?
opts['ms:startPage'] = @start_page if @start_page
opts = CAPABILITIES.each_with_object({}) do |(capability_alias, capability_name), hash|
capability_value = options.delete(capability_alias)
hash[capability_name] = capability_value unless capability_value.nil?
end

opts
generate_as_json(opts.merge(options))
end

private

def validate_extension(path)
raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.directory?(path)
end
end # Options
end # Edge
Expand Down
20 changes: 18 additions & 2 deletions rb/spec/unit/selenium/webdriver/edge/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ module EdgeHtml
describe Options do
subject(:options) { described_class.new }

describe '#initialize' do
it 'accepts defined parameters' do
allow(File).to receive(:directory?).and_return(true)

options = Options.new(in_private: true,
extension_paths: ['/path1', '/path2'],
start_page: 'http://seleniumhq.org')

expect(options.in_private).to eq(true)
expect(options.extension_paths).to eq(['/path1', '/path2'])
expect(options.start_page).to eq('http://seleniumhq.org')
end
end

describe '#add_extension path' do
it 'adds extension path to the list' do
options.add_extension_path(__dir__)
Expand All @@ -38,13 +52,15 @@ module EdgeHtml

describe '#as_json' do
it 'returns JSON hash' do
allow(File).to receive(:directory?).and_return(true)

options = Options.new(in_private: true,
extension_paths: ['/path1', '/path2'],
start_page: 'http://seleniumhq.org')
options.add_extension_path(__dir__)

json = options.as_json
expect(json).to eq('ms:inPrivate' => true,
'ms:extensionPaths' => [__dir__],
'ms:extensionPaths' => ['/path1', '/path2'],
'ms:startPage' => 'http://seleniumhq.org')
end
end
Expand Down

0 comments on commit f97c519

Please sign in to comment.