Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / phantomjs / lib / phantom / examples / run-qunit.coffee
1 system = require 'system'
2
3 ##
4 # Wait until the test condition is true or a timeout occurs. Useful for waiting
5 # on a server response or for a ui change (fadeIn, etc.) to occur.
6 #
7 # @param testFx javascript condition that evaluates to a boolean,
8 # it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
9 # as a callback function.
10 # @param onReady what to do when testFx condition is fulfilled,
11 # it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
12 # as a callback function.
13 # @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
14 ##
15 waitFor = (testFx, onReady, timeOutMillis=3000) ->
16   start = new Date().getTime()
17   condition = false
18   f = ->
19     if (new Date().getTime() - start < timeOutMillis) and not condition
20       # If not time-out yet and condition not yet fulfilled
21       condition = (if typeof testFx is 'string' then eval testFx else testFx()) #< defensive code
22     else
23       if not condition
24         # If condition still not fulfilled (timeout but condition is 'false')
25         console.log "'waitFor()' timeout"
26         phantom.exit 1
27       else
28         # Condition fulfilled (timeout and/or condition is 'true')
29         console.log "'waitFor()' finished in #{new Date().getTime() - start}ms."
30         if typeof onReady is 'string' then eval onReady else onReady() #< Do what it's supposed to do once the condition is fulfilled
31         clearInterval interval #< Stop this interval
32   interval = setInterval f, 100 #< repeat check every 100ms
33
34 if system.args.length isnt 2
35   console.log 'Usage: run-qunit.coffee URL'
36   phantom.exit 1
37
38 page = require('webpage').create()
39
40 # Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
41 page.onConsoleMessage = (msg) ->
42   console.log msg
43
44 page.open system.args[1], (status) ->
45   if status isnt 'success'
46     console.log 'Unable to access network'
47     phantom.exit 1
48   else
49     waitFor ->
50       page.evaluate ->
51         el = document.getElementById 'qunit-testresult'
52         if el and el.innerText.match 'completed'
53           return true
54         return false
55     , ->
56       failedNum = page.evaluate ->
57         el = document.getElementById 'qunit-testresult'
58         console.log el.innerText
59         try
60           return el.getElementsByClassName('failed')[0].innerHTML
61         catch e
62         return 10000
63
64       phantom.exit if parseInt(failedNum, 10) > 0 then 1 else 0