Skip to content

Commit 1d78e8d

Browse files
committed
[rb] process output in driver finder not selenium manager
1 parent eea55ae commit 1d78e8d

File tree

5 files changed

+45
-44
lines changed

5 files changed

+45
-44
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,22 @@ def result(options, klass)
2626
path = path.call if path.is_a?(Proc)
2727
exe = klass::EXECUTABLE
2828

29-
results = if path
30-
WebDriver.logger.debug("Skipping Selenium Manager; user provided #{exe} location: #{path}")
31-
{driver_path: path}
32-
else
33-
SeleniumManager.results(*to_args(options))
34-
end
35-
validate_files(**results)
29+
if path
30+
WebDriver.logger.debug("Skipping Selenium Manager; path to #{exe} specified in service class: #{path}")
31+
Platform.assert_executable(path)
32+
{driver_path: path}
33+
else
34+
output = SeleniumManager.result(*to_args(options))
35+
result = {driver_path: Platform.cygwin_path(output['driver_path'], only_cygwin: true),
36+
browser_path: Platform.cygwin_path(output['browser_path'], only_cygwin: true)}
37+
Platform.assert_executable(result[:driver_path])
38+
Platform.assert_executable(result[:browser_path])
39+
result
40+
end
3641
rescue StandardError => e
3742
WebDriver.logger.error("Exception occurred: #{e.message}")
3843
WebDriver.logger.error("Backtrace:\n\t#{e.backtrace&.join("\n\t")}")
39-
raise Error::NoSuchDriverError, "Unable to obtain #{exe} using Selenium Manager"
44+
raise Error::NoSuchDriverError, "Unable to obtain #{exe}"
4045
end
4146

4247
def path(options, klass)
@@ -62,11 +67,6 @@ def to_args(options)
6267
end
6368
args
6469
end
65-
66-
def validate_files(**opts)
67-
opts.each_value { |value| Platform.assert_executable(value) }
68-
opts
69-
end
7070
end
7171
end
7272
end

rb/lib/selenium/webdriver/common/platform.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def wrap_in_quotes_if_necessary(str)
111111
windows? && !cygwin? ? %("#{str}") : str
112112
end
113113

114-
def cygwin_path(path, **opts)
114+
def cygwin_path(path, only_cygwin: false, **opts)
115+
return path if only_cygwin && !cygwin?
116+
115117
flags = []
116118
opts.each { |k, v| flags << "--#{k}" if v }
117119

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ def bin_path
3636

3737
# @param [Array] arguments what gets sent to to Selenium Manager binary.
3838
# @return [Hash] paths to the requested assets.
39-
def results(*arguments)
39+
def result(*arguments)
4040
arguments += %w[--language-binding ruby]
4141
arguments += %w[--output json]
4242
arguments << '--debug' if WebDriver.logger.debug?
43-
output = run(binary, *arguments)
4443

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']}
44+
run(binary, *arguments)
4745
end
4846

4947
private
@@ -71,16 +69,17 @@ def run(*command)
7169
raise Error::WebDriverError, "Unsuccessful command executed: #{command}; #{e.message}"
7270
end
7371

74-
json_output = stdout.empty? ? {} : JSON.parse(stdout)
75-
(json_output['logs'] || []).each do |log|
72+
json_output = stdout.empty? ? {'logs' => [], 'result' => {}} : JSON.parse(stdout)
73+
json_output['logs'].each do |log|
7674
level = log['level'].casecmp('info').zero? ? 'debug' : log['level'].downcase
7775
WebDriver.logger.send(level, log['message'], id: :selenium_manager)
7876
end
7977

8078
result = json_output['result']
81-
return result unless status.exitstatus.positive?
79+
return result unless status.exitstatus.positive? || result.nil?
8280

83-
raise Error::WebDriverError, "Unsuccessful command executed: #{command}\n#{result}\n#{stderr}"
81+
raise Error::WebDriverError,
82+
"Unsuccessful command executed: #{command} - Code #{status.exitstatus}\n#{result}\n#{stderr}"
8483
end
8584

8685
def platform_location

rb/spec/unit/selenium/webdriver/common/driver_finder_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@ module WebDriver
4545
end
4646

