From 2fd646c5d788b0f1002b969649d8e2fc24cebd03 Mon Sep 17 00:00:00 2001 From: Titus Fortner Date: Mon, 8 Jul 2019 14:30:16 -0700 Subject: [PATCH] [rb] remove Firefox::Binary class --- rb/lib/selenium/webdriver/common/service.rb | 13 --- rb/lib/selenium/webdriver/firefox.rb | 8 +- rb/lib/selenium/webdriver/firefox/binary.rb | 110 ------------------ rb/lib/selenium/webdriver/firefox/options.rb | 5 +- .../selenium/webdriver/firefox/binary_spec.rb | 38 ------ .../webdriver/firefox/service_spec.rb | 14 +-- 6 files changed, 16 insertions(+), 172 deletions(-) delete mode 100644 rb/lib/selenium/webdriver/firefox/binary.rb delete mode 100644 rb/spec/unit/selenium/webdriver/firefox/binary_spec.rb diff --git a/rb/lib/selenium/webdriver/common/service.rb b/rb/lib/selenium/webdriver/common/service.rb index 3c81bb9132542..6fad5a8f0e1fa 100644 --- a/rb/lib/selenium/webdriver/common/service.rb +++ b/rb/lib/selenium/webdriver/common/service.rb @@ -37,19 +37,6 @@ def chrome(**opts) end def firefox(**opts) - binary_path = Firefox::Binary.path - args = opts.delete(:args) - case args - when Hash - args[:binary] ||= binary_path - opts[:args] = args - when Array - opts[:args] = ["--binary=#{binary_path}"] - opts[:args] += args - else - opts[:args] = ["--binary=#{binary_path}"] - end - Firefox::Service.new(**opts) end diff --git a/rb/lib/selenium/webdriver/firefox.rb b/rb/lib/selenium/webdriver/firefox.rb index 02bba43c3d4b7..35c7212ba5ce5 100644 --- a/rb/lib/selenium/webdriver/firefox.rb +++ b/rb/lib/selenium/webdriver/firefox.rb @@ -25,7 +25,6 @@ module Selenium module WebDriver module Firefox autoload :Extension, 'selenium/webdriver/firefox/extension' - autoload :Binary, 'selenium/webdriver/firefox/binary' autoload :ProfilesIni, 'selenium/webdriver/firefox/profiles_ini' autoload :Profile, 'selenium/webdriver/firefox/profile' autoload :Bridge, 'selenium/webdriver/firefox/bridge' @@ -52,7 +51,12 @@ def self.driver_path end def self.path=(path) - Binary.path = path + Platform.assert_executable path + @path = path + end + + def self.path + @path ||= nil end end # Firefox end # WebDriver diff --git a/rb/lib/selenium/webdriver/firefox/binary.rb b/rb/lib/selenium/webdriver/firefox/binary.rb deleted file mode 100644 index 1cffacc627a5a..0000000000000 --- a/rb/lib/selenium/webdriver/firefox/binary.rb +++ /dev/null @@ -1,110 +0,0 @@ -# frozen_string_literal: true - -# Licensed to the Software Freedom Conservancy (SFC) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The SFC licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -module Selenium - module WebDriver - module Firefox - # @api private - class Binary - class << self - # - # @api private - # - # @see Firefox.path= - # - - def path=(path) - Platform.assert_executable(path) - @path = path - end - - def reset_path! - @path = nil - end - - def path - @path ||= case Platform.os - when :macosx - macosx_path - when :windows - windows_path - when :linux, :unix - Platform.find_binary('firefox3', 'firefox2', 'firefox') || '/usr/bin/firefox' - else - raise Error::WebDriverError, "unknown platform: #{Platform.os}" - end - - @path = Platform.cygwin_path(@path, windows: true) if Platform.cygwin? - - unless File.file?(@path.to_s) - error = "Could not find Firefox binary (os=#{Platform.os}). " \ - "Make sure Firefox is installed or set the path manually with #{self}.path=" - raise Error::WebDriverError, error - end - - @path - end - - def version - @version = case Platform.os - when :macosx - `#{path} -v`.strip[/[^\s]*$/][/^\d+/].to_i - when :windows - `\"#{path}\" -v | more`.strip[/[^\s]*$/][/^\d+/].to_i - when :linux - `#{path} -v`.strip[/[^\s]*$/][/^\d+/].to_i - else - 0 - end - end - - private - - def windows_path - windows_registry_path || - Platform.find_in_program_files('\\Mozilla Firefox\\firefox.exe') || - Platform.find_binary('firefox') - end - - def macosx_path - path = '/Applications/Firefox.app/Contents/MacOS/firefox-bin' - path = File.expand_path('~/Applications/Firefox.app/Contents/MacOS/firefox-bin') unless File.exist?(path) - path = Platform.find_binary('firefox-bin') unless File.exist?(path) - - path - end - - def windows_registry_path - require 'win32/registry' - - lm = Win32::Registry::HKEY_LOCAL_MACHINE - lm.open('SOFTWARE\\Mozilla\\Mozilla Firefox') do |reg| - main = lm.open("SOFTWARE\\Mozilla\\Mozilla Firefox\\#{reg.keys[0]}\\Main") - entry = main.find { |key, _type, _data| /pathtoexe/i.match? key } - return entry.last if entry - end - rescue LoadError - # older JRuby or IronRuby does not have win32/registry - rescue Win32::Registry::Error - end - end # class << self - end # Binary - end # Firefox - end # WebDriver -end # Selenium diff --git a/rb/lib/selenium/webdriver/firefox/options.rb b/rb/lib/selenium/webdriver/firefox/options.rb index ebd9116cb97dc..a0bca32ec557f 100644 --- a/rb/lib/selenium/webdriver/firefox/options.rb +++ b/rb/lib/selenium/webdriver/firefox/options.rb @@ -139,7 +139,10 @@ def log_level=(level) # def as_json(*) - {KEY => generate_as_json(super)} + options = super + options['binary'] ||= Firefox.path if Firefox.path + + {KEY => generate_as_json(options)} end private diff --git a/rb/spec/unit/selenium/webdriver/firefox/binary_spec.rb b/rb/spec/unit/selenium/webdriver/firefox/binary_spec.rb deleted file mode 100644 index 356414293c8d8..0000000000000 --- a/rb/spec/unit/selenium/webdriver/firefox/binary_spec.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -# Licensed to the Software Freedom Conservancy (SFC) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The SFC licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -require File.expand_path('../spec_helper', __dir__) - -module Selenium - module WebDriver - module Firefox - describe Binary do - describe '.path' do - context 'when can not find binary file' do - it 'raises Selenium::WebDriver::Error::WebDriverError' do - expect(File).to receive(:file?).and_return(false) - - expect { Binary.path }.to raise_error(Selenium::WebDriver::Error::WebDriverError) - end - end - end - end - end # Firefox - end # WebDriver -end # Selenium diff --git a/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb b/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb index 775d4e4a4971b..4d107956678a7 100644 --- a/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb +++ b/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb @@ -26,7 +26,6 @@ module WebDriver before do allow(Platform).to receive(:assert_executable).and_return(true) - allow(Firefox::Binary).to receive(:path).and_return('/foo/bar') end describe '#new' do @@ -85,12 +84,12 @@ module WebDriver expect(service.executable_path).to eq path end - it 'provides binary path by default' do + it 'does not create args by default' do allow(Platform).to receive(:find_binary).and_return(service_path) service = Service.firefox - expect(service.instance_variable_get('@extra_args')).to eq(['--binary=/foo/bar']) + expect(service.instance_variable_get('@extra_args')).to be_empty end it 'uses provided args' do @@ -98,7 +97,7 @@ module WebDriver service = Service.firefox(args: ['--foo', '--bar']) - expect(service.instance_variable_get('@extra_args')).to include('--foo', '--bar') + expect(service.instance_variable_get('@extra_args')).to eq ['--foo', '--bar'] end # This is deprecated behavior @@ -108,7 +107,7 @@ module WebDriver service = Service.firefox(args: {log: '/path/to/log', marionette_port: 4}) - expect(service.instance_variable_get('@extra_args')).to include('--log=/path/to/log', '--marionette-port=4') + expect(service.instance_variable_get('@extra_args')).to eq ['--log=/path/to/log', '--marionette-port=4'] end end end @@ -120,7 +119,6 @@ module Firefox before do allow(Remote::Bridge).to receive(:new).and_return(bridge) - allow(Firefox::Binary).to receive(:path).and_return('/foo/bar') end it 'is not created when :url is provided' do @@ -140,7 +138,7 @@ module Firefox expect(Service).to receive(:new).with(path: driver_path, port: nil, - args: ['--binary=/foo/bar']).and_return(service) + args: nil).and_return(service) expect { described_class.new(driver_path: driver_path) @@ -152,7 +150,7 @@ module Firefox expect(Service).to receive(:new).with(path: nil, port: driver_port, - args: ['--binary=/foo/bar']).and_return(service) + args: nil).and_return(service) expect { described_class.new(port: driver_port)