forked from rails/javascript_test
/
javascript_test.rb
222 lines (183 loc) · 4.29 KB
/
javascript_test.rb
1
2
3
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#require 'rake/tasklib'
require 'thread'
require 'webrick'
class Browser
def supported?; true; end
def setup ; end
def open(url) ; end
def teardown ; end
def host
require 'rbconfig'
Config::CONFIG['host']
end
def macos?
host.include?('darwin')
end
def windows?
host.include?('mswin')
end
def linux?
host.include?('linux')
end
def applescript(script)
raise "Can't run AppleScript on #{host}" unless macos?
system "osascript -e '#{script}' 2>&1 >/dev/null"
end
end
class FirefoxBrowser < Browser
def initialize(path='c:\Program Files\Mozilla Firefox\firefox.exe')
@path = path
end
def visit(url)
applescript('tell application "Firefox" to Get URL "' + url + '"') if macos?
system("#{@path} #{url}") if windows?
system("firefox #{url}") if linux?
end
def to_s
"Firefox"
end
end
class SafariBrowser < Browser
def supported?
macos?
end
def setup
applescript('tell application "Safari" to make new document')
end
def visit(url)
applescript('tell application "Safari" to set URL of front document to "' + url + '"')
end
def teardown
#applescript('tell application "Safari" to close front document')
end
def to_s
"Safari"
end
end
class IEBrowser < Browser
def initialize(path='C:\Program Files\Internet Explorer\IEXPLORE.EXE')
@path = path
end
def setup
if windows?
puts %{
MAJOR ANNOYANCE on Windows.
You have to shut down the Internet Explorer manually after each test
for the script to proceed.
Any suggestions on fixing this is GREATLY appreaciated!
Thank you for your understanding.
}
end
end
def supported?
windows?
end
def visit(url)
system("#{@path} #{url}") if windows?
end
def to_s
"Internet Explorer"
end
end
class KonquerorBrowser < Browser
def supported?
linux?
end
def visit(url)
system("kfmclient openURL #{url}")
end
def to_s
"Konqueror"
end
end
# shut up, webrick :-)
class ::WEBrick::HTTPServer
def access_log(config, req, res)
# nop
end
end
class ::WEBrick::BasicLog
def log(level, data)
# nop
end
end
class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler
def do_GET(req, res)
super
res['etag'] = nil
res['last-modified'] = Time.now + 1000
res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0"'
res['Pragma'] = 'no-cache'
res['Expires'] = Time.now - 1000
end
end
class JavaScriptTestrunner
def initialize(name=:test)
@name = name
@tests = []
@browsers = []
@result = true
@queue = Queue.new
result = []
@server = WEBrick::HTTPServer.new(:Port => 4711) # TODO: make port configurable
@server.mount_proc("/results") do |req, res|
@queue.push(req.query['result'])
res.body = "OK"
end
yield self if block_given?
define
end
def successful?
@result
end
def define
trap("INT") { @server.shutdown }
t = Thread.new { @server.start }
# run all combinations of browsers and tests
@browsers.each do |browser|
if browser.supported?
browser.setup
@tests.each do |test|
browser.visit("http://localhost:4711#{test}?resultsURL=http://localhost:4711/results&t=" + ("%.6f" % Time.now.to_f))
result = @queue.pop
puts "#{test} on #{browser}: #{result}"
@result = false unless result == 'SUCCESS'
end
browser.teardown
else
puts "Skipping #{browser}, not supported on this OS"
end
browser.teardown
end
@server.shutdown
t.join
end
def mount(path, dir=nil)
dir ||= (Dir.pwd + path)
@server.mount(path, NonCachingFileHandler, dir)
end
# test should be specified as a url
def run(test)
url = "/test/javascript/#{test}_test.html"
unless File.exists?(RAILS_ROOT+url)
raise "Missing test file #{url} for #{test}"
end
@tests << url
end
def browser(browser)
browser =
case(browser)
when :firefox
FirefoxBrowser.new
when :safari
SafariBrowser.new
when :ie
IEBrowser.new
when :konqueror
KonquerorBrowser.new
else
browser
end
@browsers<<browser
end
end