1 // Invoke via: ./test
.sh
3 // or phantomjs phantom-driver.js [testCase.test]
5 // For more on phantomjs, visit www.phantomjs.org.
7 var RunAllAutoTests
= function(done_callback
) {
9 var page
= require('webpage').create();
11 // NOTE: Cannot include '#' or '?' in this URL.
12 var url
= 'auto_tests/misc/local.html';
14 // NOTE: changing the line below to this:
15 // page.open(url, function(status)) {
16 // makes phantomjs hang.
17 page
.open(url
, function(status
) {
18 if (status
!== 'success') {
19 console
.warn('Page status: ' + status
);
25 if (phantom
.args
.length
== 1) {
26 var parts
= phantom
.args
[0].split('.');
27 if (2 != parts
.length
) {
28 console
.warn('Usage: phantomjs phantom-driver.js [testCase.test]');
35 var loggingOn
= false;
36 page
.onConsoleMessage
= function (msg
) {
37 if (msg
== 'Running ' + test
) {
39 } else if (msg
.substr(0, 'Running'.length
) == 'Running') {
42 if (loggingOn
) console
.log(msg
);
45 page
.onError
= function (msg
, trace
) {
47 trace
.forEach(function(item
) {
48 console
.log(' ', item
.file
, ':', item
.line
);
55 var start
= new Date().getTime();
56 results
= page
.evaluate(function() {
57 // Phantom doesn't like stacktrace.js using the "arguments" object
58 // in stacktrace.js, which it interprets in strict mode.
59 printStackTrace
= undefined
;
61 var testCases
= getAllTestCases();
63 for (var idx
in testCases
) {
64 var entry
= testCases
[idx
];
66 var prototype = entry
.testCase
;
67 var tc
= new entry
.testCase();
68 var result
= tc
.runAllTests();
69 results
[entry
.name
] = result
;
73 var end
= new Date().getTime();
74 var elapsed
= (end
- start
) / 1000;
76 var num_passing
= 0, num_failing
= 0;
78 for (var testCase
in results
) {
79 var caseResults
= results
[testCase
];
80 for (var test
in caseResults
) {
81 if (caseResults
[test
] !== true) {
83 failures
.push(testCase
+ '.' + test
);
85 // console.log(testCase + '.' + test + ' passed');
91 console
.log('Ran ' + (num_passing
+ num_failing
) + ' tests in ' + elapsed
+ 's.');
92 console
.log(num_passing
+ ' test(s) passed');
93 console
.log(num_failing
+ ' test(s) failed:');
94 for (var i
= 0; i
< failures
.length
; i
++) {
95 // TODO(danvk): print an auto_test/misc/local URL that runs
this test
.
96 console
.log(' ' + failures
[i
] + ' failed.');
99 done_callback(num_failing
, num_passing
);
104 // Load all "tests/" pages.
105 var LoadAllManualTests = function(totally_done_callback) {
107 var fs = require('fs');
108 var tests = fs.list('tests');
111 function make_barrier_closure(n, fn) {
118 // console.log('' + calls + ' done, ' + (n - calls) + ' remain');
124 for (var i = 0; i < tests.length; i++) {
125 if (tests[i].substr(-5) != '.html') continue;
126 tasks.push(tests[i]);
128 tasks = [ 'independent-series.html' ];
130 var loaded_page = make_barrier_closure(tasks.length, function() {
131 // Wait 2 secs to allow JS errors to happen after page load.
132 setTimeout(function() {
133 var success = 0, failures = 0;
134 for (var i = 0; i < pages.length; i++) {
135 if (pages[i].success && !pages[i].hasErrors) {
141 console.log('Successfully loaded ' + success + ' / ' +
142 (success + failures) + ' test pages.');
143 totally_done_callback(failures, success);
148 for (var i = 0; i < tasks.length; i++) {
149 var url = 'file://' + fs.absolute('tests/' + tasks[i]);
150 pages.push(function(path, url) {
151 var page = require('webpage').create();
152 page.success = false;
153 page.hasErrors = false;
154 page.onError = function (msg, trace) {
155 console.log(path + ': ' + msg);
156 page.hasErrors = true;
157 trace.forEach(function(item) {
158 console.log(' ', item.file, ':', item.line);
161 page.onLoadFinished = function(status) {
162 if (status == 'success') {
165 if (!page.done) loaded_page();
176 // First run all auto_tests.
177 // If they all pass, load the manual tests.
178 RunAllAutoTests(function(num_failing, num_passing) {
179 if (num_failing !== 0) {
187 // This is not yet reliable enough to be useful:
189 LoadAllManualTests(function(failing, passing) {