88
|
1 // Simple phantom.js integration script
|
|
2 // Adapted from Modernizr
|
|
3
|
|
4 function waitFor(testFx, onReady, timeOutMillis) {
|
|
5 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5001 //< Default Max Timout is 5s
|
|
6 , start = new Date().getTime()
|
|
7 , condition = false
|
|
8 , interval = setInterval(function () {
|
|
9 if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
|
|
10 // If not time-out yet and condition not yet fulfilled
|
|
11 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()) //< defensive code
|
|
12 } else {
|
|
13 if (!condition) {
|
|
14 // If condition still not fulfilled (timeout but condition is 'false')
|
|
15 console.log("'waitFor()' timeout")
|
|
16 phantom.exit(1)
|
|
17 } else {
|
|
18 // Condition fulfilled (timeout and/or condition is 'true')
|
|
19 typeof(onReady) === "string" ? eval(onReady) : onReady() //< Do what it's supposed to do once the condition is fulfilled
|
|
20 clearInterval(interval) //< Stop this interval
|
|
21 }
|
|
22 }
|
|
23 }, 100) //< repeat check every 100ms
|
|
24 }
|
|
25
|
|
26
|
|
27 if (phantom.args.length === 0 || phantom.args.length > 2) {
|
|
28 console.log('Usage: phantom.js URL')
|
|
29 phantom.exit()
|
|
30 }
|
|
31
|
|
32 var page = new WebPage()
|
|
33
|
|
34 // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
|
|
35 page.onConsoleMessage = function(msg) {
|
|
36 console.log(msg)
|
|
37 };
|
|
38
|
|
39 page.open(phantom.args[0], function(status){
|
|
40 if (status !== "success") {
|
|
41 console.log("Unable to access network")
|
|
42 phantom.exit()
|
|
43 } else {
|
|
44 waitFor(function(){
|
|
45 return page.evaluate(function(){
|
|
46 var el = document.getElementById('qunit-testresult')
|
|
47 if (el && el.innerText.match('completed')) {
|
|
48 return true
|
|
49 }
|
|
50 return false
|
|
51 })
|
|
52 }, function(){
|
|
53 var failedNum = page.evaluate(function(){
|
|
54 var el = document.getElementById('qunit-testresult')
|
|
55 try {
|
|
56 return el.getElementsByClassName('failed')[0].innerHTML
|
|
57 } catch (e) { }
|
|
58 return 10000
|
|
59 });
|
|
60 phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0)
|
|
61 })
|
|
62 }
|
|
63 }) |