Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / phantomjs / lib / phantom / examples / run-jasmine.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-jasmine.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()
48   else
49     waitFor ->
50       page.evaluate ->
51         if document.body.querySelector '.finished-at'
52           return true
53         return false
54     , ->
55       page.evaluate ->
56         console.log document.body.querySelector('.description').innerText
57         list = document.body.querySelectorAll('.failed > .description, .failed > .messages > .resultMessage')
58         for el in list
59           console.log el.innerText
60
61       phantom.exit()