Newer
Older
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment') unless defined?(Rails)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'timeout'
require 'socket'
require 'singleton'
require 'capybara/rails'
require 'capybara/cucumber'
require 'capybara/session'
class TestServerFixture
# simple interface to script/server
def self.is_port_open(host, port, tries)
for i in (1..tries)
begin
Timeout::timeout(2) do
begin
s = TCPSocket.new(host, port)
s.close
return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
sleep( 2)
end
end
rescue Timeout::Error
return false
end
end
return false
end
def self.start_if_needed
unless TestServerFixture.is_port_open( "localhost", 3000, 2)
system( "script/server -d")
if TestServerFixture.is_port_open( "localhost", 3000, 30)
puts "script/server started"
else
puts "Error: can't start script/server"
end
end
end
end
class CapybaraSettings
# simple save/restore for Capybara
include Singleton
def save
begin
@run_server = Capybara.run_server
@driver = Capybara.current_driver
@host = Capybara.app_host
rescue => e
puts "Saving exception: " + e.inspect
end
end
def restore
begin
Capybara.current_driver = @driver
Capybara.app_host = @host
Capybara.run_server = @run_server
rescue => e
puts "Restore exception: " + e.inspect
end
end
end