| 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 page = require('webpage').create(); |
| 8 | |
| 9 | // NOTE: Cannot include '#' or '?' in this URL. |
| 10 | var url = 'auto_tests/misc/local.html'; |
| 11 | |
| 12 | // NOTE: changing the line below to this: |
| 13 | // page.open(url, function(status)) { |
| 14 | // makes phantomjs hang. |
| 15 | page.open(url, function(status) { |
| 16 | if (status !== 'success') { |
| 17 | console.warn('Page status: ' + status); |
| 18 | console.log(page); |
| 19 | phantom.exit(); |
| 20 | } |
| 21 | |
| 22 | var testCase, test; |
| 23 | if (phantom.args.length == 1) { |
| 24 | var parts = phantom.args[0].split('.'); |
| 25 | if (2 != parts.length) { |
| 26 | console.warn('Usage: phantomjs phantom-driver.js [testCase.test]'); |
| 27 | phantom.exit(); |
| 28 | } |
| 29 | testCase = parts[0]; |
| 30 | test = parts[1]; |
| 31 | } |
| 32 | |
| 33 | var loggingOn = false; |
| 34 | page.onConsoleMessage = function (msg) { |
| 35 | if (msg == 'Running ' + test) { |
| 36 | loggingOn = true; |
| 37 | } else if (msg.substr(0, 'Running'.length) == 'Running') { |
| 38 | loggingOn = false; |
| 39 | } |
| 40 | if (loggingOn) console.log(msg); |
| 41 | }; |
| 42 | |
| 43 | page.onError = function (msg, trace) { |
| 44 | console.log(msg); |
| 45 | trace.forEach(function(item) { |
| 46 | console.log(' ', item.file, ':', item.line); |
| 47 | }) |
| 48 | }; |
| 49 | |
| 50 | var results; |
| 51 | |
| 52 | // Run all tests. |
| 53 | var start = new Date().getTime(); |
| 54 | results = page.evaluate(function() { |
| 55 | // Phantom doesn't like stacktrace.js using the "arguments" object |
| 56 | // in stacktrace.js, which it interprets in strict mode. |
| 57 | printStackTrace = undefined; |
| 58 | |
| 59 | var testCases = getAllTestCases(); |
| 60 | var results = {}; |
| 61 | for (var idx in testCases) { |
| 62 | var entry = testCases[idx]; |
| 63 | |
| 64 | var prototype = entry.testCase; |
| 65 | var tc = new entry.testCase(); |
| 66 | var result = tc.runAllTests(); |
| 67 | results[entry.name] = result; |
| 68 | } |
| 69 | return results; |
| 70 | }); |
| 71 | var end = new Date().getTime(); |
| 72 | var elapsed = (end - start) / 1000; |
| 73 | |
| 74 | var num_passing = 0, num_failing = 0; |
| 75 | var failures = []; |
| 76 | for (var testCase in results) { |
| 77 | var caseResults = results[testCase]; |
| 78 | for (var test in caseResults) { |
| 79 | if (caseResults[test] !== true) { |
| 80 | num_failing++; |
| 81 | failures.push(testCase + '.' + test + ' failed'); |
| 82 | } else { |
| 83 | // console.log(testCase + '.' + test + ' passed'); |
| 84 | num_passing++; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | console.log('Ran ' + (num_passing + num_failing) + ' tests in ' + elapsed + 's.'); |
| 90 | console.log(num_passing + ' test(s) passed'); |
| 91 | console.log(num_failing + ' test(s) failed:'); |
| 92 | for (var i = 0; i < failures.length; i++) { |
| 93 | // TODO(danvk): print an auto_test/misc/local URL that runs this test. |
| 94 | console.log(' ' + failures[i] + ' failed.'); |
| 95 | } |
| 96 | |
| 97 | if (num_failing == 0) { |
| 98 | console.log('PASS'); |
| 99 | } else { |
| 100 | console.log('FAIL'); |
| 101 | } |
| 102 | |
| 103 | phantom.exit(); |
| 104 | }); |