class Watir::Rails

Constants

VERSION

Attributes

ignore_exceptions[W]
middleware[R]
port[R]
server[W]

Public Class Methods

app() click to toggle source

Rails app under test.

@return [Object] Rails Rack app.

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

Start the Rails server for tests. Will be called automatically by {Watir::Browser#initialize}.

@param [Integer] port port for the Rails up to run on. If omitted use

previously selected port or select random available port.
# File lib/watir/rails.rb, line 23
def boot(port: nil)
  @port = port || @port || find_available_port

  unless running?
    @middleware = Middleware.new(app)

    @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, "Rails Rack application timed out during boot"
end
error() click to toggle source

Error rescued by the middleware.

@return [Exception or NilClass]

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

Set error rescued by the middleware.

@param value

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

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

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

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

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

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

# File lib/watir/rails.rb, line 49
def host=(host)
  @host = host
end
ignore_exceptions?() click to toggle source

Check if Rails exceptions should be ignored. Defaults to false.

@return [Boolean] true if exceptions should be ignored, false otherwise.

# File lib/watir/rails.rb, line 84
def ignore_exceptions?
  if @ignore_exceptions.nil?
    if ::Rails.application.config.action_dispatch.show_exceptions
      warn '[WARN] "action_dispatch.show_exceptions" is set to "true", disabling watir-rails exception catcher.'
      @ignore_exceptions = true
    end
  end

  !!@ignore_exceptions
end
local_host() click to toggle source

Local host for Rails app under test.

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

# File lib/watir/rails.rb, line 56
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/rails.rb, line 70
def pending_requests?
  @middleware.pending_requests?
end
running?() click to toggle source

Check if Rails app under test is running.

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

# File lib/watir/rails.rb, line 98
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, EOFError
  return false
end

Private Class Methods

boot_timeout() click to toggle source
# File lib/watir/rails.rb, line 123
def boot_timeout
  60
end
find_available_port() click to toggle source
# File lib/watir/rails.rb, line 127
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/rails.rb, line 134
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