class Watir::Hanami

Constants

VERSION

Attributes

middleware[R]
port[RW]
server[W]

Public Class Methods

app() click to toggle source

Hanami app under test.

@return [Object] Hanami Rack app.

# File lib/watir/hanami.rb, line 96
def app
  @app ||= Rack::Builder.new do
    map "/" do
      run ::Hanami.app
    end
  end.to_app
end
boot(port: nil) click to toggle source

Start the Hanami Will be called automatically by {Watir::Browser#initialize}.

@param [Integer] port port for the Hanami

# File lib/watir/hanami.rb, line 21
def boot(port: nil)
  unless running?
    @middleware = Middleware.new(app)
    @port = port || find_available_port

    @server_thread = Thread.new do
      server.call @middleware, @port
    end

    Timeout.timeout(boot_timeout) { @server_thread.join(0.1) until running? }
  end
rescue Timeout::Error
  raise Timeout::Error, "Hanami Rack application timed out during boot"
end
error() click to toggle source

Error rescued by the middleware.

@return [Exception or NilClass]

# File lib/watir/hanami.rb, line 60
def error
  @middleware.error
end
error=(value) click to toggle source

Set error rescued by the middleware.

@param value

# File lib/watir/hanami.rb, line 74
def error=(value)
  @middleware.error = value
end
host() click to toggle source

Host for Hanami app under test. Default is {.local_host}.

@return [String] Host for Hanami app under test.

# File lib/watir/hanami.rb, line 39
def host
  @host || local_host
end
host=(host) click to toggle source

Set host for Hanami app. Will be used by {Browser#goto} method.

@param [String] host host to use when using {Browser#goto}.

# File lib/watir/hanami.rb, line 46
def host=(host)
  @host = host
end
local_host() click to toggle source

Local host for Hanami app under test.

@return [String] Local host with the value of “127.0.0.1”.

# File lib/watir/hanami.rb, line 53
def local_host
  "127.0.0.1"
end
pending_requests?() click to toggle source

Returns true if there are pending requests to server.

@return [Boolean]

# File lib/watir/hanami.rb, line 67
def pending_requests?
  @middleware.pending_requests?
end
running?() click to toggle source

Check if Hanami app under test is running.

@return [Boolean] true when Hanami app under test is running, false otherwise.

# File lib/watir/hanami.rb, line 81
def running?
  return false if @server_thread && @server_thread.join(0)

  res = Net::HTTP.start(local_host, @port) { |http| http.get('/__identify__') }

  if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
    return res.body == @app.object_id.to_s
  end
rescue Errno::ECONNREFUSED, Errno::EBADF
  return false
end

Private Class Methods

boot_timeout() click to toggle source
# File lib/watir/hanami.rb, line 106
def boot_timeout
  60
end
find_available_port() click to toggle source
# File lib/watir/hanami.rb, line 110
def find_available_port
  server = TCPServer.new(local_host, 0)
  server.addr[1]
ensure
  server.close if server
end
server() click to toggle source
# File lib/watir/hanami.rb, line 117
def server
  @server ||= lambda do |app, port|
    begin
      require 'rack/handler/thin'
      Thin::Logging.silent = true
      return Rack::Handler::Thin.run(app, :Port => port)
    rescue LoadError
    end

    begin
      require 'rack/handler/puma'
      return Rack::Handler::Puma.run(app, :Port => port, :Silent => true)
    rescue LoadError
    end

    require 'rack/handler/webrick'
    Rack::Handler::WEBrick.run(app, :Port => port, :AccessLog => [], :Logger => WEBrick::Log::new(nil, 0))
  end
end