From 0ba8188b1a26ff3587f08afa6b6182c32479e980 Mon Sep 17 00:00:00 2001 From: Alex Rodionov Date: Fri, 18 May 2018 10:13:04 +0300 Subject: [PATCH] Use goog:chromeOptions when sending Chrome options capabilities It has been introduced in ChromeDriver 2.31 and is compliant to W3C spec. Please note this this commit requires you to have ChromeDriver 2.31 at least. --- rb/lib/selenium/webdriver/chrome/driver.rb | 2 +- rb/lib/selenium/webdriver/chrome/options.rb | 5 ++++- .../selenium/webdriver/chrome/driver_spec.rb | 18 +++++++++--------- .../selenium/webdriver/chrome/options_spec.rb | 12 ++++++------ 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/rb/lib/selenium/webdriver/chrome/driver.rb b/rb/lib/selenium/webdriver/chrome/driver.rb index 4a8a9868285d7..33254c299f992 100644 --- a/rb/lib/selenium/webdriver/chrome/driver.rb +++ b/rb/lib/selenium/webdriver/chrome/driver.rb @@ -102,7 +102,7 @@ def create_capabilities(opts) end options = options.as_json - caps[:chrome_options] = options unless options.empty? + caps.merge!(options) unless options.empty? caps[:proxy] = opts.delete(:proxy) if opts.key?(:proxy) caps[:proxy] ||= opts.delete('proxy') if opts.key?('proxy') diff --git a/rb/lib/selenium/webdriver/chrome/options.rb b/rb/lib/selenium/webdriver/chrome/options.rb index 661b91582df5d..eb062742bd811 100755 --- a/rb/lib/selenium/webdriver/chrome/options.rb +++ b/rb/lib/selenium/webdriver/chrome/options.rb @@ -22,6 +22,8 @@ class Options attr_reader :args, :prefs, :options, :emulation, :extensions, :encoded_extensions attr_accessor :binary + KEY = 'goog:chromeOptions'.freeze + # # Create a new Options instance. # @@ -175,7 +177,8 @@ def as_json(*) opts[:extensions] = extensions if extensions.any? opts[:mobileEmulation] = @emulation unless @emulation.empty? opts[:prefs] = @prefs unless @prefs.empty? - opts + + {KEY => opts} end end # Options end # Chrome diff --git a/rb/spec/unit/selenium/webdriver/chrome/driver_spec.rb b/rb/spec/unit/selenium/webdriver/chrome/driver_spec.rb index a2a27d1ab8bf5..0196e4cf9973f 100644 --- a/rb/spec/unit/selenium/webdriver/chrome/driver_spec.rb +++ b/rb/spec/unit/selenium/webdriver/chrome/driver_spec.rb @@ -35,13 +35,13 @@ module Chrome it 'sets the args capability' do Driver.new(http_client: http, args: %w[--foo=bar]) - expect(caps[:chrome_options][:args]).to eq(%w[--foo=bar]) + expect(caps['goog:chromeOptions'][:args]).to eq(%w[--foo=bar]) end it 'sets the args capability from switches' do Driver.new(http_client: http, switches: %w[--foo=bar]) - expect(caps[:chrome_options][:args]).to eq(%w[--foo=bar]) + expect(caps['goog:chromeOptions'][:args]).to eq(%w[--foo=bar]) end it 'sets the proxy capabilitiy' do @@ -54,20 +54,20 @@ module Chrome it 'does not set the chrome.detach capability by default' do Driver.new(http_client: http) - expect(caps[:chrome_options]).to be nil + expect(caps['goog:chromeOptions']).to eq({}) expect(caps['chrome.detach']).to be nil end it 'sets the prefs capability' do Driver.new(http_client: http, prefs: {foo: 'bar'}) - expect(caps[:chrome_options][:prefs]).to eq(foo: 'bar') + expect(caps['goog:chromeOptions'][:prefs]).to eq(foo: 'bar') end it 'lets the user override chrome.detach' do Driver.new(http_client: http, detach: true) - expect(caps[:chrome_options][:detach]).to be true + expect(caps['goog:chromeOptions'][:detach]).to be true end it 'raises an ArgumentError if args is not an Array' do @@ -83,8 +83,8 @@ module Chrome Driver.new(http_client: http, profile: profile) profile_data = profile.as_json - expect(caps[:chrome_options][:args].first).to include(profile_data[:directory]) - expect(caps[:chrome_options][:extensions]).to eq(profile_data[:extensions]) + expect(caps['goog:chromeOptions'][:args].first).to include(profile_data[:directory]) + expect(caps['goog:chromeOptions'][:extensions]).to eq(profile_data[:extensions]) end it 'takes desired capabilities' do @@ -101,10 +101,10 @@ module Chrome it 'lets direct arguments take presedence over capabilities' do custom_caps = Remote::Capabilities.new - custom_caps[:chrome_options] = {'args' => %w[foo bar]} + custom_caps['goog:chromeOptions'] = {'args' => %w[foo bar]} expect(http).to receive(:call) do |_, _, payload| - expect(payload[:desiredCapabilities][:chrome_options][:args]).to eq(['baz']) + expect(payload[:desiredCapabilities]['goog:chromeOptions'][:args]).to eq(['baz']) resp end diff --git a/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb b/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb index bb3596404f60b..4042dda454071 100644 --- a/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb +++ b/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb @@ -156,12 +156,12 @@ module Chrome options: {foo: :bar}, emulation: {c: 3}) json = opts.as_json - 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') - expect(json[:foo]).to eq(:bar) - expect(json[:mobileEmulation]).to include(c: 3) + expect(json['goog:chromeOptions'][:args]).to eq(['foo']) + expect(json['goog:chromeOptions'][:binary]).to eq('/foo/bar') + expect(json['goog:chromeOptions'][:prefs]).to include(a: 1) + expect(json['goog:chromeOptions'][:extensions]).to include('bar') + expect(json['goog:chromeOptions'][:foo]).to eq(:bar) + expect(json['goog:chromeOptions'][:mobileEmulation]).to include(c: 3) end end end # Options