1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
# frozen_string_literal: true
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require 'childprocess'
require 'selenium/webdriver/common/socket_poller'
require 'net/http'
module Selenium
#
# Wraps the remote server jar
#
# Usage:
#
# server = Selenium::Server.new('/path/to/selenium-server-standalone.jar')
# server.start
#
# Automatically download the given version:
#
# server = Selenium::Server.get '2.6.0'
# server.start
#
# or the latest version:
#
# server = Selenium::Server.get :latest
# server.start
#
# Run the server in the background:
#
# server = Selenium::Server.new(jar, :background => true)
# server.start
#
# Add additional arguments:
#
# server = Selenium::Server.new(jar)
# server << ["--additional", "args"]
# server.start
#
class Server
class Error < StandardError; end
CL_RESET = WebDriver::Platform.windows? ? '' : "\r\e[0K"
def self.get(required_version, opts = {})
new(download(required_version), opts)
end
#
# Download the given version of the selenium-server-standalone jar.
#
class << self
def download(required_version)
required_version = latest if required_version == :latest
download_file_name = "selenium-server-standalone-#{required_version}.jar"
return download_file_name if File.exist? download_file_name
begin
File.open(download_file_name, 'wb') do |destination|
net_http.start('selenium-release.storage.googleapis.com') do |http|
resp = http.request_get("/#{required_version[/(\d+\.\d+)\./, 1]}/#{download_file_name}") do |response|
total = response.content_length
progress = 0
segment_count = 0
response.read_body do |segment|
progress += segment.length
segment_count += 1
if (segment_count % 15).zero?
percent = (progress.to_f / total.to_f) * 100
print "#{CL_RESET}Downloading #{download_file_name}: #{percent.to_i}% (#{progress} / #{total})"
segment_count = 0
end
destination.write(segment)
end
end
raise Error, "#{resp.code} for #{download_file_name}" unless resp.is_a? Net::HTTPSuccess
end
end
rescue
FileUtils.rm download_file_name if File.exist? download_file_name
raise
end
download_file_name
end
#
# Ask Google Code what the latest selenium-server-standalone version is.
#
def latest
require 'rexml/document'
net_http.start('selenium-release.storage.googleapis.com') do |http|
versions = REXML::Document.new(http.get('/').body).root.get_elements('//Contents/Key').map do |e|
e.text[/selenium-server-standalone-(\d+\.\d+\.\d+)\.jar/, 1]
end
versions.compact.map { |version| Gem::Version.new(version) }.max.version
end
end
def net_http
http_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
if http_proxy
http_proxy = "http://#{http_proxy}" unless http_proxy.start_with?('http://')
uri = URI.parse(http_proxy)
Net::HTTP::Proxy(uri.host, uri.port)
else
Net::HTTP
end
end
end
#
# The server port
#
attr_accessor :port
#
# The server timeout
#
attr_accessor :timeout
#
# Whether to launch the server in the background
#
attr_accessor :background
#
# Path to log file, or 'true' for stdout.
#
attr_accessor :log
#
# @param [String] jar Path to the server jar.
# @param [Hash] opts the options to create the server process with
#
# @option opts [Integer] :port Port the server should listen on (default: 4444).
# @option opts [Integer] :timeout Seconds to wait for server launch/shutdown (default: 30)
# @option opts [true,false] :background Run the server in the background (default: false)
# @option opts [true,false,String] :log Either a path to a log file,
# or true to pass server log to stdout.
# @raise [Errno::ENOENT] if the jar file does not exist
#
def initialize(jar, opts = {})
raise Errno::ENOENT, jar unless File.exist?(jar)
@jar = jar
@host = '127.0.0.1'
@port = opts.fetch(:port, 4444)
@timeout = opts.fetch(:timeout, 30)
@background = opts.fetch(:background, false)
@log = opts[:log]
@additional_args = []
end
def start
process.start
poll_for_service
process.wait unless @background
end
def stop
begin
Net::HTTP.get(@host, '/selenium-server/driver/?cmd=shutDownSeleniumServer', @port)
rescue Errno::ECONNREFUSED
end
stop_process if @process
poll_for_shutdown
@log_file&.close
end
def webdriver_url
"http://#{@host}:#{@port}/wd/hub"
end
def <<(arg)
if arg.is_a?(Array)
@additional_args += arg
else
@additional_args << arg.to_s
end
end
private
def stop_process
return unless @process.alive?
begin
@process.poll_for_exit(5)
rescue ChildProcess::TimeoutError
@process.stop
end
rescue Errno::ECHILD
# already dead
ensure
@process = nil
end
def process
@process ||= begin
# extract any additional_args that start with -D as options
properties = @additional_args.dup - @additional_args.delete_if { |arg| arg[/^-D/] }
server_command = ['java'] + properties + ['-jar', @jar, '-port', @port.to_s] + @additional_args
cp = ChildProcess.build(*server_command)
WebDriver.logger.debug("Executing Process #{server_command}")
io = cp.io
if @log.is_a?(String)
@log_file = File.open(@log, 'w')
io.stdout = io.stderr = @log_file
elsif @log
io.inherit!
end
cp.detach = @background
cp
end
end
def poll_for_service
return if socket.connected?
raise Error, "remote server not launched in #{@timeout} seconds"
end
def poll_for_shutdown
return if socket.closed?
raise Error, "remote server not stopped in #{@timeout} seconds"
end
def socket
@socket ||= WebDriver::SocketPoller.new(@host, @port, @timeout)
end
end # Server
end # Selenium
|