Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/selenium_manager.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require 'open3'
21
22
module Selenium
23
module WebDriver
24
#
25
# Wrapper for getting information from the Selenium Manager binaries.
26
# This implementation is still in beta, and may change.
27
# @api private
28
#
29
class SeleniumManager
30
class << self
31
attr_writer :bin_path
32
33
def bin_path
34
@bin_path ||= '../../../../../bin'
35
end
36
37
# @param [Array] arguments what gets sent to to Selenium Manager binary.
38
# @return [Hash] paths to the requested assets.
39
def binary_paths(*arguments)
40
arguments += %w[--language-binding ruby]
41
arguments += %w[--output json]
42
arguments << '--debug' if WebDriver.logger.debug?
43
44
run(binary, *arguments)
45
end
46
47
private
48
49
# @return [String] the path to the correct selenium manager
50
def binary
51
@binary ||= begin
52
if (location = ENV.fetch('SE_MANAGER_PATH', nil))
53
WebDriver.logger.debug("Selenium Manager set by ENV['SE_MANAGER_PATH']: #{location}")
54
end
55
location ||= platform_location
56
57
Platform.assert_executable(location)
58
WebDriver.logger.debug("Selenium Manager binary found at #{location}", id: :selenium_manager)
59
location
60
end
61
end
62
63
def run(*command)
64
stdout, stderr, status = execute_command(*command)
65
result = parse_result_and_log(stdout)
66
67
validate_command_result(command, status, result, stderr)
68
69
result
70
end
71
72
def platform_location
73
directory = File.expand_path(bin_path, __FILE__)
74
if Platform.windows?
75
"#{directory}/windows/selenium-manager.exe"
76
elsif Platform.mac?
77
"#{directory}/macos/selenium-manager"
78
elsif Platform.linux?
79
"#{directory}/linux/selenium-manager"
80
elsif Platform.unix?
81
WebDriver.logger.warn('Selenium Manager binary may not be compatible with Unix',
82
id: %i[selenium_manager unix_binary])
83
"#{directory}/linux/selenium-manager"
84
else
85
raise Error::WebDriverError, "unsupported platform: #{Platform.os}"
86
end
87
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
120
end
121
end # SeleniumManager
122
end # WebDriver
123
end # Selenium
124
125