Class: Watir::Rails

Inherits:
Object
  • Object
show all
Defined in:
lib/watir/rails.rb,
lib/watir/rails/version.rb,
lib/watir/rails/middleware.rb

Constant Summary collapse

VERSION =
"2.3.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ignore_exceptions=(value) (writeonly)

Sets the attribute ignore_exceptions

Parameters:

  • value

    the value to set the attribute ignore_exceptions to.



16
17
18
# File 'lib/watir/rails.rb', line 16

def ignore_exceptions=(value)
  @ignore_exceptions = value
end

.middleware (readonly)

Returns the value of attribute middleware.



15
16
17
# File 'lib/watir/rails.rb', line 15

def middleware
  @middleware
end

.port (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/watir/rails.rb', line 15

def port
  @port
end

.server=(value)

Sets the attribute server

Parameters:

  • value

    the value to set the attribute server to.



16
17
18
# File 'lib/watir/rails.rb', line 16

def server=(value)
  @server = value
end

Class Method Details

.appObject

Rails app under test.

Returns:

  • (Object)

    Rails Rack app.



113
114
115
116
117
118
119
# 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)

Start the Rails server for tests. Will be called automatically by Browser#initialize.

Parameters:

  • port (Integer) (defaults to: nil)

    port for the Rails up to run on. If omitted use previously selected port or select random available port.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

.errorException or NilClass

Error rescued by the middleware.

Returns:

  • (Exception or NilClass)


63
64
65
# File 'lib/watir/rails.rb', line 63

def error
  @middleware.error
end

.error=(value)

Set error rescued by the middleware.

Parameters:

  • value


77
78
79
# File 'lib/watir/rails.rb', line 77

def error=(value)
  @middleware.error = value
end

.hostString

Host for Rails app under test. Default is local_host.

Returns:

  • (String)

    Host for Rails app under test.



42
43
44
# File 'lib/watir/rails.rb', line 42

def host
  @host || local_host
end

.host=(host)

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

Parameters:



49
50
51
# File 'lib/watir/rails.rb', line 49

def host=(host)
  @host = host
end

.ignore_exceptions?Boolean

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

Returns:

  • (Boolean)

    true if exceptions should be ignored, false otherwise.



84
85
86
87
88
89
90
91
92
93
# 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_hostString

Local host for Rails app under test.

Returns:

  • (String)

    Local host with the value of “127.0.0.1”.



56
57
58
# File 'lib/watir/rails.rb', line 56

def local_host
  "127.0.0.1"
end

.pending_requests?Boolean

Returns true if there are pending requests to server.

Returns:

  • (Boolean)


70
71
72
# File 'lib/watir/rails.rb', line 70

def pending_requests?
  @middleware.pending_requests?
end

.running?Boolean

Check if Rails app under test is running.

Returns:

  • (Boolean)

    true when Rails app under test is running, false otherwise.



98
99
100
101
102
103
104
105
106
107
108
# 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