From 7602371488ebd14d2c6d8d980134bff42bbd17e9 Mon Sep 17 00:00:00 2001 From: Augustin Gottlieb Pequeno <33221555+aguspe@users.noreply.github.com> Date: Mon, 16 Sep 2024 18:53:38 +0200 Subject: [PATCH] [rb] Allow driver path to be set using ENV variables (#14287) --- rb/lib/selenium/webdriver/chrome/service.rb | 1 + rb/lib/selenium/webdriver/common/service.rb | 15 +++++-- rb/lib/selenium/webdriver/edge/service.rb | 2 +- rb/lib/selenium/webdriver/firefox/service.rb | 1 + rb/lib/selenium/webdriver/ie/service.rb | 1 + rb/lib/selenium/webdriver/safari/service.rb | 2 +- .../lib/selenium/webdriver/chrome/service.rbs | 2 + .../lib/selenium/webdriver/common/service.rbs | 2 + .../lib/selenium/webdriver/edge/service.rbs | 2 + .../selenium/webdriver/firefox/service.rbs | 1 + rb/sig/lib/selenium/webdriver/ie/service.rbs | 1 + .../lib/selenium/webdriver/safari/service.rbs | 1 + .../selenium/webdriver/ie/service_spec.rb | 43 +++++++++++++++++++ .../selenium/webdriver/safari/service_spec.rb | 43 +++++++++++++++++++ .../selenium/webdriver/chrome/service_spec.rb | 22 ++++++++++ .../selenium/webdriver/edge/service_spec.rb | 22 ++++++++++ .../webdriver/firefox/service_spec.rb | 22 ++++++++++ .../selenium/webdriver/ie/service_spec.rb | 22 ++++++++++ .../selenium/webdriver/safari/service_spec.rb | 22 ++++++++++ 19 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 rb/spec/integration/selenium/webdriver/ie/service_spec.rb create mode 100644 rb/spec/integration/selenium/webdriver/safari/service_spec.rb diff --git a/rb/lib/selenium/webdriver/chrome/service.rb b/rb/lib/selenium/webdriver/chrome/service.rb index 82945bba8964a..834c80d8ea461 100644 --- a/rb/lib/selenium/webdriver/chrome/service.rb +++ b/rb/lib/selenium/webdriver/chrome/service.rb @@ -24,6 +24,7 @@ class Service < WebDriver::Service DEFAULT_PORT = 9515 EXECUTABLE = 'chromedriver' SHUTDOWN_SUPPORTED = true + DRIVER_PATH_ENV_KEY = 'SE_CHROMEDRIVER' def log return @log unless @log.is_a? String diff --git a/rb/lib/selenium/webdriver/common/service.rb b/rb/lib/selenium/webdriver/common/service.rb index e20f7569db28c..b0f274959ebeb 100644 --- a/rb/lib/selenium/webdriver/common/service.rb +++ b/rb/lib/selenium/webdriver/common/service.rb @@ -69,6 +69,7 @@ def driver_path=(path) def initialize(path: nil, port: nil, log: nil, args: nil) port ||= self.class::DEFAULT_PORT args ||= [] + path ||= env_path @executable_path = path @host = Platform.localhost @@ -87,16 +88,22 @@ def initialize(path: nil, port: nil, log: nil, args: nil) end def launch - @executable_path ||= begin - default_options = WebDriver.const_get("#{self.class.name&.split('::')&.[](2)}::Options").new - DriverFinder.new(default_options, self).driver_path - end + @executable_path ||= env_path || find_driver_path ServiceManager.new(self).tap(&:start) end def shutdown_supported self.class::SHUTDOWN_SUPPORTED end + + def find_driver_path + default_options = WebDriver.const_get("#{self.class.name&.split('::')&.[](2)}::Options").new + DriverFinder.new(default_options, self).driver_path + end + + def env_path + ENV.fetch(self.class::DRIVER_PATH_ENV_KEY, nil) + end end # Service end # WebDriver end # Selenium diff --git a/rb/lib/selenium/webdriver/edge/service.rb b/rb/lib/selenium/webdriver/edge/service.rb index 7b4ca72ecba27..3d99635c59f62 100644 --- a/rb/lib/selenium/webdriver/edge/service.rb +++ b/rb/lib/selenium/webdriver/edge/service.rb @@ -24,7 +24,7 @@ class Service < WebDriver::Service DEFAULT_PORT = 9515 EXECUTABLE = 'msedgedriver' SHUTDOWN_SUPPORTED = true - + DRIVER_PATH_ENV_KEY = 'SE_EDGEDRIVER' def log return @log unless @log.is_a? String diff --git a/rb/lib/selenium/webdriver/firefox/service.rb b/rb/lib/selenium/webdriver/firefox/service.rb index 44f449f2a60d3..ce1526d1ef718 100644 --- a/rb/lib/selenium/webdriver/firefox/service.rb +++ b/rb/lib/selenium/webdriver/firefox/service.rb @@ -24,6 +24,7 @@ class Service < WebDriver::Service DEFAULT_PORT = 4444 EXECUTABLE = 'geckodriver' SHUTDOWN_SUPPORTED = false + DRIVER_PATH_ENV_KEY = 'SE_GECKODRIVER' end # Service end # Firefox end # WebDriver diff --git a/rb/lib/selenium/webdriver/ie/service.rb b/rb/lib/selenium/webdriver/ie/service.rb index 079d1e0341113..9c3b8967e74a4 100644 --- a/rb/lib/selenium/webdriver/ie/service.rb +++ b/rb/lib/selenium/webdriver/ie/service.rb @@ -24,6 +24,7 @@ class Service < WebDriver::Service DEFAULT_PORT = 5555 EXECUTABLE = 'IEDriverServer' SHUTDOWN_SUPPORTED = true + DRIVER_PATH_ENV_KEY = 'SE_IEDRIVER' end # Server end # IE end # WebDriver diff --git a/rb/lib/selenium/webdriver/safari/service.rb b/rb/lib/selenium/webdriver/safari/service.rb index f720b21d4ea70..8b4182aab47be 100644 --- a/rb/lib/selenium/webdriver/safari/service.rb +++ b/rb/lib/selenium/webdriver/safari/service.rb @@ -24,7 +24,7 @@ class Service < WebDriver::Service DEFAULT_PORT = 7050 EXECUTABLE = 'safaridriver' SHUTDOWN_SUPPORTED = false - + DRIVER_PATH_ENV_KEY = 'SE_SAFARIDRIVER' def initialize(path: nil, port: nil, log: nil, args: nil) raise Error::WebDriverError, 'Safari Service does not support setting log output' if log diff --git a/rb/sig/lib/selenium/webdriver/chrome/service.rbs b/rb/sig/lib/selenium/webdriver/chrome/service.rbs index a94d9c8d4900c..a8fa6e98d4bc0 100644 --- a/rb/sig/lib/selenium/webdriver/chrome/service.rbs +++ b/rb/sig/lib/selenium/webdriver/chrome/service.rbs @@ -2,6 +2,8 @@ module Selenium module WebDriver module Chrome class Service < WebDriver::Service + DRIVER_PATH_ENV_KEY: String + @log: untyped DEFAULT_PORT: Integer diff --git a/rb/sig/lib/selenium/webdriver/common/service.rbs b/rb/sig/lib/selenium/webdriver/common/service.rbs index a6abee02e79d2..e07af9ac3e1ce 100644 --- a/rb/sig/lib/selenium/webdriver/common/service.rbs +++ b/rb/sig/lib/selenium/webdriver/common/service.rbs @@ -47,6 +47,8 @@ module Selenium attr_accessor args: untyped + def env_path: -> String + alias extra_args args def initialize: (?path: untyped?, ?port: untyped?, ?log: untyped?, ?args: untyped?) -> void diff --git a/rb/sig/lib/selenium/webdriver/edge/service.rbs b/rb/sig/lib/selenium/webdriver/edge/service.rbs index 9723e6548efed..85752e9c03fba 100644 --- a/rb/sig/lib/selenium/webdriver/edge/service.rbs +++ b/rb/sig/lib/selenium/webdriver/edge/service.rbs @@ -2,6 +2,8 @@ module Selenium module WebDriver module Edge class Service < WebDriver::Service + DRIVER_PATH_ENV_KEY: String + @log: untyped DEFAULT_PORT: Integer diff --git a/rb/sig/lib/selenium/webdriver/firefox/service.rbs b/rb/sig/lib/selenium/webdriver/firefox/service.rbs index 85073c264be3c..64d996b973a04 100644 --- a/rb/sig/lib/selenium/webdriver/firefox/service.rbs +++ b/rb/sig/lib/selenium/webdriver/firefox/service.rbs @@ -4,6 +4,7 @@ module Selenium class Service < WebDriver::Service DEFAULT_PORT: 4444 + DRIVER_PATH_ENV_KEY: String EXECUTABLE: "geckodriver" SHUTDOWN_SUPPORTED: false diff --git a/rb/sig/lib/selenium/webdriver/ie/service.rbs b/rb/sig/lib/selenium/webdriver/ie/service.rbs index 94f9d492a292f..e39fc16a850fd 100644 --- a/rb/sig/lib/selenium/webdriver/ie/service.rbs +++ b/rb/sig/lib/selenium/webdriver/ie/service.rbs @@ -4,6 +4,7 @@ module Selenium class Service < WebDriver::Service DEFAULT_PORT: Integer + DRIVER_PATH_ENV_KEY: String EXECUTABLE: String SHUTDOWN_SUPPORTED: bool diff --git a/rb/sig/lib/selenium/webdriver/safari/service.rbs b/rb/sig/lib/selenium/webdriver/safari/service.rbs index e75e317682014..171f0addbef93 100644 --- a/rb/sig/lib/selenium/webdriver/safari/service.rbs +++ b/rb/sig/lib/selenium/webdriver/safari/service.rbs @@ -4,6 +4,7 @@ module Selenium class Service < WebDriver::Service DEFAULT_PORT: Integer + DRIVER_PATH_ENV_KEY: String EXECUTABLE: String SHUTDOWN_SUPPORTED: bool diff --git a/rb/spec/integration/selenium/webdriver/ie/service_spec.rb b/rb/spec/integration/selenium/webdriver/ie/service_spec.rb new file mode 100644 index 0000000000000..bee1f25908de6 --- /dev/null +++ b/rb/spec/integration/selenium/webdriver/ie/service_spec.rb @@ -0,0 +1,43 @@ +# 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_relative '../spec_helper' + +module Selenium + module WebDriver + module IE + describe Service, exclusive: [{bidi: false, reason: 'Not yet implemented with BiDi'}, {browser: :ie}] do + let(:service) { described_class.new } + let(:service_manager) { service.launch } + + after { service_manager.stop } + + it 'auto uses iedriver' do + service.executable_path = DriverFinder.new(Options.new, described_class.new).driver_path + + expect(service_manager.uri).to be_a(URI) + end + + it 'can be started outside driver' do + expect(service_manager.uri).to be_a(URI) + end + end + end # IE + end # WebDriver +end # Selenium diff --git a/rb/spec/integration/selenium/webdriver/safari/service_spec.rb b/rb/spec/integration/selenium/webdriver/safari/service_spec.rb new file mode 100644 index 0000000000000..c040a637828da --- /dev/null +++ b/rb/spec/integration/selenium/webdriver/safari/service_spec.rb @@ -0,0 +1,43 @@ +# 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_relative '../spec_helper' + +module Selenium + module WebDriver + module Safari + describe Service, exclusive: [{bidi: false, reason: 'Not yet implemented with BiDi'}, {browser: :safari}] do + let(:service) { described_class.new } + let(:service_manager) { service.launch } + + after { service_manager.stop } + + it 'auto uses safaridriver' do + service.executable_path = DriverFinder.new(Options.new, described_class.new).driver_path + + expect(service_manager.uri).to be_a(URI) + end + + it 'can be started outside driver' do + expect(service_manager.uri).to be_a(URI) + end + end + end # Safari + end # WebDriver +end # Selenium diff --git a/rb/spec/unit/selenium/webdriver/chrome/service_spec.rb b/rb/spec/unit/selenium/webdriver/chrome/service_spec.rb index c2f2ebf241841..cbbe6f9ed7ed5 100644 --- a/rb/spec/unit/selenium/webdriver/chrome/service_spec.rb +++ b/rb/spec/unit/selenium/webdriver/chrome/service_spec.rb @@ -119,6 +119,28 @@ module Chrome driver.new(service: service) expect(described_class).not_to have_received(:new) end + + context 'with a path env variable' do + let(:service) { described_class.new } + let(:service_path) { "/path/to/#{Service::EXECUTABLE}" } + + before do + ENV['SE_CHROMEDRIVER'] = service_path + end + + after { ENV.delete('SE_CHROMEDRIVER') } + + it 'uses the path from the environment' do + expect(service.executable_path).to match(/chromedriver/) + end + + it 'updates the path after setting the environment variable' do + ENV['SE_CHROMEDRIVER'] = '/foo/bar' + service.executable_path = service_path + + expect(service.executable_path).to match(/chromedriver/) + end + end end end end # Chrome diff --git a/rb/spec/unit/selenium/webdriver/edge/service_spec.rb b/rb/spec/unit/selenium/webdriver/edge/service_spec.rb index 8f398ca999693..048f2170df300 100644 --- a/rb/spec/unit/selenium/webdriver/edge/service_spec.rb +++ b/rb/spec/unit/selenium/webdriver/edge/service_spec.rb @@ -129,6 +129,28 @@ module Edge expect(service.log).to be_nil expect(service.args).to eq ['--log-path=/path/to/log.txt'] end + + context 'with a path env variable' do + let(:service) { described_class.new } + let(:service_path) { "/path/to/#{Service::EXECUTABLE}" } + + before do + ENV['SE_EDGEDRIVER'] = service_path + end + + after { ENV.delete('SE_EDGEDRIVER') } + + it 'uses the path from the environment' do + expect(service.executable_path).to match(/edgedriver/) + end + + it 'updates the path after setting the environment variable' do + ENV['SE_EDGEDRIVER'] = '/foo/bar' + service.executable_path = service_path + + expect(service.executable_path).to match(/edgedriver/) + end + end end end end # Edge diff --git a/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb b/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb index 60268fcd02eb4..811894e74f4e3 100644 --- a/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb +++ b/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb @@ -117,6 +117,28 @@ module Firefox expect(described_class).not_to have_received(:new) end + + context 'with a path env variable' do + let(:service) { described_class.new } + let(:service_path) { "/path/to/#{Service::EXECUTABLE}" } + + before do + ENV['SE_GECKODRIVER'] = service_path + end + + after { ENV.delete('SE_GECKODRIVER') } + + it 'uses the path from the environment' do + expect(service.executable_path).to match(/geckodriver/) + end + + it 'updates the path after setting the environment variable' do + ENV['SE_GECKODRIVER'] = '/foo/bar' + service.executable_path = service_path + + expect(service.executable_path).to match(/geckodriver/) + end + end end end end # Firefox diff --git a/rb/spec/unit/selenium/webdriver/ie/service_spec.rb b/rb/spec/unit/selenium/webdriver/ie/service_spec.rb index 34f102ca94f75..3c6ecd4edefb6 100644 --- a/rb/spec/unit/selenium/webdriver/ie/service_spec.rb +++ b/rb/spec/unit/selenium/webdriver/ie/service_spec.rb @@ -119,6 +119,28 @@ module IE expect(described_class).not_to have_received(:new) end + + context 'with a path env variable' do + let(:service) { described_class.new } + let(:service_path) { "/path/to/#{Service::EXECUTABLE}" } + + before do + ENV['SE_IEDRIVER'] = service_path + end + + after { ENV.delete('SE_IEDRIVER') } + + it 'uses the path from the environment' do + expect(service.executable_path).to match(/IEDriver/) + end + + it 'updates the path after setting the environment variable' do + ENV['SE_IEDRIVER'] = '/foo/bar' + service.executable_path = service_path + + expect(service.executable_path).to match(/IEDriver/) + end + end end end end # IE diff --git a/rb/spec/unit/selenium/webdriver/safari/service_spec.rb b/rb/spec/unit/selenium/webdriver/safari/service_spec.rb index 9ae84dba3be74..b24ff0cb45688 100644 --- a/rb/spec/unit/selenium/webdriver/safari/service_spec.rb +++ b/rb/spec/unit/selenium/webdriver/safari/service_spec.rb @@ -114,6 +114,28 @@ module Safari expect(described_class).not_to have_received(:new) end + + context 'with a path env variable' do + let(:service) { described_class.new } + let(:service_path) { "/path/to/#{Service::EXECUTABLE}" } + + before do + ENV['SE_SAFARIDRIVER'] = service_path + end + + after { ENV.delete('SE_SAFARIDRIVER') } + + it 'uses the path from the environment' do + expect(service.executable_path).to match(/safaridriver/) + end + + it 'updates the path after setting the environment variable' do + ENV['SE_SAFARIDRIVER'] = '/foo/bar' + service.executable_path = service_path + + expect(service.executable_path).to match(/safaridriver/) + end + end end end end # Safari