forked from madrobby/zepto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.coffee
72 lines (60 loc) · 1.93 KB
/
runner.coffee
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
# Test runner for PhantomJS <phantomjs.org>
# Usage:
# $ phantomjs test/runner.coffee <url-prefix> [<page1>, <page2>, ...]
#
# When no test pages specified, runs all automated tests.
system = require('system')
fs = require('fs')
args = system.args.slice(1)
prefix = args.shift() || "file://#{fs.workingDirectory}/"
if args.length > 0
# list of test pages to run
suites = args
else
# by default, run all test/*.html pages
modules = 'zepto ajax data detect event form fx selector stack'.split /\s+/
suites = modules.map (name)-> "test/#{name}.html"
page = require('webpage').create()
page.onConsoleMessage = (msg) ->
console.log msg
page.onError = (msg, trace) ->
console.log 'ERROR: ' + msg
# used for waiting until the tests finish running
waitFor = (testFn, onReady, timeout=3000) ->
start = new Date()
interval = setInterval ->
if testFn()
clearInterval interval
onReady()
else if new Date() - start > timeout
console.log "timed out."
phantom.exit(1)
, 100
loadNextSuite = ->
if not suites.length
phantom.exit()
else
url = suites.shift() + "?verbosity=WARN"
# PhantomJS chokes on the query string on relative paths
url = prefix + url if not /:\/\//.test url
page.open url, (status) ->
if status isnt "success"
console.log "failed opening #{url}"
phantom.exit(1)
waitFor ->
page.evaluate ->
# the "#results" element needs to have the "finished" class
res = document.getElementById 'results'
/finished/.test res.className if res
, ->
passed = page.evaluate ->
res = document.getElementById 'results'
paths = location.pathname.split('/')
# echo test results to the console
console.log "#{paths[paths.length - 1]} - " + res.textContent
/passed/.test res.className
if passed
loadNextSuite()
else
phantom.exit(1)
loadNextSuite()