Skip to content

Commit 9fdeaa9

Browse files
authored
[rb] add browser output from selenium manager to options (SeleniumHQ#12398)
1 parent d4a124d commit 9fdeaa9

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

rb/Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
selenium-devtools (0.114.0)
4+
selenium-devtools (0.115.0)
55
selenium-webdriver (~> 4.2)
66
selenium-webdriver (4.11.0)
77
rexml (~> 3.2, >= 3.2.5)

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,24 @@ def bin_path
3939
def driver_path(options)
4040
command = generate_command(binary, options)
4141

42-
location = run(*command)
43-
WebDriver.logger.debug("Driver found at #{location}", id: :selenium_manager)
44-
Platform.assert_executable location
42+
output = run(*command)
4543

46-
location
44+
browser_path = output['browser_path']
45+
driver_path = output['driver_path']
46+
Platform.assert_executable driver_path
47+
48+
if options.respond_to? :binary
49+
options.binary = browser_path
50+
options.browser_version = nil
51+
end
52+
53+
driver_path
4754
end
4855

4956
private
5057

5158
def generate_command(binary, options)
52-
command = [binary, '--browser', options.browser_name, '--output', 'json']
59+
command = [binary, '--browser', options.browser_name]
5360
if options.browser_version
5461
command << '--browser-version'
5562
command << options.browser_version
@@ -62,7 +69,6 @@ def generate_command(binary, options)
6269
command << '--proxy'
6370
(command << options.proxy.ssl) || options.proxy.http
6471
end
65-
command << '--debug' if WebDriver.logger.debug?
6672
command
6773
end
6874

@@ -95,12 +101,15 @@ def binary
95101
end
96102

97103
def run(*command)
104+
command += %w[--output json]
105+
command << '--debug' if WebDriver.logger.debug?
106+
98107
WebDriver.logger.debug("Executing Process #{command}", id: :selenium_manager)
99108

100109
begin
101110
stdout, stderr, status = Open3.capture3(*command)
102111
json_output = stdout.empty? ? nil : JSON.parse(stdout)
103-
result = json_output&.dig('result', 'message')
112+
result = json_output['result']
104113
rescue StandardError => e
105114
raise Error::WebDriverError, "Unsuccessful command executed: #{command}; #{e.message}"
106115
end

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

+18-10
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ def stub_binary(binary)
7878

7979
describe 'self.driver_path' do
8080
it 'determines browser name by default' do
81-
allow(described_class).to receive(:run)
81+
allow(described_class).to receive(:run).and_return('browser_path' => '', 'driver_path' => '')
8282
allow(described_class).to receive(:binary).and_return('selenium-manager')
8383
allow(Platform).to receive(:assert_executable)
8484

8585
described_class.driver_path(Options.chrome)
8686

8787
expect(described_class).to have_received(:run)
88-
.with('selenium-manager', '--browser', 'chrome', '--output', 'json')
88+
.with('selenium-manager', '--browser', 'chrome')
8989
end
9090

9191
it 'uses browser version if specified' do
92-
allow(described_class).to receive(:run)
92+
allow(described_class).to receive(:run).and_return('browser_path' => '', 'driver_path' => '')
9393
allow(described_class).to receive(:binary).and_return('selenium-manager')
9494
allow(Platform).to receive(:assert_executable)
9595
options = Options.chrome(browser_version: 1)
@@ -99,13 +99,12 @@ def stub_binary(binary)
9999
expect(described_class).to have_received(:run)
100100
.with('selenium-manager',
101101
'--browser', 'chrome',
102-
'--output', 'json',
103102
'--browser-version', 1)
104103
end
105104

106105
it 'uses proxy if specified' do
107106
proxy = Selenium::WebDriver::Proxy.new(ssl: 'proxy')
108-
allow(described_class).to receive(:run)
107+
allow(described_class).to receive(:run).and_return('browser_path' => '', 'driver_path' => '')
109108
allow(described_class).to receive(:binary).and_return('selenium-manager')
110109
allow(Platform).to receive(:assert_executable)
111110
options = Options.chrome(proxy: proxy)
@@ -115,34 +114,43 @@ def stub_binary(binary)
115114
expect(described_class).to have_received(:run)
116115
.with('selenium-manager',
117116
'--browser', 'chrome',
118-
'--output', 'json',
119117
'--proxy', 'proxy')
120118
end
121119

122120
it 'uses browser location if specified' do
123-
allow(described_class).to receive(:run)
121+
allow(described_class).to receive(:run).and_return('browser_path' => '', 'driver_path' => '')
124122
allow(described_class).to receive(:binary).and_return('selenium-manager')
125123
allow(Platform).to receive(:assert_executable)
126124
options = Options.chrome(binary: '/path/to/browser')
127125

128126
described_class.driver_path(options)
129127

130128
expect(described_class).to have_received(:run)
131-
.with('selenium-manager', '--browser', 'chrome', '--output', 'json', '--browser-path', '/path/to/browser')
129+
.with('selenium-manager', '--browser', 'chrome', '--browser-path', '/path/to/browser')
132130
end
133131

134132
it 'properly escapes plain spaces in browser location' do
135-
allow(described_class).to receive(:run)
133+
allow(described_class).to receive(:run).and_return('browser_path' => 'a', 'driver_path' => '')
136134
allow(described_class).to receive(:binary).and_return('selenium-manager')
137135
allow(Platform).to receive(:assert_executable)
138136
options = Options.chrome(binary: '/path to/the/browser')
139137

140138
described_class.driver_path(options)
141139

142140
expect(described_class).to have_received(:run)
143-
.with('selenium-manager', '--browser', 'chrome', '--output', 'json',
141+
.with('selenium-manager', '--browser', 'chrome',
144142
'--browser-path', '/path to/the/browser')
145143
end
144+
145+
it 'sets binary location on options' do
146+
allow(described_class).to receive(:run).and_return('browser_path' => 'foo', 'driver_path' => '')
147+
allow(described_class).to receive(:binary).and_return('selenium-manager')
148+
allow(Platform).to receive(:assert_executable)
149+
options = Options.chrome
150+
151+
described_class.driver_path(options)
152+
expect(options.binary).to eq 'foo'
153+
end
146154
end
147155
end
148156
end # WebDriver

0 commit comments

Comments
 (0)