1 var webpage = require('webpage');
2 var server = require('webserver').create();
3 var system = require('system');
4 var fs = require('fs');
8 var listening = server.listen(port, function(request, response) {
9 //console.log("Requested " + request.url);
11 var filename = ("test/" + request.url.slice(1)).replace(/[\\\/]/g, fs.separator);
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>");
20 // we set the headers here
21 response.statusCode = 200;
24 "Content-Type": "text/html"
27 response.write(fs.read(filename));
32 console.log("could not create web server listening on port " + port);
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.
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
50 function waitFor(testFx, onReady, timeOutMillis, timeOutErrorMessage) {
51 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 10001, //< Default Max Timeout is 10s
52 start = new Date().getTime(),
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
60 // If condition still not fulfilled (timeout but condition is 'false')
61 console.log(timeOutErrorMessage || "'waitFor()' timeout");
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
69 }, 100); //< repeat check every 100ms
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);
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;
86 page.onConsoleMessage = function(msg) {
89 var exitCode = page.evaluate(function() {
91 console.log(document.body.querySelector('.description').innerText);
92 var list = document.body.querySelectorAll('.results > #details > .specDetail.failed');
93 if (list && list.length > 0) {
95 console.log(list.length + ' test(s) FAILED:');
96 for (var i = 0; i < list.length; ++i) {
98 desc = el.querySelector('.description'),
99 msg = el.querySelector('.resultMessage.fail');
101 console.log(desc.innerText);
102 console.log(msg.innerText);
107 console.log(document.body.querySelector('.alert > .passingAlert.bar').innerText);
111 testFinished(exitCode);
113 10000, // 10 second timeout
114 "Test failed waiting for jasmine results on page: " + url);
119 function scanDirectory(path, regex) {
121 fs.list(path).forEach(function(file) {
122 if (file.match(regex)) {
133 function testFinished(failed) {
138 if (totalDone === totalTests) {
139 phantom.exit(totalFailed > 0 ? 1 : 0);
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);