diff --git a/rb/lib/selenium/webdriver/common/selenium_manager.rb b/rb/lib/selenium/webdriver/common/selenium_manager.rb index 1dcf628042d5b9..f028c5f527618c 100644 --- a/rb/lib/selenium/webdriver/common/selenium_manager.rb +++ b/rb/lib/selenium/webdriver/common/selenium_manager.rb @@ -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 @@ -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 diff --git a/rb/spec/unit/selenium/webdriver/common/selenium_manager_spec.rb b/rb/spec/unit/selenium/webdriver/common/selenium_manager_spec.rb index 5142248f19592e..3910e5b82676e2 100644 --- a/rb/spec/unit/selenium/webdriver/common/selenium_manager_spec.rb +++ b/rb/spec/unit/selenium/webdriver/common/selenium_manager_spec.rb @@ -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) @@ -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) @@ -115,12 +114,11 @@ 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') @@ -128,11 +126,11 @@ def stub_binary(binary) 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') @@ -140,9 +138,19 @@ def stub_binary(binary) 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