Class: Selenium::Server

Inherits:
Object
  • Object
show all
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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jar, opts = {}) ⇒ Server

Returns a new instance of Server.

Parameters:

  • jar (String) —

    Path to the server jar.

  • opts (Hash) (defaults to: {}) —

    the options to create the server process with

Options Hash (opts):

  • :port (Integer) —

    Port the server should listen on (default: 4444).

  • :timeout (Integer) —

    Seconds to wait for server launch/shutdown (default: 30)

  • :background (true, false) —

    Run the server in the background (default: false)

  • :log (true, false, String) —

    Either a path to a log file, or true to pass server log to stdout.

  • :env (Hash<String, String, nil>) —

    Environment variables for the server process, merged into the inherited environment.

Raises:

  • (Errno::ENOENT) —

    if the jar file does not exist



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'rb/lib/selenium/server.rb', line 138

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]
  @env = opts[:env]
  if opts[:log_level]
    @log ||= true
    @additional_args << '--log-level'
    @additional_args << opts[:log_level].to_s
  end

  @log_file = nil
end

Instance Attribute Details

#background ⇒ Object

The Mode of the Server :standalone, #hub, #node



122
123
124
# File 'rb/lib/selenium/server.rb', line 122

def background
  @background
end

#host ⇒ Object

The Mode of the Server :standalone, #hub, #node



122
123
124
# File 'rb/lib/selenium/server.rb', line 122

def host
  @host
end

#log ⇒ Object

The Mode of the Server :standalone, #hub, #node



122
123
124
# File 'rb/lib/selenium/server.rb', line 122

def log
  @log
end

#port ⇒ Object

The Mode of the Server :standalone, #hub, #node



122
123
124
# File 'rb/lib/selenium/server.rb', line 122

def port
  @port
end

#role ⇒ Object

The Mode of the Server :standalone, #hub, #node



122
123
124
# File 'rb/lib/selenium/server.rb', line 122

def role
  @role
end

#timeout ⇒ Object

The Mode of the Server :standalone, #hub, #node



122
123
124
# File 'rb/lib/selenium/server.rb', line 122

def timeout
  @timeout
end

Class Method Details

.download(required_version = :latest) ⇒ String

Download the given version of the selenium-server jar with Selenium Manager and return location

Parameters:

  • required_version (String, Symbol) (defaults to: :latest) —

    X.Y.Z defaults to ':latest'

Returns:

  • (String) —

    location of downloaded file

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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'
    message = "Selenium Manager is in offline mode (#{offline_source}) "
    raise Error, message + "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.message
end

.get(required_version = :latest, opts = {}) ⇒ Selenium::Server

Download the given version of the selenium-server jar and return instance

Parameters:

  • required_version (String, Symbol) (defaults to: :latest) —

    X.Y.Z defaults to ':latest'

  • opts (Hash) (defaults to: {})

Returns:



70
71
72
# File 'rb/lib/selenium/server.rb', line 70

def get(required_version = :latest, opts = {})
  new(download(required_version), opts)
end

.latest ⇒ String

Download the latest selenium-server jar with Selenium Manager and return its version

Returns:

  • (String) —

    X.Y.Z



112
113
114
# File 'rb/lib/selenium/server.rb', line 112

def latest
  download[/selenium-server-(\d+\.\d+\.\d+)\.jar/, 1]
end

Instance Method Details

#<<(arg) ⇒ Object



192
193
194
195
196
197
198
# File 'rb/lib/selenium/server.rb', line 192

def <<(arg)
  if arg.is_a?(Array)
    @additional_args += arg
  else
    @additional_args << arg.to_s
  end
end

#start ⇒ Object



160
161
162
163
164
165
# File 'rb/lib/selenium/server.rb', line 160

def start
  process.start
  poll_for_ready

  process.wait unless @background
end

#status_ok? ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'rb/lib/selenium/server.rb', line 178

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

#stop ⇒ Object



167
168
169
170
171
172
# File 'rb/lib/selenium/server.rb', line 167

def stop
  stop_process if @process
  poll_for_shutdown

  @log_file&.close
end

#webdriver_url ⇒ Object



174
175
176
# File 'rb/lib/selenium/server.rb', line 174

def webdriver_url
  "http://#{@host}:#{@port}/wd/hub"
end