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 with ::Selenium Manager:
server = Selenium::Server.get '4.30.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 Method Summary
-
.download(required_version = :latest) ⇒ String
Download the given version of the selenium-server jar with
::SeleniumManager and return location. -
.get(required_version = :latest, opts = {}) ⇒ Selenium::Server
Download the given version of the selenium-server jar and return instance.
-
.latest ⇒ String
Download the latest selenium-server jar with
::SeleniumManager and return its version. - .new(jar, opts = {}) ⇒ Server constructor
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 - #status_ok? ⇒ Boolean readonly
-
#timeout
rw
The Mode of the
Server:standalone,#hub,#node - #socket_connected? ⇒ Boolean readonly private
Instance Method Summary
- #<<(arg)
- #start
- #stop
- #webdriver_url
- #poll_for_ready 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 136
def initialize(jar, opts = {}) raise Errno::ENOENT, jar unless File.exist?(jar) @java = opts.fetch(:java, '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
.download(required_version = :latest) ⇒ String
Download the given version of the selenium-server jar with ::Selenium Manager and return location
# File 'rb/lib/selenium/server.rb', line 82
def download(required_version = :latest) arguments = ['--grid'] unless required_version == :latest download_file_name = "selenium-server-#{required_version}.jar" return download_file_name if File.exist? download_file_name arguments << required_version.to_s end cached_jar = WebDriver::SeleniumManager.binary_paths(*arguments)['driver_path'] if cached_jar.to_s.empty? offline_source = ENV['SE_OFFLINE'] == 'true' ? 'SE_OFFLINE is true' : 'offline is true in se-config.toml' = "Selenium Manager is in offline mode (#{offline_source}) " raise Error, + "and has no cached selenium-server #{required_version}" end download_file_name = File.basename(cached_jar) FileUtils.cp(cached_jar, download_file_name) unless File.exist? download_file_name download_file_name rescue WebDriver::Error::WebDriverError => e raise Error, e. end
.get(required_version = :latest, opts = {}) ⇒ Server
Download the given version of the selenium-server jar and return instance
.latest ⇒ String
Download the latest selenium-server jar with ::Selenium Manager and return its version
# File 'rb/lib/selenium/server.rb', line 112
def latest download[/selenium-server-(\d\.\d\.\d+)\.jar/, 1] 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 122
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 122
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 122
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 122
attr_accessor :role, :host, :port, :timeout, :background, :log
#socket_connected? ⇒ Boolean (readonly, private)
[ GitHub ]
# File 'rb/lib/selenium/server.rb', line 239
def socket_connected? @socket_connected ||= socket.connected? end
#status_ok? ⇒ Boolean (readonly)
[ GitHub ]
# File 'rb/lib/selenium/server.rb', line 175
def status_ok? return false unless @process&.alive? && socket_connected? Net::HTTP.start(@host, @port, open_timeout: 2, read_timeout: 2) do |http| response = http.get('/status') return false unless response.is_a?(Net::HTTPSuccess) status = JSON.parse(response.body) status.dig('value', 'ready') == true end rescue StandardError false end
#timeout (rw)
The Mode of the Server
:standalone, #hub, #node
# File 'rb/lib/selenium/server.rb', line 122
attr_accessor :role, :host, :port, :timeout, :background, :log
Instance Method Details
#<<(arg)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 189
def <<(arg) if arg.is_a?(Array) @additional_args += arg else @additional_args << arg.to_s end end
#poll_for_ready (private)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 227
def poll_for_ready start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) loop do return if status_ok? elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time raise Error, "remote server not ready in #{@timeout} seconds" if elapsed > @timeout sleep 0.5 end end
#poll_for_shutdown (private)
#process (private)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 207
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 249
def socket @socket ||= WebDriver::SocketPoller.new(@host, @port, @timeout) end
#start
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 157
def start process.start poll_for_ready process.wait unless @background end
#stop
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 164
def stop stop_process if @process poll_for_shutdown @log_file&.close end
#stop_process (private)
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 199
def stop_process @process.stop rescue Errno::ECHILD # already dead ensure @process = nil end
#webdriver_url
[ GitHub ]# File 'rb/lib/selenium/server.rb', line 171
def webdriver_url "http://#{@host}:#{@port}/wd/hub" end