Skip to content

Commit

Permalink
[rb] Allow driver path to be set using ENV variables (#14287)
Browse files Browse the repository at this point in the history
  • Loading branch information
aguspe authored Sep 16, 2024
1 parent 39c38e4 commit 7602371
Show file tree
Hide file tree
Showing 19 changed files with 221 additions and 6 deletions.
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/chrome/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions rb/lib/selenium/webdriver/common/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion rb/lib/selenium/webdriver/edge/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/firefox/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/ie/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rb/lib/selenium/webdriver/safari/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions rb/sig/lib/selenium/webdriver/chrome/service.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Selenium
module WebDriver
module Chrome
class Service < WebDriver::Service
DRIVER_PATH_ENV_KEY: String

@log: untyped

DEFAULT_PORT: Integer
Expand Down
2 changes: 2 additions & 0 deletions rb/sig/lib/selenium/webdriver/common/service.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions rb/sig/lib/selenium/webdriver/edge/service.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Selenium
module WebDriver
module Edge
class Service < WebDriver::Service
DRIVER_PATH_ENV_KEY: String

@log: untyped

DEFAULT_PORT: Integer
Expand Down
1 change: 1 addition & 0 deletions rb/sig/lib/selenium/webdriver/firefox/service.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Selenium
class Service < WebDriver::Service
DEFAULT_PORT: 4444

DRIVER_PATH_ENV_KEY: String
EXECUTABLE: "geckodriver"

SHUTDOWN_SUPPORTED: false
Expand Down
1 change: 1 addition & 0 deletions rb/sig/lib/selenium/webdriver/ie/service.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Selenium
class Service < WebDriver::Service
DEFAULT_PORT: Integer

DRIVER_PATH_ENV_KEY: String
EXECUTABLE: String

SHUTDOWN_SUPPORTED: bool
Expand Down
1 change: 1 addition & 0 deletions rb/sig/lib/selenium/webdriver/safari/service.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Selenium
class Service < WebDriver::Service
DEFAULT_PORT: Integer

DRIVER_PATH_ENV_KEY: String
EXECUTABLE: String

SHUTDOWN_SUPPORTED: bool
Expand Down
43 changes: 43 additions & 0 deletions rb/spec/integration/selenium/webdriver/ie/service_spec.rb
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions rb/spec/integration/selenium/webdriver/safari/service_spec.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions rb/spec/unit/selenium/webdriver/chrome/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions rb/spec/unit/selenium/webdriver/edge/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions rb/spec/unit/selenium/webdriver/firefox/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions rb/spec/unit/selenium/webdriver/ie/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions rb/spec/unit/selenium/webdriver/safari/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7602371

Please sign in to comment.