Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/platform.rb
3987 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 'rbconfig'
21
require 'socket'
22
23
module Selenium
24
module WebDriver
25
# @api private
26
module Platform
27
module_function
28
29
def home
30
@home ||= Dir.home
31
end
32
33
def engine
34
@engine ||= RUBY_ENGINE.to_sym
35
end
36
37
def os
38
host_os = RbConfig::CONFIG['host_os']
39
@os ||= case host_os
40
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
41
:windows
42
when /darwin|mac os/
43
:macosx
44
when /linux/
45
:linux
46
when /solaris|bsd/
47
:unix
48
else
49
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
50
end
51
end
52
53
def ci
54
if ENV['JENKINS']
55
:jenkins
56
elsif ENV['APPVEYOR']
57
:appveyor
58
elsif ENV['GITHUB_ACTIONS']
59
:github
60
end
61
end
62
63
def jruby?
64
engine == :jruby
65
end
66
67
def truffleruby?
68
engine == :truffleruby
69
end
70
71
def ruby_version
72
RUBY_VERSION
73
end
74
75
def windows?
76
os == :windows
77
end
78
79
def mac?
80
os == :macosx
81
end
82
83
def linux?
84
os == :linux
85
end
86
87
def unix?
88
os == :unix
89
end
90
91
def wsl?
92
return false unless linux?
93
94
File.read('/proc/version').downcase.include?('microsoft')
95
rescue Errno::EACCES
96
# the file cannot be accessed on Linux on DeX
97
false
98
end
99
100
def cygwin?
101
RUBY_PLATFORM.include?('cygwin')
102
end
103
104
def null_device
105
File::NULL
106
end
107
108
def wrap_in_quotes_if_necessary(str)
109
windows? && !cygwin? ? %("#{str}") : str
110
end
111
112
def cygwin_path(path, only_cygwin: false, **opts)
113
return path if only_cygwin && !cygwin?
114
115
flags = []
116
opts.each { |k, v| flags << "--#{k}" if v }
117
118
`cygpath #{flags.join ' '} "#{path}"`.strip
119
end
120
121
def unix_path(path)
122
path.tr(File::ALT_SEPARATOR, File::SEPARATOR)
123
end
124
125
def windows_path(path)
126
path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
127
end
128
129
def make_writable(file)
130
File.chmod 0o766, file
131
end
132
133
def assert_file(path)
134
return if File.file? path
135
136
raise Error::WebDriverError, "not a file: #{path.inspect}"
137
end
138
139
def assert_executable(path)
140
assert_file(path)
141
142
return if File.executable? path
143
144
raise Error::WebDriverError, "not executable: #{path.inspect}"
145
end
146
147
def exit_hook
148
pid = Process.pid
149
150
at_exit { yield if Process.pid == pid }
151
end
152
153
def localhost
154
info = Socket.getaddrinfo 'localhost', 80, Socket::AF_INET, Socket::SOCK_STREAM
155
156
return info[0][3] unless info.empty?
157
158
raise Error::WebDriverError, "unable to translate 'localhost' for TCP + IPv4"
159
end
160
161
def ip
162
orig = Socket.do_not_reverse_lookup
163
Socket.do_not_reverse_lookup = true
164
165
begin
166
UDPSocket.open do |s|
167
s.connect '8.8.8.8', 53
168
return s.addr.last
169
end
170
ensure
171
Socket.do_not_reverse_lookup = orig
172
end
173
rescue Errno::ENETUNREACH, Errno::EHOSTUNREACH
174
# no external ip
175
end
176
177
def interfaces
178
interfaces = Socket.getaddrinfo('localhost', 8080).map { |e| e[3] }
179
interfaces += ['0.0.0.0', Platform.ip]
180
181
interfaces.compact.uniq
182
end
183
end # Platform
184
end # WebDriver
185
end # Selenium
186
187