Skip to content

Commit 4ce5514

Browse files
committed
[rb] improve error management
1 parent cd556fc commit 4ce5514

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

rb/lib/selenium/webdriver/common/driver_finder.rb

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,52 +20,57 @@
2020
module Selenium
2121
module WebDriver
2222
class DriverFinder
23-
def self.results(options, klass)
24-
path = klass.driver_path
25-
path = path.call if path.is_a?(Proc)
23+
class << self
24+
def results(options, klass)
25+
path = klass.driver_path
26+
path = path.call if path.is_a?(Proc)
27+
exe = klass::EXECUTABLE
2628

27-
results = if path
28-
{driver_path: path}
29-
else
30-
begin
31-
SeleniumManager.results(to_args(options))
32-
rescue StandardError => e
33-
raise Error::NoSuchDriverError,
34-
"Unable to obtain #{klass::EXECUTABLE} using Selenium Manager; #{e.message}"
35-
end
36-
end
37-
38-
begin
39-
Platform.assert_executable(results[:driver_path])
40-
rescue TypeError
41-
raise Error::NoSuchDriverError, "Unable to locate or obtain #{klass::EXECUTABLE}"
42-
rescue Error::WebDriverError => e
43-
raise Error::NoSuchDriverError, "#{klass::EXECUTABLE} located, but: #{e.message}"
29+
if path
30+
validate_files(exe, driver_path: path)
31+
else
32+
begin
33+
validate_files(exe, **SeleniumManager.results(to_args(options)))
34+
rescue StandardError => e
35+
raise Error::NoSuchDriverError("Unable to obtain #{exe} using Selenium Manager"), e.message, e.backtrace
36+
end
37+
end
4438
end
4539

46-
results
47-
end
40+
def path(options, klass)
41+
WebDriver.logger.deprecate('`DriverFinder.path`', '`DriverFinder.results`', id: :driver_finder)
42+
results(options, klass)[:driver_path]
43+
end
4844

49-
def self.path(options, klass)
50-
WebDriver.logger.deprecate('`DriverFinder.path`', '`DriverFinder.results`', id: :driver_finder)
51-
results(options, klass)[:driver_path]
52-
end
45+
private
5346

54-
def self.to_args(options)
55-
args = ['--browser', options.browser_name]
56-
if options.browser_version
57-
args << '--browser-version'
58-
args << options.browser_version
47+
def to_args(options)
48+
args = ['--browser', options.browser_name]
49+
if options.browser_version
50+
args << '--browser-version'
51+
args << options.browser_version
52+
end
53+
if options.respond_to?(:binary) && !options.binary.nil?
54+
args << '--browser-path'
55+
args << options.binary.gsub('\\', '\\\\\\')
56+
end
57+
if options.proxy
58+
args << '--proxy'
59+
args << (options.proxy.ssl || options.proxy.http)
60+
end
61+
args
5962
end
60-
if options.respond_to?(:binary) && !options.binary.nil?
61-
args << '--browser-path'
62-
args << options.binary.gsub('\\', '\\\\\\')
63-
end
64-
if options.proxy
65-
args << '--proxy'
66-
args << (options.proxy.ssl || options.proxy.http)
63+
64+
def validate_files(exe, **opts)
65+
opts.each_value do |value|
66+
raise Error::NoSuchDriverError("Unable to locate or obtain #{exe})") if value.nil?
67+
68+
Platform.assert_executable(value)
69+
rescue Error::WebDriverError => e
70+
raise Error::NoSuchDriverError("#{exe} located, but has problems"), e.message, e.backtrace
71+
end
72+
opts
6773
end
68-
args
6974
end
7075
end
7176
end

rb/lib/selenium/webdriver/common/selenium_manager.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ def results(*arguments)
4242
arguments << '--debug' if WebDriver.logger.debug?
4343
output = run(binary, *arguments)
4444

45-
browser_path = Platform.cygwin? ? Platform.cygwin_path(output['browser_path']) : output['browser_path']
46-
driver_path = Platform.cygwin? ? Platform.cygwin_path(output['driver_path']) : output['driver_path']
47-
Platform.assert_executable driver_path
48-
49-
{driver_path: driver_path, browser_path: browser_path}
45+
{driver_path: Platform.cygwin? ? Platform.cygwin_path(output['driver_path']) : output['driver_path'],
46+
browser_path: Platform.cygwin? ? Platform.cygwin_path(output['browser_path']) : output['browser_path']}
5047
end
5148

5249
private
@@ -77,14 +74,12 @@ def binary
7774
end
7875

7976
def validate_location(location)
77+
raise Error::WebDriverError('Unable to locate or obtain Selenium Manager binary') if value.nil?
78+
8079
begin
81-
Platform.assert_file(location)
8280
Platform.assert_executable(location)
83-
rescue TypeError
84-
raise Error::WebDriverError,
85-
"Unable to locate or obtain Selenium Manager binary; #{location} is not a valid file object"
8681
rescue Error::WebDriverError => e
87-
raise Error::WebDriverError, "Selenium Manager binary located, but #{e.message}"
82+
raise Error::WebDriverError, "#{exe} located, but has problems: #{e.message}"
8883
end
8984

9085
WebDriver.logger.debug("Selenium Manager binary found at #{location}", id: :selenium_manager)

0 commit comments

Comments
 (0)