Skip to content

Commit 479d197

Browse files
authored
[rb] handle issue with selenium manager exit status being nil (#15676)
Fixes #14622
1 parent 3186f19 commit 479d197

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

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

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,12 @@ def binary
6161
end
6262

6363
def run(*command)
64-
WebDriver.logger.debug("Executing Process #{command}", id: :selenium_manager)
65-
66-
begin
67-
stdout, stderr, status = Open3.capture3(*command)
68-
rescue StandardError => e
69-
raise Error::WebDriverError, "Unsuccessful command executed: #{command}; #{e.message}"
70-
end
64+
stdout, stderr, status = execute_command(*command)
65+
result = parse_result_and_log(stdout)
7166

72-
json_output = stdout.empty? ? {'logs' => [], 'result' => {}} : JSON.parse(stdout)
73-
json_output['logs'].each do |log|
74-
level = log['level'].casecmp('info').zero? ? 'debug' : log['level'].downcase
75-
WebDriver.logger.send(level, log['message'], id: :selenium_manager)
76-
end
67+
validate_command_result(command, status, result, stderr)
7768

78-
result = json_output['result']
79-
return result unless status.exitstatus.positive? || result.nil?
80-
81-
raise Error::WebDriverError,
82-
"Unsuccessful command executed: #{command} - Code #{status.exitstatus}\n#{result}\n#{stderr}"
69+
result
8370
end
8471

8572
def platform_location
@@ -98,6 +85,38 @@ def platform_location
9885
raise Error::WebDriverError, "unsupported platform: #{Platform.os}"
9986
end
10087
end
88+
89+
def execute_command(*command)
90+
WebDriver.logger.debug("Executing Process #{command}", id: :selenium_manager)
91+
92+
Open3.capture3(*command)
93+
rescue StandardError => e
94+
raise Error::WebDriverError, "Unsuccessful command executed: #{command}; #{e.message}"
95+
end
96+
97+
def parse_result_and_log(stdout)
98+
json_output = stdout.empty? ? {'logs' => [], 'result' => {}} : JSON.parse(stdout)
99+
100+
json_output['logs'].each do |log|
101+
level = log['level'].casecmp('info').zero? ? 'debug' : log['level'].downcase
102+
WebDriver.logger.send(level, log['message'], id: :selenium_manager)
103+
end
104+
105+
json_output['result']
106+
end
107+
108+
def validate_command_result(command, status, result, stderr)
109+
if status.nil? || status.exitstatus.nil?
110+
WebDriver.logger.info("No exit status for: #{command}. Assuming success if result is present.",
111+
id: :selenium_manager)
112+
end
113+
114+
return unless status&.exitstatus&.positive? || result.nil?
115+
116+
code = status&.exitstatus || 'exit status not available'
117+
raise Error::WebDriverError,
118+
"Unsuccessful command executed: #{command} - Code #{code}\n#{result}\n#{stderr}"
119+
end
101120
end
102121
end # SeleniumManager
103122
end # WebDriver

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ module WebDriver
9292
described_class.send(:run, 'anything')
9393
}.to raise_error(Error::WebDriverError, msg)
9494
end
95+
96+
it 'succeeds when exitstatus is nil and result is present' do
97+
status = instance_double(Process::Status, exitstatus: nil)
98+
stdout = '{"result": "value", "logs": []}'
99+
allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])
100+
101+
expect {
102+
expect(described_class.send(:run, 'anything')).to eq 'value'
103+
}.to have_info(:selenium_manager)
104+
end
105+
106+
it 'raises if result is nil even with successful exitstatus' do
107+
status = instance_double(Process::Status, exitstatus: 0)
108+
stdout = '{"logs": []}'
109+
allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])
110+
111+
expect {
112+
described_class.send(:run, 'anything')
113+
}.to raise_error(Error::WebDriverError, /Unsuccessful command executed/)
114+
end
95115
end
96116

97117
describe '.binary_paths' do

0 commit comments

Comments
 (0)