Class: Selenium::Server
- Inherits:
-
Object
- Object
- Selenium::Server
- 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
-
#background ⇒ Object
The Mode of the Server :standalone, #hub, #node.
-
#host ⇒ Object
The Mode of the Server :standalone, #hub, #node.
-
#log ⇒ Object
The Mode of the Server :standalone, #hub, #node.
-
#port ⇒ Object
The Mode of the Server :standalone, #hub, #node.
-
#role ⇒ Object
The Mode of the Server :standalone, #hub, #node.
-
#timeout ⇒ Object
The Mode of the Server :standalone, #hub, #node.
Class Method Summary collapse
-
.download(required_version = :latest) ⇒ String
Download the given version of the selenium-server jar with Selenium Manager 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 Selenium Manager and return its version.
Instance Method Summary collapse
- #<<(arg) ⇒ Object
-
#initialize(jar, opts = {}) ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
- #status_ok? ⇒ Boolean
- #stop ⇒ Object
- #webdriver_url ⇒ Object
Constructor Details
#initialize(jar, opts = {}) ⇒ Server
Returns a new instance of Server.
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
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' = "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 = {}) ⇒ Selenium::Server
Download the given version of the selenium-server jar and return instance
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
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
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 |