From 3490d4a5613cd426a94fe280513a2e0b7e935ae1 Mon Sep 17 00:00:00 2001 From: Titus Fortner Date: Thu, 20 Jun 2019 17:00:12 +0200 Subject: [PATCH] [rb] remove unnecessary methods from driver classes --- rb/lib/selenium/webdriver/chrome/driver.rb | 38 +------------------ rb/lib/selenium/webdriver/chrome/options.rb | 11 +++++- .../selenium/webdriver/edge_chrome/driver.rb | 8 ---- rb/lib/selenium/webdriver/edge_html/driver.rb | 13 +------ rb/lib/selenium/webdriver/firefox/driver.rb | 13 +------ rb/lib/selenium/webdriver/ie/driver.rb | 14 +------ rb/lib/selenium/webdriver/safari/driver.rb | 12 +----- .../webdriver/edge_chrome/profile_spec.rb | 4 +- 8 files changed, 17 insertions(+), 96 deletions(-) diff --git a/rb/lib/selenium/webdriver/chrome/driver.rb b/rb/lib/selenium/webdriver/chrome/driver.rb index 0425dd2dee0b6..b7d7326a2aaeb 100644 --- a/rb/lib/selenium/webdriver/chrome/driver.rb +++ b/rb/lib/selenium/webdriver/chrome/driver.rb @@ -34,7 +34,7 @@ class Driver < WebDriver::Driver include DriverExtensions::DownloadsFiles def initialize(opts = {}) - opts[:desired_capabilities] = create_capabilities(opts) + opts[:desired_capabilities] ||= Remote::Capabilities.send(browser) opts[:url] ||= service_url(opts) @@ -61,42 +61,6 @@ def quit def execute_cdp(cmd, **params) @bridge.send_command(cmd: cmd, params: params) end - - private - - def create_capabilities(opts) - caps = opts.delete(:desired_capabilities) { Remote::Capabilities.chrome } - options = opts.delete(:options) { Options.new } - - profile = opts.delete(:profile) - if profile - profile = profile.as_json - - options.args ||= [] - if options.args.none?(&/user-data-dir/.method(:match?)) - options.add_argument("--user-data-dir=#{profile['directory']}") - end - - if profile['extensions'] - WebDriver.logger.deprecate 'Using Selenium::WebDriver::Chrome::Profile#extensions', - 'Selenium::WebDriver::Chrome::Options#add_extension' - profile['extensions'].each do |extension| - options.add_encoded_extension(extension) - end - end - end - - detach = opts.delete(:detach) - options.add_option(:detach, true) if detach - - options = options.as_json - caps.merge!(options) unless options[Options::KEY].empty? - - caps[:proxy] = opts.delete(:proxy) if opts.key?(:proxy) - caps[:proxy] ||= opts.delete('proxy') if opts.key?('proxy') - - caps - end end # Driver end # Chrome end # WebDriver diff --git a/rb/lib/selenium/webdriver/chrome/options.rb b/rb/lib/selenium/webdriver/chrome/options.rb index a75b2615eff17..4dac1ffdb2b0e 100755 --- a/rb/lib/selenium/webdriver/chrome/options.rb +++ b/rb/lib/selenium/webdriver/chrome/options.rb @@ -21,6 +21,7 @@ module Selenium module WebDriver module Chrome class Options < WebDriver::Common::Options + attr_accessor :profile KEY = 'goog:chromeOptions' @@ -54,6 +55,8 @@ class Options < WebDriver::Common::Options # options = Selenium::WebDriver::Chrome::Options.new(args: ['start-maximized', 'user-data-dir=/tmp/temp_profile']) # driver = Selenium::WebDriver.for(:chrome, options: options) # + # @param [Profile] :profile An instance of a Firefox::Profile Class + # @param [Array] :encoded_extensions List of extensions that do not need to be Base64 encoded # @param [Hash] opts the pre-defined options to create the Chrome::Options with # @option opts [Array] :args List of command-line arguments to use when starting Chrome # @option opts [String] :binary Path to the Chrome executable to use @@ -70,9 +73,10 @@ class Options < WebDriver::Common::Options # @option opts [Array] :window_types A list of window types to appear in the list of window handles # - def initialize(encoded_extensions: nil, **opts) + def initialize(profile: nil, encoded_extensions: nil, **opts) super(opts) + @profile = profile @options[:encoded_extensions] = encoded_extensions if encoded_extensions @options[:extensions]&.each(&method(:validate_extension)) end @@ -182,6 +186,11 @@ def add_emulation(**opts) def as_json(*) options = super + if @profile + options['args'] ||= [] + options['args'] << "--user-data-dir=#{@profile[:directory]}" + end + options['binary'] ||= binary_path if binary_path extensions = options['extensions'] || [] encoded_extensions = options.delete(:encoded_extensions) || [] diff --git a/rb/lib/selenium/webdriver/edge_chrome/driver.rb b/rb/lib/selenium/webdriver/edge_chrome/driver.rb index 1c10662482336..03b7152c9a4bf 100644 --- a/rb/lib/selenium/webdriver/edge_chrome/driver.rb +++ b/rb/lib/selenium/webdriver/edge_chrome/driver.rb @@ -32,14 +32,6 @@ class Driver < Selenium::WebDriver::Chrome::Driver def browser :edge_chrome end - - private - - def create_capabilities(opts) - opts[:desired_capabilities] ||= Remote::Capabilities.edge_chrome - opts[:options] ||= Options.new - super(opts) - end end # Driver end # Chrome end # WebDriver diff --git a/rb/lib/selenium/webdriver/edge_html/driver.rb b/rb/lib/selenium/webdriver/edge_html/driver.rb index 0792508d16a2e..f33d032c0b657 100644 --- a/rb/lib/selenium/webdriver/edge_html/driver.rb +++ b/rb/lib/selenium/webdriver/edge_html/driver.rb @@ -31,7 +31,7 @@ class Driver < WebDriver::Driver include DriverExtensions::TakesScreenshot def initialize(opts = {}) - opts[:desired_capabilities] = create_capabilities(opts) + opts[:desired_capabilities] ||= Remote::Capabilities.edge opts[:url] ||= service_url(opts) @@ -53,17 +53,6 @@ def quit ensure @service&.stop end - - private - - def create_capabilities(opts) - caps = opts.delete(:desired_capabilities) { Remote::Capabilities.edge } - options = opts.delete(:options) { Options.new } - options = options.as_json - caps.merge!(options) unless options.empty? - - caps - end end # Driver end # Edge end # WebDriver diff --git a/rb/lib/selenium/webdriver/firefox/driver.rb b/rb/lib/selenium/webdriver/firefox/driver.rb index 14028fe915217..5051f9dec553d 100644 --- a/rb/lib/selenium/webdriver/firefox/driver.rb +++ b/rb/lib/selenium/webdriver/firefox/driver.rb @@ -32,7 +32,7 @@ class Driver < WebDriver::Driver include DriverExtensions::TakesScreenshot def initialize(opts = {}) - opts[:desired_capabilities] = create_capabilities(opts) + opts[:desired_capabilities] ||= Remote::Capabilities.firefox opts[:url] ||= service_url(opts) @@ -55,17 +55,6 @@ def quit ensure @service&.stop end - - private - - def create_capabilities(opts) - caps = opts.delete(:desired_capabilities) { Remote::Capabilities.firefox } - options = opts.delete(:options) { Options.new } - options = options.as_json - caps.merge!(options) unless options.empty? - - caps - end end # Driver end # Firefox end # WebDriver diff --git a/rb/lib/selenium/webdriver/ie/driver.rb b/rb/lib/selenium/webdriver/ie/driver.rb index 84441c7775813..d4e4b08b100cb 100644 --- a/rb/lib/selenium/webdriver/ie/driver.rb +++ b/rb/lib/selenium/webdriver/ie/driver.rb @@ -32,7 +32,7 @@ class Driver < WebDriver::Driver include DriverExtensions::TakesScreenshot def initialize(opts = {}) - opts[:desired_capabilities] = create_capabilities(opts) + opts[:desired_capabilities] ||= Remote::Capabilities.internet_explorer opts[:url] ||= service_url(opts) @@ -54,18 +54,6 @@ def quit ensure @service&.stop end - - private - - def create_capabilities(opts) - caps = opts.delete(:desired_capabilities) { Remote::Capabilities.internet_explorer } - options = opts.delete(:options) { Options.new } - options = options.as_json - caps.merge!(options) unless options.empty? - - caps - end - end # Driver end # IE end # WebDriver diff --git a/rb/lib/selenium/webdriver/safari/driver.rb b/rb/lib/selenium/webdriver/safari/driver.rb index 78215b12831c8..7977b50070034 100644 --- a/rb/lib/selenium/webdriver/safari/driver.rb +++ b/rb/lib/selenium/webdriver/safari/driver.rb @@ -32,7 +32,7 @@ class Driver < WebDriver::Driver include DriverExtensions::TakesScreenshot def initialize(opts = {}) - opts[:desired_capabilities] = create_capabilities(opts) + opts[:desired_capabilities] ||= Remote::Capabilities.safari opts[:url] ||= service_url(opts) @@ -55,16 +55,6 @@ def quit ensure @service&.stop end - - private - - def create_capabilities(opts = {}) - caps = opts.delete(:desired_capabilities) { Remote::Capabilities.safari } - options = opts.delete(:options) { Options.new } - caps.merge!(options.as_json) - caps - end - end # Driver end # Safari end # WebDriver diff --git a/rb/spec/integration/selenium/webdriver/edge_chrome/profile_spec.rb b/rb/spec/integration/selenium/webdriver/edge_chrome/profile_spec.rb index dc63d4f5f688b..f7a1a70095786 100755 --- a/rb/spec/integration/selenium/webdriver/edge_chrome/profile_spec.rb +++ b/rb/spec/integration/selenium/webdriver/edge_chrome/profile_spec.rb @@ -61,8 +61,8 @@ module EdgeChrome expect(profile).to receive(:layout_on_disk).and_return 'ignored' - expect(profile.as_json).to eq(directory: 'ignored', - extensions: [Base64.strict_encode64('test')]) + expect(profile.as_json).to eq('directory' => 'ignored', + 'extensions' => [Base64.strict_encode64('test')]) end it "raises an error if the extension doesn't exist" do