remove if statement
[dygraphs.git] / phantom-driver.js
CommitLineData
08fcae0c
DV
1// Invoke via: ./test.sh
2//
3// or phantomjs phantom-driver.js [testCase.test]
4//
5// For more on phantomjs, visit www.phantomjs.org.
6
300c5ff0
DV
7var RunAllAutoTests = function(done_callback) {
8
86a3e64f
DV
9var page = require('webpage').create();
10
11// NOTE: Cannot include '#' or '?' in this URL.
12var url = 'auto_tests/misc/local.html';
13
14// NOTE: changing the line below to this:
15// page.open(url, function(status)) {
16// makes phantomjs hang.
17page.open(url, function(status) {
18 if (status !== 'success') {
19 console.warn('Page status: ' + status);
20 console.log(page);
21 phantom.exit();
22 }
23
24 var testCase, test;
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]');
29 phantom.exit();
30 }
31 testCase = parts[0];
32 test = parts[1];
33 }
34
35 var loggingOn = false;
36 page.onConsoleMessage = function (msg) {
37 if (msg == 'Running ' + test) {
38 loggingOn = true;
39 } else if (msg.substr(0, 'Running'.length) == 'Running') {
40 loggingOn = false;
41 }
42 if (loggingOn) console.log(msg);
43 };
44
45 page.onError = function (msg, trace) {
46 console.log(msg);
47 trace.forEach(function(item) {
48 console.log(' ', item.file, ':', item.line);
49 })
50 };
51
52 var results;
53
54 // Run all tests.
08fcae0c 55 var start = new Date().getTime();
86a3e64f
DV
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;
60
61 var testCases = getAllTestCases();
62 var results = {};
63 for (var idx in testCases) {
64 var entry = testCases[idx];
65
66 var prototype = entry.testCase;
67 var tc = new entry.testCase();
68 var result = tc.runAllTests();
69 results[entry.name] = result;
70 }
71 return results;
72 });
08fcae0c
DV
73 var end = new Date().getTime();
74 var elapsed = (end - start) / 1000;
86a3e64f
DV
75
76 var num_passing = 0, num_failing = 0;
08fcae0c 77 var failures = [];
86a3e64f
DV
78 for (var testCase in results) {
79 var caseResults = results[testCase];
80 for (var test in caseResults) {
81 if (caseResults[test] !== true) {
86a3e64f 82 num_failing++;
08fcae0c 83 failures.push(testCase + '.' + test + ' failed');
86a3e64f
DV
84 } else {
85 // console.log(testCase + '.' + test + ' passed');
86 num_passing++;
87 }
88 }
89 }
08fcae0c
DV
90
91 console.log('Ran ' + (num_passing + num_failing) + ' tests in ' + elapsed + 's.');
86a3e64f 92 console.log(num_passing + ' test(s) passed');
08fcae0c
DV
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.');
97 }
86a3e64f 98
300c5ff0
DV
99 done_callback(num_failing, num_passing);
100});
101
102};
103
104// Load all "tests/" pages.
105var LoadAllManualTests = function(totally_done_callback) {
106
107var fs = require('fs');
108var tests = fs.list('tests');
109var pages = [];
110
111function make_barrier_closure(n, fn) {
112 var calls = 0;
113 return function() {
114 calls++;
115 if (calls == n) {
116 fn();
117 } else {
118 // console.log('' + calls + ' done, ' + (n - calls) + ' remain');
119 }
120 };
121}
122
123var tasks = [];
124for (var i = 0; i < tests.length; i++) {
125 if (tests[i].substr(-5) != '.html') continue;
126 tasks.push(tests[i]);
127}
128tasks = [ 'independent-series.html' ];
129
130var 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) {
136 success++;
137 } else {
138 failures++;
139 }
140 }
141 console.log('Successfully loaded ' + success + ' / ' +
142 (success + failures) + ' test pages.');
143 totally_done_callback(failures, success);
144 }, 2000);
145});
146
147
148for (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);
159 });
160 };
161 page.onLoadFinished = function(status) {
162 if (status == 'success') {
163 page.success = true;
164 }
165 if (!page.done) loaded_page();
166 page.done = true;
167 };
168 page.open(url);
169 return page;
170 }(tasks[i], url));
171}
172
173};
174
175
176// First run all auto_tests.
177// If they all pass, load the manual tests.
178RunAllAutoTests(function(num_failing, num_passing) {
179 if (num_failing !== 0) {
86a3e64f 180 console.log('FAIL');
300c5ff0 181 phantom.exit();
86a3e64f 182 }
300c5ff0 183 console.log('PASS');
86a3e64f 184 phantom.exit();
300c5ff0
DV
185
186 // This is not yet reliable enough to be useful:
187 /*
188 LoadAllManualTests(function(failing, passing) {
189 if (failing !== 0) {
190 console.log('FAIL');
191 } else {
192 console.log('PASS');
193 }
194 phantom.exit();
195 });
196 */
86a3e64f 197});