1
var webpage = require('webpage');
2
var server = require('webserver').create();
3
var system = require('system');
4
var fs = require('fs');
7
var listening = server.listen(port, function (request, response) {
8
//console.log("Requested "+request.url);
10
var filename = ("test/" + request.url.slice(1)).replace(/[\\\/]/g, fs.separator);
12
if (!fs.exists(filename) || !fs.isFile(filename)) {
13
response.statusCode = 404;
14
response.write("<html><head></head><body><h1>File Not Found</h1><h2>File:"+filename+"</h2></body></html>");
19
// we set the headers here
20
response.statusCode = 200;
21
response.headers = {"Cache": "no-cache", "Content-Type": "text/html"};
23
response.write(fs.read(filename));
28
console.log("could not create web server listening on port " + port);
33
* Wait until the test condition is true or a timeout occurs. Useful for waiting
34
* on a server response or for a ui change (fadeIn, etc.) to occur.
36
* @param testFx javascript condition that evaluates to a boolean,
37
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
38
* as a callback function.
39
* @param onReady what to do when testFx condition is fulfilled,
40
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
41
* as a callback function.
42
* @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
44
function waitFor(testFx, onReady, timeOutMillis) {
45
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 10001, //< Default Max Timeout is 10s
46
start = new Date().getTime(),
48
interval = setInterval(function() {
49
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
50
// If not time-out yet and condition not yet fulfilled
51
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
54
// If condition still not fulfilled (timeout but condition is 'false')
55
console.log("'waitFor()' timeout");
58
// Condition fulfilled (timeout and/or condition is 'true')
59
//console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
60
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
61
clearInterval(interval); //< Stop this interval
64
}, 100); //< repeat check every 100ms
67
function testPage(url) {
68
var page = webpage.create();
69
page.open(url, function (status) {
70
if (status !== "success") {
71
console.log("Unable to access network - " + status);
75
return page.evaluate(function(){
76
return document.body && document.body.querySelector &&
77
document.body.querySelector('.symbolSummary .pending') === null &&
78
document.body.querySelector('.results') !== null;
81
page.onConsoleMessage = function (msg) {
84
var exitCode = page.evaluate(function(){
86
console.log(document.body.querySelector('.description').innerText);
87
var list = document.body.querySelectorAll('.results > #details > .specDetail.failed');
88
if (list && list.length > 0) {
90
console.log(list.length + ' test(s) FAILED:');
91
for (i = 0; i < list.length; ++i) {
93
desc = el.querySelector('.description'),
94
msg = el.querySelector('.resultMessage.fail');
96
console.log(desc.innerText);
97
console.log(msg.innerText);
102
console.log(document.body.querySelector('.alert > .passingAlert.bar').innerText);
106
testFinished(exitCode);
112
function scanDirectory(path, regex) {
114
fs.list(path).forEach(function (file) {
115
if (file.match(regex)) {
126
function testFinished(failed) {
127
if (failed) { totalFailed++; }
129
if (totalDone === totalTests) { phantom.exit(totalFailed > 0 ? 1 : 0); }
132
if (system.args.length != 2 && system.args[1] != "--no-tests") {
133
var files = scanDirectory("test/browser/", /^test-runner-.+\.htm$/);
134
totalTests = files.length;
135
console.log("found " + files.length + " tests");
136
files.forEach(function(file) {
137
testPage("http://localhost:8081/browser/" + file);
b'\\ No newline at end of file'