Class: Watir::Rails

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

Constant Summary collapse

VERSION =
"2.2.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.



21
22
23
# File 'lib/watir/rails.rb', line 21

def ignore_exceptions=(value)
  @ignore_exceptions = value
end

.middleware (readonly)

Returns the value of attribute middleware.



20
21
22
# File 'lib/watir/rails.rb', line 20

def middleware
  @middleware
end

.port (readonly)

Returns the value of attribute port.



20
21
22
# File 'lib/watir/rails.rb', line 20

def port
  @port
end

.server=(value)

Sets the attribute server

Parameters:

  • value

    the value to set the attribute server to.



21
22
23
# File 'lib/watir/rails.rb', line 21

def server=(value)
  @server = value
end

Class Method Details

.appObject

Rails app under test.

Returns:

  • (Object)

    Rails Rack app.



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

def app
  legacy = legacy_rails?
  @app ||= Rack::Builder.new do
    map "/" do
      if legacy
        use ::Rails::Rack::Static
        run ActionController::Dispatcher.new
      else
        run ::Rails.application
      end
    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 random port will be picked.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/watir/rails.rb', line 27

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, "Rails Rack application timed out during boot"
end

.errorException or NilClass

Error rescued by the middleware.

Returns:

  • (Exception or NilClass)


66
67
68
# File 'lib/watir/rails.rb', line 66

def error
  @middleware.error
end

.error=(value)

Set error rescued by the middleware.

Parameters:

  • value


80
81
82
# File 'lib/watir/rails.rb', line 80

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.



45
46
47
# File 'lib/watir/rails.rb', line 45

def host
  @host || local_host
end

.host=(host)

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

Parameters:



52
53
54
# File 'lib/watir/rails.rb', line 52

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/watir/rails.rb', line 87

def ignore_exceptions?
  if @ignore_exceptions.nil?
    show_exceptions = if legacy_rails?
             ::Rails.configuration.action_dispatch.show_exceptions
           else
             ::Rails.application.config.action_dispatch.show_exceptions
           end

    if 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”.



59
60
61
# File 'lib/watir/rails.rb', line 59

def local_host
  "127.0.0.1"
end

.pending_requests?Boolean

Returns true if there are pending requests to server.

Returns:

  • (Boolean)


73
74
75
# File 'lib/watir/rails.rb', line 73

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.



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/watir/rails.rb', line 107

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