| 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 | |
| 7 | var RunAllAutoTests = function(done_callback) { |
| 8 | |
| 9 | var page = require('webpage').create(); |
| 10 | |
| 11 | // NOTE: Cannot include '#' or '?' in this URL. |
| 12 | var 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. |
| 17 | page.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 | var verbose = false; |
| 26 | var optIdx = 0; |
| 27 | if (phantom.args.length > 0 && phantom.args[0] === "--verbose") { |
| 28 | verbose = true; |
| 29 | optIdx = 1; |
| 30 | } |
| 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]'); |
| 35 | phantom.exit(); |
| 36 | } |
| 37 | testCase = parts[0]; |
| 38 | test = parts[1]; |
| 39 | } |
| 40 | |
| 41 | var loggingOn = false; |
| 42 | |
| 43 | page.onConsoleMessage = function (msg) { |
| 44 | if (msg == 'Running ' + test) { |
| 45 | loggingOn = true; |
| 46 | } else if (msg.substr(0, 'Running'.length) == 'Running') { |
| 47 | loggingOn = false; |
| 48 | } |
| 49 | if (verbose || loggingOn) console.log(msg); |
| 50 | }; |
| 51 | |
| 52 | page.onError = function (msg, trace) { |
| 53 | console.log(msg); |
| 54 | trace.forEach(function(item) { |
| 55 | console.log(' ', item.file, ':', item.line); |
| 56 | }) |
| 57 | }; |
| 58 | |
| 59 | var results; |
| 60 | |
| 61 | // Run all tests. |
| 62 | var start = new Date().getTime(); |
| 63 | results = page.evaluate(function() { |
| 64 | var num_passing = 0, num_failing = 0; |
| 65 | var failures = []; |
| 66 | |
| 67 | jstestdriver.attachListener({ |
| 68 | finish : function(tc, name, result, e) { |
| 69 | console.log("Result:", tc.name, name, result, e); |
| 70 | if (result) { |
| 71 | // console.log(testCase + '.' + test + ' passed'); |
| 72 | num_passing++; |
| 73 | } else { |
| 74 | num_failing++; |
| 75 | failures.push(tc.name + '.' + name); |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | var testCases = getAllTestCases(); |
| 80 | for (var idx in testCases) { |
| 81 | var entry = testCases[idx]; |
| 82 | |
| 83 | var prototype = entry.testCase; |
| 84 | var tc = new entry.testCase(); |
| 85 | var result = tc.runAllTests(); |
| 86 | } |
| 87 | return { |
| 88 | num_passing : num_passing, |
| 89 | num_failing : num_failing, |
| 90 | failures : failures |
| 91 | }; |
| 92 | }); |
| 93 | var end = new Date().getTime(); |
| 94 | var elapsed = (end - start) / 1000; |
| 95 | |
| 96 | console.log('Ran ' + (results.num_passing + results.num_failing) + ' tests in ' + elapsed + 's.'); |
| 97 | console.log(results.num_passing + ' test(s) passed'); |
| 98 | console.log(results.num_failing + ' test(s) failed:'); |
| 99 | for (var i = 0; i < results.failures.length; i++) { |
| 100 | // TODO(danvk): print an auto_test/misc/local URL that runs this test. |
| 101 | console.log(' ' + results.failures[i] + ' failed.'); |
| 102 | } |
| 103 | |
| 104 | done_callback(results.num_failing, results.num_passing); |
| 105 | }); |
| 106 | |
| 107 | }; |
| 108 | |
| 109 | // Load all "tests/" pages. |
| 110 | var LoadAllManualTests = function(totally_done_callback) { |
| 111 | |
| 112 | var fs = require('fs'); |
| 113 | var tests = fs.list('tests'); |
| 114 | var pages = []; |
| 115 | |
| 116 | function make_barrier_closure(n, fn) { |
| 117 | var calls = 0; |
| 118 | return function() { |
| 119 | calls++; |
| 120 | if (calls == n) { |
| 121 | fn(); |
| 122 | } else { |
| 123 | // console.log('' + calls + ' done, ' + (n - calls) + ' remain'); |
| 124 | } |
| 125 | }; |
| 126 | } |
| 127 | |
| 128 | var tasks = []; |
| 129 | for (var i = 0; i < tests.length; i++) { |
| 130 | if (tests[i].substr(-5) != '.html') continue; |
| 131 | tasks.push(tests[i]); |
| 132 | } |
| 133 | tasks = [ 'independent-series.html' ]; |
| 134 | |
| 135 | var loaded_page = make_barrier_closure(tasks.length, function() { |
| 136 | // Wait 2 secs to allow JS errors to happen after page load. |
| 137 | setTimeout(function() { |
| 138 | var success = 0, failures = 0; |
| 139 | for (var i = 0; i < pages.length; i++) { |
| 140 | if (pages[i].success && !pages[i].hasErrors) { |
| 141 | success++; |
| 142 | } else { |
| 143 | failures++; |
| 144 | } |
| 145 | } |
| 146 | console.log('Successfully loaded ' + success + ' / ' + |
| 147 | (success + failures) + ' test pages.'); |
| 148 | totally_done_callback(failures, success); |
| 149 | }, 2000); |
| 150 | }); |
| 151 | |
| 152 | |
| 153 | for (var i = 0; i < tasks.length; i++) { |
| 154 | var url = 'file://' + fs.absolute('tests/' + tasks[i]); |
| 155 | pages.push(function(path, url) { |
| 156 | var page = require('webpage').create(); |
| 157 | page.success = false; |
| 158 | page.hasErrors = false; |
| 159 | page.onError = function (msg, trace) { |
| 160 | console.log(path + ': ' + msg); |
| 161 | page.hasErrors = true; |
| 162 | trace.forEach(function(item) { |
| 163 | console.log(' ', item.file, ':', item.line); |
| 164 | }); |
| 165 | }; |
| 166 | page.onLoadFinished = function(status) { |
| 167 | if (status == 'success') { |
| 168 | page.success = true; |
| 169 | } |
| 170 | if (!page.done) loaded_page(); |
| 171 | page.done = true; |
| 172 | }; |
| 173 | page.open(url); |
| 174 | return page; |
| 175 | }(tasks[i], url)); |
| 176 | } |
| 177 | |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | // First run all auto_tests. |
| 182 | // If they all pass, load the manual tests. |
| 183 | RunAllAutoTests(function(num_failing, num_passing) { |
| 184 | if (num_failing !== 0) { |
| 185 | console.log('FAIL'); |
| 186 | phantom.exit(); |
| 187 | } else { |
| 188 | console.log('PASS'); |
| 189 | } |
| 190 | phantom.exit(); |
| 191 | |
| 192 | // This is not yet reliable enough to be useful: |
| 193 | /* |
| 194 | LoadAllManualTests(function(failing, passing) { |
| 195 | if (failing !== 0) { |
| 196 | console.log('FAIL'); |
| 197 | } else { |
| 198 | console.log('PASS'); |
| 199 | } |
| 200 | phantom.exit(); |
| 201 | }); |
| 202 | */ |
| 203 | }); |