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