Class: Selenium::Server
- Inherits:
-
Object
- Object
- Selenium::Server
- Defined in:
- 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
Defined Under Namespace
Classes: Error
Constant Summary collapse
- CL_RESET =
WebDriver::Platform.windows? ? '' : "\r\e[0K"
Instance Attribute Summary collapse
-
#background ⇒ 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
- .available_assets ⇒ Object private
-
.download(required_version = :latest) ⇒ String
Download the given version of the selenium-server jar and return location.
- .download_server(uri, destination) ⇒ Object
-
.get(required_version = :latest, opts = {}) ⇒ Selenium::Server
Download the given version of the selenium-server jar and return instance.
-
.latest ⇒ Object
Ask GitHub what the latest selenium-server version is.
- .net_http_start(address, &block) ⇒ Object
Instance Method Summary collapse
- #<<(arg) ⇒ Object
-
#initialize(jar, opts = {}) ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
- #stop ⇒ Object
- #webdriver_url ⇒ Object
Constructor Details
#initialize(jar, opts = {}) ⇒ Server
Returns a new instance of Server.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/selenium/server.rb', line 182 def initialize(jar, opts = {}) raise Errno::ENOENT, jar unless File.exist?(jar) @jar = jar @host = '127.0.0.1' @role = opts.fetch(:role, 'standalone') @port = opts.fetch(:port, 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 |
Instance Attribute Details
#background ⇒ Object
The Mode of the Server :standalone, #hub, #node
168 169 170 |
# File 'lib/selenium/server.rb', line 168 def background @background end |
#log ⇒ Object
The Mode of the Server :standalone, #hub, #node
168 169 170 |
# File 'lib/selenium/server.rb', line 168 def log @log end |
#port ⇒ Object
The Mode of the Server :standalone, #hub, #node
168 169 170 |
# File 'lib/selenium/server.rb', line 168 def port @port end |
#role ⇒ Object
The Mode of the Server :standalone, #hub, #node
168 169 170 |
# File 'lib/selenium/server.rb', line 168 def role @role end |
#timeout ⇒ Object
The Mode of the Server :standalone, #hub, #node
168 169 170 |
# File 'lib/selenium/server.rb', line 168 def timeout @timeout end |
Class Method Details
.available_assets ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
115 116 117 118 119 120 121 122 |
# File 'lib/selenium/server.rb', line 115 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/selenium/server.rb', line 80 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) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/selenium/server.rb', line 136 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 = {}) ⇒ Selenium::Server
Download the given version of the selenium-server jar and return instance
69 70 71 |
# File 'lib/selenium/server.rb', line 69 def get(required_version = :latest, opts = {}) new(download(required_version), opts) end |
.latest ⇒ Object
Ask GitHub what the latest selenium-server version is.
106 107 108 109 110 111 |
# File 'lib/selenium/server.rb', line 106 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, &block) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/selenium/server.rb', line 124 def net_http_start(address, &block) 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, &block) else Net::HTTP.start(address, use_ssl: true, &block) end end |
Instance Method Details
#<<(arg) ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/selenium/server.rb', line 226 def <<(arg) if arg.is_a?(Array) @additional_args += arg else @additional_args << arg.to_s end end |
#start ⇒ Object
202 203 204 205 206 207 |
# File 'lib/selenium/server.rb', line 202 def start process.start poll_for_service process.wait unless @background end |
#stop ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/selenium/server.rb', line 209 def stop begin Net::HTTP.get(@host, '/selenium-server/driver/?cmd=shutDownSeleniumServer', @port) rescue Errno::ECONNREFUSED nil end stop_process if @process poll_for_shutdown @log_file&.close end |
#webdriver_url ⇒ Object
222 223 224 |
# File 'lib/selenium/server.rb', line 222 def webdriver_url "http://#{@host}:#{@port}/wd/hub" end |