Class: Selenium::Server
Relationships & Source Files | |
Namespace Children | |
Exceptions:
| |
Inherits: | Object |
Defined in: | rb/lib/selenium/server.rb |
Overview
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
Constant Summary
-
CL_RESET =
# File 'rb/lib/selenium/server.rb', line 59WebDriver::Platform.windows? ? '' : "\r\e[0K"
Class Method Summary
-
.download(required_version = :latest) ⇒ String
Download the given version of the selenium-server jar and return location.
- .download_server(uri, destination)
-
.get(required_version = :latest, opts = {}) ⇒ Selenium::Server
Download the given version of the selenium-server jar and return instance.
-
.latest
Ask GitHub what the latest selenium-server version is.
- .net_http_start(address)
- .new(jar, opts = {}) ⇒ Server constructor
- .available_assets Internal use only Internal use only
Instance Attribute Summary
-
#background
rw
The Mode of the
Server
:standalone
,#hub
,#node
-
#host
rw
The Mode of the
Server
:standalone
,#hub
,#node
-
#log
rw
The Mode of the
Server
:standalone
,#hub
,#node
-
#port
rw
The Mode of the
Server
:standalone
,#hub
,#node
-
#role
rw
The Mode of the
Server
:standalone
,#hub
,#node
-
#timeout
rw
The Mode of the
Server
:standalone
,#hub
,#node
Instance Method Summary
- #<<(arg)
- #start
- #stop
- #webdriver_url
- #poll_for_service private
- #poll_for_shutdown private
- #process private
- #socket private
- #stop_process private
Constructor Details
.new(jar, opts = {}) ⇒ Server
# File 'rb/lib/selenium/server.rb', line 183
def initialize(jar, opts = {}) raise Errno::ENOENT, jar unless File.exist?(jar) @java = opts.fetch(:java, 'java') @jar = jar @host = '127.0.0.1' @role = opts.fetch(:role, 'standalone') @port = opts.fetch(:port, WebDriver::PortProber.above(4444)) @timeout = opts.fetch(:timeout, 30) @background = opts.fetch(:background, false) @additional_args = opts.fetch(:args, []) @log = opts[:log] if opts[:log_level] @log ||= true @additional_args << '--log-level' @additional_args << opts[:log_level].to_s end @log_file = nil end
Class Method Details
.available_assets
# File 'rb/lib/selenium/server.rb', line 116
def available_assets @available_assets ||= net_http_start('api.github.com') do |http| json = http.get('/repos/seleniumhq/selenium/releases').body all_assets = JSON.parse(json).map { |release| release['assets'] }.flatten server_assets = all_assets.select { |asset| asset['name'].match(/selenium-server-(\d\.\d\.\d+)\.jar/) } server_assets.each_with_object({}) { |asset, hash| hash[asset.delete('name')] = asset } end end
.download(required_version = :latest) ⇒ String
Download the given version of the selenium-server jar and return location
# File 'rb/lib/selenium/server.rb', line 81
def download(required_version = :latest) required_version = latest if required_version == :latest download_file_name = "selenium-server-#{required_version}.jar" return download_file_name if File.exist? download_file_name begin download_location = available_assets[download_file_name]['browser_download_url'] released = Net::HTTP.get_response(URI.parse(download_location)) redirected = URI.parse released.header['location'] File.open(download_file_name, 'wb') do |destination| download_server(redirected, destination) end rescue StandardError FileUtils.rm_rf download_file_name raise end download_file_name end
.download_server(uri, destination)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 137
def download_server(uri, destination) net_http_start('github-releases.githubusercontent.com') do |http| request = Net::HTTP::Get.new uri resp = http.request(request) 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.fdiv(total) * 100 print "#{CL_RESET}Downloading #{destination.path}: #{percent.to_i}% (#{progress} / #{total})" segment_count = 0 end destination.write(segment) end end raise Error, "#{resp.code} for #{destination.path}" unless resp.is_a? Net::HTTPSuccess end end
.get(required_version = :latest, opts = {}) ⇒ Server
Download the given version of the selenium-server jar and return instance
.latest
Ask GitHub what the latest selenium-server version is.
# File 'rb/lib/selenium/server.rb', line 107
def latest @latest ||= begin available = available_assets.keys.map { |key| key[/selenium-server-(\d\.\d\.\d+)\.jar/, 1] } available.map { |asset| Gem::Version.new(asset) }.max.to_s end end
.net_http_start(address)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 125
def net_http_start(address, &) http_proxy = ENV.fetch('http_proxy', nil) || ENV.fetch('HTTP_PROXY', nil) 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, &) else Net::HTTP.start(address, use_ssl: true, &) end end
Instance Attribute Details
#background (rw)
The Mode of the Server
:standalone
, #hub
, #node
#host (rw)
The Mode of the Server
:standalone
, #hub
, #node
# File 'rb/lib/selenium/server.rb', line 169
attr_accessor :role, :host, :port, :timeout, :background, :log
#log (rw)
The Mode of the Server
:standalone
, #hub
, #node
# File 'rb/lib/selenium/server.rb', line 169
attr_accessor :role, :host, :port, :timeout, :background, :log
#port (rw)
The Mode of the Server
:standalone
, #hub
, #node
# File 'rb/lib/selenium/server.rb', line 169
attr_accessor :role, :host, :port, :timeout, :background, :log
#role (rw)
The Mode of the Server
:standalone
, #hub
, #node
# File 'rb/lib/selenium/server.rb', line 169
attr_accessor :role, :host, :port, :timeout, :background, :log
#timeout (rw)
The Mode of the Server
:standalone
, #hub
, #node
# File 'rb/lib/selenium/server.rb', line 169
attr_accessor :role, :host, :port, :timeout, :background, :log
Instance Method Details
#<<(arg)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 222
def <<(arg) if arg.is_a?(Array) @additional_args += arg else @additional_args << arg.to_s end end
#poll_for_service (private)
#poll_for_shutdown (private)
#process (private)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 240
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, @role, '--port', @port.to_s] server_command = [@java] + properties + args + @additional_args cp = WebDriver::ChildProcess.build(*server_command) if @log.is_a?(String) cp.io = @log elsif @log cp.io = :out end cp.detach = @background cp end end
#socket (private)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 272
def socket @socket ||= WebDriver::SocketPoller.new(@host, @port, @timeout) end
#start
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 204
def start process.start poll_for_service process.wait unless @background end
#stop
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 211
def stop stop_process if @process poll_for_shutdown @log_file&.close end
#stop_process (private)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 232
def stop_process @process.stop rescue Errno::ECHILD # already dead ensure @process = nil end
#webdriver_url
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 218
def webdriver_url "http://#{@host}:#{@port}/wd/hub" end