add tag and category models
[redakcja.git] / apps / wiki / static--wiki--editor--node_modules--grunt-contrib-less--node_modules--less--test / browser / phantom-runner.js
1 var webpage = require('webpage');
2 var server = require('webserver').create();
3 var system = require('system');
4 var fs = require('fs');
5 var host;
6 var port = 8081;
7
8 var listening = server.listen(port, function(request, response) {
9   //console.log("Requested " + request.url);
10
11   var filename = ("test/" + request.url.slice(1)).replace(/[\\\/]/g, fs.separator);
12
13   if (!fs.exists(filename) || !fs.isFile(filename)) {
14     response.statusCode = 404;
15     response.write("<html><head></head><body><h1>File Not Found</h1><h2>File:" + filename + "</h2></body></html>");
16     response.close();
17     return;
18   }
19
20   // we set the headers here
21   response.statusCode = 200;
22   response.headers = {
23     "Cache": "no-cache",
24     "Content-Type": "text/html"
25   };
26
27   response.write(fs.read(filename));
28
29   response.close();
30 });
31 if (!listening) {
32   console.log("could not create web server listening on port " + port);
33   phantom.exit();
34 }
35
36 /**
37  * Wait until the test condition is true or a timeout occurs. Useful for waiting
38  * on a server response or for a ui change (fadeIn, etc.) to occur.
39  *
40  * @param testFx javascript condition that evaluates to a boolean,
41  * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
42  * as a callback function.
43  * @param onReady what to do when testFx condition is fulfilled,
44  * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
45  * as a callback function.
46  * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
47  * @param timeOutErrorMessage the error message if time out occurs
48  */
49
50 function waitFor(testFx, onReady, timeOutMillis, timeOutErrorMessage) {
51   var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 10001, //< Default Max Timeout is 10s
52     start = new Date().getTime(),
53     condition = false,
54     interval = setInterval(function() {
55       if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
56         // If not time-out yet and condition not yet fulfilled
57         condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
58       } else {
59         if (!condition) {
60           // If condition still not fulfilled (timeout but condition is 'false')
61           console.log(timeOutErrorMessage || "'waitFor()' timeout");
62           phantom.exit(1);
63         } else {
64           // Condition fulfilled (timeout and/or condition is 'true')
65           typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
66           clearInterval(interval); //< Stop this interval
67         }
68       }
69     }, 100); //< repeat check every 100ms
70 }
71
72 function testPage(url) {
73   var page = webpage.create();
74   page.open(url, function(status) {
75     if (status !== "success") {
76       console.log("Unable to access network - " + status);
77       phantom.exit();
78     } else {
79       waitFor(function() {
80         return page.evaluate(function() {
81           return document.body && document.body.querySelector &&
82             document.body.querySelector('.symbolSummary .pending') === null &&
83             document.body.querySelector('.results') !== null;
84         });
85       }, function() {
86         page.onConsoleMessage = function(msg) {
87           console.log(msg);
88         };
89         var exitCode = page.evaluate(function() {
90           console.log('');
91           console.log(document.body.querySelector('.description').innerText);
92           var list = document.body.querySelectorAll('.results > #details > .specDetail.failed');
93           if (list && list.length > 0) {
94             console.log('');
95             console.log(list.length + ' test(s) FAILED:');
96             for (var i = 0; i < list.length; ++i) {
97               var el = list[i],
98                 desc = el.querySelector('.description'),
99                 msg = el.querySelector('.resultMessage.fail');
100               console.log('');
101               console.log(desc.innerText);
102               console.log(msg.innerText);
103               console.log('');
104             }
105             return 1;
106           } else {
107             console.log(document.body.querySelector('.alert > .passingAlert.bar').innerText);
108             return 0;
109           }
110         });
111         testFinished(exitCode);
112       },
113         10000, // 10 second timeout
114       "Test failed waiting for jasmine results on page: " + url);
115     }
116   });
117 }
118
119 function scanDirectory(path, regex) {
120   var files = [];
121   fs.list(path).forEach(function(file) {
122     if (file.match(regex)) {
123       files.push(file);
124     }
125   });
126   return files;
127 }
128
129 var totalTests = 0,
130   totalFailed = 0,
131   totalDone = 0;
132
133 function testFinished(failed) {
134   if (failed) {
135     totalFailed++;
136   }
137   totalDone++;
138   if (totalDone === totalTests) {
139     phantom.exit(totalFailed > 0 ? 1 : 0);
140   }
141 }
142
143 if (system.args.length != 2 && system.args[1] != "--no-tests") {
144   var files = scanDirectory("test/browser/", /^test-runner-.+\.htm$/);
145   totalTests = files.length;
146   console.log("found " + files.length + " tests");
147   files.forEach(function(file) {
148     testPage("http://localhost:8081/browser/" + file);
149   });
150 }