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.2"

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.



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

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
41
# File 'lib/watir/rails.rb', line 27

def boot(port: nil)
  @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)


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

def error
  @middleware.error
end

.error=(value)

Set error rescued by the middleware.

Parameters:

  • value


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

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.



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

def host
  @host || local_host
end

.host=(host)

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

Parameters:



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

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.



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

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



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

def local_host
  "127.0.0.1"
end

.pending_requests?Boolean

Returns true if there are pending requests to server.

Returns:

  • (Boolean)


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

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.



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

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