Path: blob/trunk/rb/lib/selenium/webdriver/common/selenium_manager.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require 'open3'2021module Selenium22module WebDriver23#24# Wrapper for getting information from the Selenium Manager binaries.25# This implementation is still in beta, and may change.26# @api private27#28class SeleniumManager29class << self30attr_writer :bin_path3132def bin_path33@bin_path ||= '../../../../../bin'34end3536# @param [Array] arguments what gets sent to to Selenium Manager binary.37# @return [Hash] paths to the requested assets.38def binary_paths(*arguments)39arguments += %w[--language-binding ruby]40arguments += %w[--output json]41arguments << '--debug' if WebDriver.logger.debug?4243run(binary, *arguments)44end4546private4748# @return [String] the path to the correct selenium manager49def binary50@binary ||= begin51if (location = ENV.fetch('SE_MANAGER_PATH', nil))52WebDriver.logger.debug("Selenium Manager set by ENV['SE_MANAGER_PATH']: #{location}")53end54location ||= platform_location5556Platform.assert_executable(location)57WebDriver.logger.debug("Selenium Manager binary found at #{location}", id: :selenium_manager)58location59end60end6162def run(*command)63stdout, stderr, status = execute_command(*command)64result = parse_result_and_log(stdout)6566validate_command_result(command, status, result, stderr)6768result69end7071def platform_location72directory = File.expand_path(bin_path, __FILE__)73if Platform.windows?74"#{directory}/windows/selenium-manager.exe"75elsif Platform.mac?76"#{directory}/macos/selenium-manager"77elsif Platform.linux?78"#{directory}/linux/selenium-manager"79elsif Platform.unix?80WebDriver.logger.warn('Selenium Manager binary may not be compatible with Unix',81id: %i[selenium_manager unix_binary])82"#{directory}/linux/selenium-manager"83else84raise Error::WebDriverError, "unsupported platform: #{Platform.os}"85end86end8788def execute_command(*command)89WebDriver.logger.debug("Executing Process #{command}", id: :selenium_manager)9091Open3.capture3(*command)92rescue StandardError => e93raise Error::WebDriverError, "Unsuccessful command executed: #{command}; #{e.message}"94end9596def parse_result_and_log(stdout)97json_output = stdout.empty? ? {'logs' => [], 'result' => {}} : JSON.parse(stdout)9899json_output['logs'].each do |log|100level = log['level'].casecmp('info').zero? ? 'debug' : log['level'].downcase101WebDriver.logger.send(level, log['message'], id: :selenium_manager)102end103104json_output['result']105end106107def validate_command_result(command, status, result, stderr)108if status.nil? || status.exitstatus.nil?109WebDriver.logger.info("No exit status for: #{command}. Assuming success if result is present.",110id: :selenium_manager)111end112113return unless status&.exitstatus&.positive? || result.nil?114115code = status&.exitstatus || 'exit status not available'116raise Error::WebDriverError,117"Unsuccessful command executed: #{command} - Code #{code}\n#{result}\n#{stderr}"118end119end120end # SeleniumManager121end # WebDriver122end # Selenium123124125