Skip to content

Commit

Permalink
[rb] fix downloading of Selenium Server
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Oct 19, 2021
1 parent a25ad32 commit e1292c8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 65 deletions.
74 changes: 44 additions & 30 deletions rb/lib/selenium/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,41 @@ class Error < StandardError; end

CL_RESET = WebDriver::Platform.windows? ? '' : "\r\e[0K"

def self.get(required_version, opts = {})
new(download(required_version), opts)
end
class << self
#
# Download the given version of the selenium-server jar and return instance
#
# @param [String, Symbol] required_version X.Y.Z defaults to ':latest'
# @param [Hash] opts
# @return [Selenium::Server]
#

#
# Download the given version of the selenium-server-standalone jar.
#
def get(required_version = :latest, opts = {})
new(download(required_version), opts)
end

class << self
def download(required_version)
#
# Download the given version of the selenium-server jar and return location
#
# @param [String, Symbol] required_version X.Y.Z defaults to ':latest'
# @return [String] location of downloaded file
#

def download(required_version = :latest)
required_version = latest if required_version == :latest
download_file_name = "selenium-server-standalone-#{required_version}.jar"
download_file_name = "selenium-server-#{required_version}.jar"

return download_file_name if File.exist? download_file_name

begin
server = 'https://github.com/seleniumhq/selenium/releases/download'
released = Net::HTTP.get_response(URI.parse("#{server}/selenium-#{required_version}/#{download_file_name}"))
redirected = URI.parse released.header['location']

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|
net_http_start('github-releases.githubusercontent.com') do |http|
request = Net::HTTP::Get.new redirected
resp = http.request(request) do |response|
total = response.content_length
progress = 0
segment_count = 0
Expand Down Expand Up @@ -106,30 +122,35 @@ def download(required_version)
end

#
# Ask Google Code what the latest selenium-server-standalone version is.
# Ask GitHub what the latest selenium-server 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
@latest ||= begin
net_http_start('api.github.com') do |http|
json = http.get('/repos/seleniumhq/selenium/releases').body
JSON.parse(json).map { |release|
release['assets']
}.flatten.map { |asset|
asset['name'][/selenium-server-(\d+\.\d+\.\d+)\.jar/, 1]
}.compact.map { |version|
Gem::Version.new(version)
}.max.version
end
end
end

# @api private

def net_http_start(address, &block)
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.start(address, nil, uri.host, uri.port, &block)
else
Net::HTTP.start(address, &block)
Net::HTTP.start(address, use_ssl: true, &block)
end
end
end
Expand Down Expand Up @@ -200,10 +221,6 @@ def <<(arg)

private

def selenium4?
@jar.match?(/[^.]4\./) || @jar.include?('deploy')
end

def stop_process
return unless @process.alive?

Expand All @@ -222,10 +239,7 @@ 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/] }
args = ['-jar', @jar]
args << @role if selenium4?
args << (selenium4? ? '--port' : '-port')
args << @port.to_s
args = ['-jar', @jar, @role, '--port', @port.to_s]
server_command = ['java'] + properties + args + @additional_args
cp = ChildProcess.build(*server_command)
WebDriver.logger.debug("Executing Process #{server_command}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def remote_server_jar
elsif File.exist?(built_jar) && ENV['DOWNLOAD_SERVER'].nil?
built_jar
else
Selenium::Server.download(:latest)
Selenium::Server.download
end

WebDriver.logger.info "Server Location: #{jar}"
Expand Down
56 changes: 22 additions & 34 deletions rb/spec/unit/selenium/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ module Selenium
end

it 'downloads the specified version from the selenium site' do
required_version = '10.2.0'
expected_download_file_name = "selenium-server-standalone-#{required_version}.jar"
required_version = '10.0.0'
expected_download_file_name = "selenium-server-#{required_version}.jar"
download = 'https://github.com/seleniumhq/selenium/releases/download'

stub_request(:get, 'http://selenium-release.storage.googleapis.com/10.2/selenium-server-standalone-10.2.0.jar')
stub_request(:get, "#{download}/selenium-10.0.0/#{expected_download_file_name}")
.to_return(body: 'this is pretending to be a jar file for testing purposes')

begin
Expand Down Expand Up @@ -154,7 +155,7 @@ module Selenium

it 'only downloads a jar if it is not present in the current directory' do
required_version = '10.2.0'
expected_download_file_name = "selenium-server-standalone-#{required_version}.jar"
expected_download_file_name = "selenium-server-#{required_version}.jar"

allow(File).to receive(:exist?).with(expected_download_file_name).and_return true

Expand All @@ -163,36 +164,23 @@ module Selenium
end

it 'should know what the latest version available is' do
latest_version = '2.42.2'
example_xml = +"<?xml version='1.0' encoding='UTF-8'?><ListBucketResult "
example_xml << "xmlns='http://doc.s3.amazonaws.com/2006-03-01'><Name>"
example_xml << 'selenium-release</Name><Contents><Key>2.39/'
example_xml << 'selenium-server-2.39.0.zip</Key></Contents><Contents>'
example_xml << "<Key>2.42/selenium-server-standalone-#{latest_version}.jar"
example_xml << '</Key></Contents></ListBucketResult>'
stub_request(:get, 'http://selenium-release.storage.googleapis.com/').to_return(body: example_xml)

expect(Selenium::Server.latest).to eq(latest_version)
end

it 'should download the latest version if that has been specified' do
required_version = '2.42.2'
minor_version = '2.42'
expected_download_file_name = "selenium-server-standalone-#{required_version}.jar"

allow(Selenium::Server).to receive(:latest).and_return required_version
stub_request(:get,
"http://selenium-release.storage.googleapis.com/#{minor_version}/#{expected_download_file_name}")
.to_return(body: 'this is pretending to be a jar file for testing purposes')

begin
actual_download_file_name = Selenium::Server.download(:latest)
expect(actual_download_file_name).to eq(expected_download_file_name)
expect(File).to exist(expected_download_file_name)
expect(Selenium::Server).to have_received(:latest)
ensure
FileUtils.rm_rf expected_download_file_name
end
expected_latest = '10.0.1'
repo = 'https://api.github.com/repos/seleniumhq/selenium/releases'
example_json = [{"url": "#{repo}/41272273",
"assets": {
"name": 'selenium-server-3.141.59.jar',
"browser_download_url": "#{repo}/selenium-3.141.59/selenium-server-standalone-3.141.59.jar"
}},
{"url": "#{repo}/51272273",
"assets": {
"name": 'selenium-server-10.0.1.jar',
"browser_download_url": "#{repo}/selenium-10.0.1/selenium-server-10.0.1.jar"
}}
]

stub_request(:get, repo).to_return(body: example_json.to_json)

expect(Selenium::Server.latest).to eq(expected_latest)
end

it 'raises Selenium::Server::Error if the server is not launched within the timeout' do
Expand Down

0 comments on commit e1292c8

Please sign in to comment.