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
);
27 if (phantom
.args
.length
> 0 && phantom
.args
[0] === "--verbose") {
31 if (phantom
.args
.length
== optIdx
+ 1) {
32 var parts
= phantom
.args
[optIdx
].split('.');
33 if (2 != parts
.length
) {
34 console
.warn('Usage: phantomjs phantom-driver.js [--verbose] [testCase.test]');
41 var loggingOn
= false;
43 page
.onConsoleMessage
= function (msg
) {
44 if (msg
== 'Running ' + test
) {
46 } else if (msg
.substr(0, 'Running'.length
) == 'Running') {
49 if (verbose
|| loggingOn
) console
.log(msg
);
52 page
.onError
= function (msg
, trace
) {
54 trace
.forEach(function(item
) {
55 console
.log(' ', item
.file
, ':', item
.line
);
62 var start
= new Date().getTime();
63 results
= page
.evaluate(function() {
64 var num_passing
= 0, num_failing
= 0;
66 // Phantom doesn't like stacktrace.js using the "arguments" object
67 // in stacktrace.js, which it interprets in strict mode.
68 printStackTrace
= undefined
;
70 jstestdriver
.attachListener({
71 finish
: function(tc
, name
, result
, e
) {
72 console
.log("Result:", tc
.name
, name
, result
, e
);
74 // console.log(testCase + '.' + test + ' passed');
78 failures
.push(tc
.name
+ '.' + name
);
82 var testCases
= getAllTestCases();
83 for (var idx
in testCases
) {
84 var entry
= testCases
[idx
];
86 var prototype = entry
.testCase
;
87 var tc
= new entry
.testCase();
88 var result
= tc
.runAllTests();
91 num_passing
: num_passing
,
92 num_failing
: num_failing
,
96 var end
= new Date().getTime();
97 var elapsed
= (end
- start
) / 1000;
99 console
.log('Ran ' + (results
.num_passing
+ results
.num_failing
) + ' tests in ' + elapsed
+ 's.');
100 console
.log(results
.num_passing
+ ' test(s) passed');
101 console
.log(results
.num_failing
+ ' test(s) failed:');
102 for (var i
= 0; i
< results
.failures
.length
; i
++) {
103 // TODO(danvk): print an auto_test/misc/local URL that runs
this test
.
104 console
.log(' ' + results
.failures
[i
] + ' failed.');
107 done_callback(results
.num_failing
, results
.num_passing
);
112 // Load all "tests/" pages.
113 var LoadAllManualTests = function(totally_done_callback) {
115 var fs = require('fs');
116 var tests = fs.list('tests');
119 function make_barrier_closure(n, fn) {
126 // console.log('' + calls + ' done, ' + (n - calls) + ' remain');
132 for (var i = 0; i < tests.length; i++) {
133 if (tests[i].substr(-5) != '.html') continue;
134 tasks.push(tests[i]);
136 tasks = [ 'independent-series.html' ];
138 var loaded_page = make_barrier_closure(tasks.length, function() {
139 // Wait 2 secs to allow JS errors to happen after page load.
140 setTimeout(function() {
141 var success = 0, failures = 0;
142 for (var i = 0; i < pages.length; i++) {
143 if (pages[i].success && !pages[i].hasErrors) {
149 console.log('Successfully loaded ' + success + ' / ' +
150 (success + failures) + ' test pages.');
151 totally_done_callback(failures, success);
156 for (var i = 0; i < tasks.length; i++) {
157 var url = 'file://' + fs.absolute('tests/' + tasks[i]);
158 pages.push(function(path, url) {
159 var page = require('webpage').create();
160 page.success = false;
161 page.hasErrors = false;
162 page.onError = function (msg, trace) {
163 console.log(path + ': ' + msg);
164 page.hasErrors = true;
165 trace.forEach(function(item) {
166 console.log(' ', item.file, ':', item.line);
169 page.onLoadFinished = function(status) {
170 if (status == 'success') {
173 if (!page.done) loaded_page();
184 // First run all auto_tests.
185 // If they all pass, load the manual tests.
186 RunAllAutoTests(function(num_failing, num_passing) {
187 if (num_failing !== 0) {
195 // This is not yet reliable enough to be useful:
197 LoadAllManualTests(function(failing, passing) {