Class: Selenium::Server

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

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.

Raises:

  • (Errno::ENOENT)

    if the jar file does not exist



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/selenium/server.rb', line 173

def initialize(jar, opts = {})
  raise Errno::ENOENT, jar unless File.exist?(jar)

  @jar        = jar
  @host       = '127.0.0.1'
  @port       = opts.fetch(:port, 4444)
  @timeout    = opts.fetch(:timeout, 30)
  @background = opts.fetch(:background, false)
  @log        = opts[:log]

  @additional_args = []
end

Instance Attribute Details

#backgroundObject

Whether to launch the server in the background



153
154
155
# File 'lib/selenium/server.rb', line 153

def background
  @background
end

#logObject

Path to log file, or ‘true’ for stdout.



159
160
161
# File 'lib/selenium/server.rb', line 159

def log
  @log
end

#portObject

The server port



141
142
143
# File 'lib/selenium/server.rb', line 141

def port
  @port
end

#timeoutObject

The server timeout



147
148
149
# File 'lib/selenium/server.rb', line 147

def timeout
  @timeout
end

Class Method Details

.download(required_version) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/selenium/server.rb', line 69

def download(required_version)
  required_version = latest if required_version == :latest
  download_file_name = "selenium-server-standalone-#{required_version}.jar"

  return download_file_name if File.exist? download_file_name

  begin
    open(download_file_name, 'wb') do |destination|
      net_http.start('selenium-release.storage.googleapis.com') do |http|
        resp = http.request_get("/#{required_version[/(\d+\.\d+)\./, 1]}/#{download_file_name}") 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 == 0
              percent = (progress.to_f / total.to_f) * 100
              print "#{CL_RESET}Downloading #{download_file_name}: #{percent.to_i}% (#{progress} / #{total})"
              segment_count = 0
            end

            destination.write(segment)
          end
        end

        unless resp.is_a? Net::HTTPSuccess
          raise Error, "#{resp.code} for #{download_file_name}"
        end
      end
    end
  rescue
    FileUtils.rm download_file_name if File.exist? download_file_name
    raise
  end

  download_file_name
end

.get(required_version, opts = {}) ⇒ Object



60
61
62
# File 'lib/selenium/server.rb', line 60

def self.get(required_version, opts = {})
  new(download(required_version), opts)
end

.latestObject

Ask Google Code what the latest selenium-server-standalone version is.



114
115
116
117
118
119
120
121
# File 'lib/selenium/server.rb', line 114

def latest
  require 'rexml/document'
  net_http.start('selenium-release.storage.googleapis.com') do |http|
    REXML::Document.new(http.get('/').body).root.get_elements('//Contents/Key').map do |e|
      e.text[/selenium-server-standalone-(\d+\.\d+\.\d+)\.jar/, 1]
    end.compact.max
  end
end

.net_httpObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/selenium/server.rb', line 123

def net_http
  http_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']

  if http_proxy
    http_proxy = "http://#{http_proxy}" unless http_proxy.start_with?('http://')
    uri = URI.parse(http_proxy)

    Net::HTTP::Proxy(uri.host, uri.port)
  else
    Net::HTTP
  end
end

Instance Method Details

#<<(arg) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/selenium/server.rb', line 209

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

#startObject



186
187
188
189
190
191
# File 'lib/selenium/server.rb', line 186

def start
  process.start
  poll_for_service

  process.wait unless @background
end

#stopObject



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/selenium/server.rb', line 193

def stop
  begin
    Net::HTTP.get(@host, '/selenium-server/driver/?cmd=shutDownSeleniumServer', @port)
  rescue Errno::ECONNREFUSED
  end

  stop_process if @process
  poll_for_shutdown

  @log_file.close if @log_file
end

#webdriver_urlObject



205
206
207
# File 'lib/selenium/server.rb', line 205

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