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