Skip to content

Commit

Permalink
[rb] add support for all specified safari options in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jun 17, 2019
1 parent e3541d6 commit 2c05f6b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 0 additions & 2 deletions rb/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ Metrics/PerceivedComplexity:
Exclude:
- 'lib/selenium/webdriver/chrome/driver.rb'
- 'lib/selenium/webdriver/remote/capabilities.rb'
- 'lib/selenium/webdriver/chrome/options.rb'

Metrics/CyclomaticComplexity:
Max: 9
Exclude:
- 'lib/selenium/webdriver/chrome/driver.rb'
- 'lib/selenium/webdriver/remote/capabilities.rb'
- 'lib/selenium/webdriver/chrome/options.rb'
- 'spec/integration/selenium/webdriver/spec_support/test_environment.rb'

Metrics/ClassLength:
Expand Down
33 changes: 23 additions & 10 deletions rb/lib/selenium/webdriver/safari/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@
module Selenium
module WebDriver
module Safari
class Options
attr_accessor :automatic_inspection, :automatic_profiling
class Options < WebDriver::Common::Options
attr_accessor :options

# @see https://developer.apple.com/documentation/webkit/about_webdriver_for_safari
CAPABILITIES = {automatic_inspection: 'safari:automaticInspection',
automatic_profiling: 'safari:automaticProfiling'}.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 W3C-capable versions of Safari.
Expand All @@ -34,25 +48,24 @@ class Options
# @option opts [Boolean] :automatic_inspection Preloads Web Inspector and JavaScript debugger. Default is false
# @option opts [Boolean] :automatic_profiling Preloads Web Inspector and starts a timeline recording. Default is false
#
# @see https://developer.apple.com/documentation/webkit/about_webdriver_for_safari
#

def initialize(**opts)
@automatic_inspection = opts.delete(:automatic_inspection) || false
@automatic_profiling = opts.delete(:automatic_profiling) || false
@options = opts
end

#
# @api private
#

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

opts['safari:automaticInspection'] = true if @automatic_inspection
opts['safari:automaticProfiling'] = true if @automatic_profiling
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
end # Options
end # Safari
Expand Down
12 changes: 10 additions & 2 deletions rb/spec/unit/selenium/webdriver/safari/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ module Safari
describe '#as_json' do
it 'returns JSON hash' do
options = Options.new(automatic_inspection: true,
automatic_profiling: true)
automatic_profiling: false)

json = options.as_json
expect(json).to eq('safari:automaticInspection' => true,
'safari:automaticProfiling' => true)
'safari:automaticProfiling' => false)
end

it 'accepts a non-documented value' do
options = Options.new
options.options['safari:fooBar'] = true

json = options.as_json
expect(json).to eq('safari:fooBar' => true)
end
end
end # Options
Expand Down

0 comments on commit 2c05f6b

Please sign in to comment.