4747
it 'validates all returned files' do
48-
allow(SeleniumManager).to receive(:result).and_return({browser_path: '/path/to/browser',
49-
driver_path: '/path/to/driver',
50-
anything: '/path/to/random'})
48+
allow(SeleniumManager).to receive(:result).and_return({'browser_path' => '/path/to/browser',
49+
'driver_path' => '/path/to/driver'})
5150
allow(Platform).to receive(:assert_executable).with('/path/to/browser').and_return(true)
5251
allow(Platform).to receive(:assert_executable).with('/path/to/driver').and_return(true)
53-
allow(Platform).to receive(:assert_executable).with('/path/to/random').and_return(true)
5452

5553
described_class.result(Options.chrome, Chrome::Service)
5654

5755
expect(Platform).to have_received(:assert_executable).with('/path/to/browser')
5856
expect(Platform).to have_received(:assert_executable).with('/path/to/driver')
59-
expect(Platform).to have_received(:assert_executable).with('/path/to/random')
6057
end
6158

6259
it 'wraps error with NoSuchDriverError' do
@@ -67,24 +64,27 @@ module WebDriver
6764
described_class.result(Options.chrome, Chrome::Service)
6865
}.to output(/Exception occurred: an error/).to_stderr_from_any_process
6966
}.to raise_error(WebDriver::Error::NoSuchDriverError,
70-
/Unable to obtain chromedriver using Selenium Manager; For documentation on this error/)
67+
/Unable to obtain chromedriver; For documentation on this error/)
7168
end
7269

7370
it 'creates arguments' do
74-
allow(SeleniumManager).to receive(:result).and_return({})
71+
allow(SeleniumManager).to receive(:result).and_return({'browser_path' => '/path/to/browser',
72+
'driver_path' => '/path/to/driver'})
7573
proxy = instance_double(Proxy, ssl: 'proxy')
7674
options = Options.chrome(browser_version: 'stable', proxy: proxy, binary: 'path/to/browser')
75+
allow(Platform).to receive(:assert_executable).with('/path/to/browser').and_return(true)
76+
allow(Platform).to receive(:assert_executable).with('/path/to/driver').and_return(true)
7777

7878
described_class.result(options, Chrome::Service)
7979

8080
expect(SeleniumManager).to have_received(:result).with('--browser',
81-
options.browser_name,
82-
'--browser-version',
83-
options.browser_version,
84-
'--browser-path',
85-
options.binary,
86-
'--proxy',
87-
options.proxy.ssl)
81+
options.browser_name,
82+
'--browser-version',
83+
options.browser_version,
84+
'--browser-path',
85+
options.binary,
86+
'--proxy',
87+
options.proxy.ssl)
8888
end
8989
end
9090
end

rb/spec/unit/selenium/webdriver/common/selenium_manager_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module WebDriver
6868
describe '.run' do
6969
it 'returns result if positive exit status' do
7070
status = instance_double(Process::Status, exitstatus: 0)
71-
stdout = '{"result": "value"}'
71+
stdout = '{"result": "value", "logs": []}'
7272
allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])
7373

7474
expect(described_class.send(:run, 'anything')).to eq 'value'
@@ -84,21 +84,21 @@ module WebDriver
8484

8585
it 'errors if exit status greater than 0' do
8686
status = instance_double(Process::Status, exitstatus: 1)
87-
stdout = '{"result": "value"}'
87+
stdout = '{"result": "value", "logs": []}'
8888
allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])
8989

90+
msg = /Unsuccessful command executed: \["anything"\] - Code 1\nvalue\nstderr/
9091
expect {
9192
described_class.send(:run, 'anything')
92-
}.to raise_error(Error::WebDriverError, /Unsuccessful command executed: \["anything"\]\nvalue\nstderr/)
93+
}.to raise_error(Error::WebDriverError, msg)
9394
end
9495
end
9596

9697
describe '.results' do
97-
it 'returns asset paths' do
98-
allow(described_class).to receive_messages(binary: 'binary', run: {'browser_path' => '/path/to/browser',
99-
'driver_path' => '/path/to/driver'})
100-
expect(described_class.result('something')).to eq({browser_path: '/path/to/browser',
101-
driver_path: '/path/to/driver'})
98+
it 'returns exact output from #run' do
99+
return_map = {}
100+
allow(described_class).to receive_messages(binary: 'binary', run: return_map)
101+
expect(described_class.result('something')).to eq(return_map)
102102
end
103103
end
104104
end

0 commit comments

Comments
 (0)