Skip to content

Commit

Permalink
[rb] add browser output from selenium manager to options
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jul 24, 2023
1 parent 9ff4c2b commit 79c1c2f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
25 changes: 17 additions & 8 deletions rb/lib/selenium/webdriver/common/selenium_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,36 @@ def bin_path
def driver_path(options)
command = generate_command(binary, options)

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

location
browser_path = output['browser_path']
driver_path = output['driver_path']
Platform.assert_executable driver_path

if options.respond_to? :binary
options.binary = browser_path
options.browser_version = nil
end

driver_path
end

private

def generate_command(binary, options)
command = [binary, '--browser', options.browser_name, '--output', 'json']
command = [binary, '--browser', options.browser_name]
if options.browser_version
command << '--browser-version'
command << options.browser_version
end
if options.respond_to?(:binary) && !options.binary.nil?
command << '--browser-path'
command << options.binary.gsub('\\', '\\\\\\')
command << options.binary
end
if options.proxy
command << '--proxy'
(command << options.proxy.ssl) || options.proxy.http
end
command << '--debug' if WebDriver.logger.debug?
command
end

Expand Down Expand Up @@ -95,12 +101,15 @@ def binary
end

def run(*command)
command += %w[--output json]
command << '--debug' if WebDriver.logger.debug?

WebDriver.logger.debug("Executing Process #{command}", id: :selenium_manager)

begin
stdout, stderr, status = Open3.capture3(*command)
json_output = stdout.empty? ? nil : JSON.parse(stdout)
result = json_output&.dig('result', 'message')
result = json_output['result']
rescue StandardError => e
raise Error::WebDriverError, "Unsuccessful command executed: #{command}; #{e.message}"
end
Expand Down
28 changes: 18 additions & 10 deletions rb/spec/unit/selenium/webdriver/common/selenium_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ def stub_binary(binary)

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

described_class.driver_path(Options.chrome)

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

it 'uses browser version if specified' do
allow(described_class).to receive(:run)
allow(described_class).to receive(:run).and_return('browser_path' => '', 'driver_path' => '')
allow(described_class).to receive(:binary).and_return('selenium-manager')
allow(Platform).to receive(:assert_executable)
options = Options.chrome(browser_version: 1)
Expand All @@ -99,13 +99,12 @@ def stub_binary(binary)
expect(described_class).to have_received(:run)
.with('selenium-manager',
'--browser', 'chrome',
'--output', 'json',
'--browser-version', 1)
end

it 'uses proxy if specified' do
proxy = Selenium::WebDriver::Proxy.new(ssl: 'proxy')
allow(described_class).to receive(:run)
allow(described_class).to receive(:run).and_return('browser_path' => '', 'driver_path' => '')
allow(described_class).to receive(:binary).and_return('selenium-manager')
allow(Platform).to receive(:assert_executable)
options = Options.chrome(proxy: proxy)
Expand All @@ -115,34 +114,43 @@ def stub_binary(binary)
expect(described_class).to have_received(:run)
.with('selenium-manager',
'--browser', 'chrome',
'--output', 'json',
'--proxy', 'proxy')
end

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

described_class.driver_path(options)

expect(described_class).to have_received(:run)
.with('selenium-manager', '--browser', 'chrome', '--output', 'json', '--browser-path', '/path/to/browser')
.with('selenium-manager', '--browser', 'chrome', '--browser-path', '/path/to/browser')
end

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

described_class.driver_path(options)

expect(described_class).to have_received(:run)
.with('selenium-manager', '--browser', 'chrome', '--output', 'json',
.with('selenium-manager', '--browser', 'chrome',
'--browser-path', '/path to/the/browser')
end

it 'sets binary location on options' do
allow(described_class).to receive(:run).and_return('browser_path' => 'foo', 'driver_path' => '')
allow(described_class).to receive(:binary).and_return('selenium-manager')
allow(Platform).to receive(:assert_executable)
options = Options.chrome

described_class.driver_path(options)
expect(options.binary).to eq 'foo'
end
end
end
end # WebDriver
Expand Down

0 comments on commit 79c1c2f

Please sign in to comment.