Skip to content

Commit

Permalink
Do not camelCase Chrome prefs options
Browse files Browse the repository at this point in the history
Fixes #7917
  • Loading branch information
p0deje committed Apr 8, 2020
1 parent 73e7c89 commit aac9264
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
11 changes: 11 additions & 0 deletions rb/lib/selenium/webdriver/chrome/options.rb
Expand Up @@ -209,6 +209,17 @@ def validate_extension(path)
raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.file?(path)
raise Error::WebDriverError, "file was not an extension #{path.inspect}" unless File.extname(path) == '.crx'
end

def generate_as_json(value, camelize_keys: true)
if value.is_a?(Hash)
value.each_with_object({}) do |(key, val), hash|
key = convert_json_key(key, camelize: camelize_keys)
hash[key] = generate_as_json(val, camelize_keys: key != 'prefs')
end
else
super
end
end
end # Options
end # Chrome
end # WebDriver
Expand Down
15 changes: 9 additions & 6 deletions rb/lib/selenium/webdriver/common/options.rb
Expand Up @@ -84,29 +84,32 @@ def as_json(*)

private

def generate_as_json(value)
def generate_as_json(value, camelize_keys: true)
if value.respond_to?(:as_json)
value.as_json
elsif value.is_a?(Hash)
value.each_with_object({}) { |(key, val), hash| hash[convert_json_key(key)] = generate_as_json(val) }
value.each_with_object({}) do |(key, val), hash|
hash[convert_json_key(key, camelize: camelize_keys)] = generate_as_json(val, camelize_keys: camelize_keys)
end
elsif value.is_a?(Array)
value.map(&method(:generate_as_json))
value.map { |val| generate_as_json(val, camelize_keys: camelize_keys) }
elsif value.is_a?(Symbol)
value.to_s
else
value
end
end

def convert_json_key(key)
key = camel_case(key) if key.is_a?(Symbol)
def convert_json_key(key, camelize: true)
key = key.to_s if key.is_a?(Symbol)
key = camel_case(key) if camelize
return key if key.is_a?(String)

raise TypeError, "expected String or Symbol, got #{key.inspect}:#{key.class}"
end

def camel_case(str)
str.to_s.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
str.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
end
end # Options
end # WebDriver
Expand Down
6 changes: 4 additions & 2 deletions rb/spec/unit/selenium/webdriver/chrome/options_spec.rb
Expand Up @@ -189,7 +189,8 @@ module Chrome
implicit: 1},
set_window_rect: false,
args: %w[foo bar],
prefs: {foo: 'bar'},
prefs: {foo: 'bar',
key_that_should_not_be_camelcased: 'baz'},
binary: '/foo/bar',
extensions: ['foo.crx', 'bar.crx'],
encoded_extensions: ['encoded_foobar'],
Expand All @@ -216,7 +217,8 @@ module Chrome
'implicit' => 1},
'setWindowRect' => false,
key => {'args' => %w[foo bar],
'prefs' => {'foo' => 'bar'},
'prefs' => {'foo' => 'bar',
'key_that_should_not_be_camelcased' => 'baz'},
'binary' => '/foo/bar',
'extensions' => %w[encoded_foo encoded_bar encoded_foobar],
'foo' => 'bar',
Expand Down

0 comments on commit aac9264

Please sign in to comment.