Class: Watir::Rack

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

Constant Summary collapse

VERSION =
"0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.middleware (readonly)

Returns the value of attribute middleware.



14
15
16
# File 'lib/watir/rack.rb', line 14

def middleware
  @middleware
end

.port

Returns the value of attribute port.



14
15
16
# File 'lib/watir/rack.rb', line 14

def port
  @port
end

.server=(value)

Sets the attribute server

Parameters:

  • value

    the value to set the attribute server to.



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

def server=(value)
  @server = value
end

.test_app

Returns the value of attribute test_app.



13
14
15
# File 'lib/watir/rack.rb', line 13

def test_app
  @test_app
end

Class Method Details

.appObject

Rack app under test.

Returns:

  • (Object)

    Rack Rack app.



97
98
99
100
101
102
103
104
105
# File 'lib/watir/rack.rb', line 97

def app
  test_app = self.test_app

  @app ||= ::Rack::Builder.new do
    map "/" do
      run test_app
    end
  end.to_app
end

.boot(port: nil)

Start the Rack Will be called automatically by Browser#initialize.

Parameters:

  • port (Integer) (defaults to: nil)

    port for the Rack



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/watir/rack.rb', line 22

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

.errorException or NilClass

Error rescued by the middleware.

Returns:

  • (Exception or NilClass)


61
62
63
# File 'lib/watir/rack.rb', line 61

def error
  @middleware.error
end

.error=(value)

Set error rescued by the middleware.

Parameters:

  • value


75
76
77
# File 'lib/watir/rack.rb', line 75

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

.hostString

Host for Rack app under test. Default is local_host.

Returns:

  • (String)

    Host for Rack app under test.



40
41
42
# File 'lib/watir/rack.rb', line 40

def host
  @host || local_host
end

.host=(host)

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

Parameters:



47
48
49
# File 'lib/watir/rack.rb', line 47

def host=(host)
  @host = host
end

.local_hostString

Local host for Rack app under test.

Returns:

  • (String)

    Local host with the value of “127.0.0.1”.



54
55
56
# File 'lib/watir/rack.rb', line 54

def local_host
  "127.0.0.1"
end

.pending_requests?Boolean

Returns true if there are pending requests to server.

Returns:

  • (Boolean)


68
69
70
# File 'lib/watir/rack.rb', line 68

def pending_requests?
  @middleware.pending_requests?
end

.running?Boolean

Check if Rack app under test is running.

Returns:

  • (Boolean)

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



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/watir/rack.rb', line 82

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