X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=phantom-driver.js;h=9b98fdf14660386797ec806538506ee8b9078daf;hb=7b00a3cd1eedf449f0d08f63efc544ffb566ed08;hp=43e811c17afad126df2ffa8c47e53852f486bfe3;hpb=e6b0b7c295a1c3838817744e4548c52bbbdf051f;p=dygraphs.git diff --git a/phantom-driver.js b/phantom-driver.js index 43e811c..9b98fdf 100644 --- a/phantom-driver.js +++ b/phantom-driver.js @@ -1,3 +1,11 @@ +// Invoke via: ./test.sh +// +// or phantomjs phantom-driver.js [testCase.test] +// +// For more on phantomjs, visit www.phantomjs.org. + +var RunAllAutoTests = function(done_callback) { + var page = require('webpage').create(); // NOTE: Cannot include '#' or '?' in this URL. @@ -44,6 +52,7 @@ page.open(url, function(status) { var results; // Run all tests. + var start = new Date().getTime(); results = page.evaluate(function() { // Phantom doesn't like stacktrace.js using the "arguments" object // in stacktrace.js, which it interprets in strict mode. @@ -61,29 +70,129 @@ page.open(url, function(status) { } return results; }); + var end = new Date().getTime(); + var elapsed = (end - start) / 1000; var num_passing = 0, num_failing = 0; + var failures = []; for (var testCase in results) { var caseResults = results[testCase]; for (var test in caseResults) { if (caseResults[test] !== true) { - // TODO(danvk): print an auto_test/misc/local URL that runs this test. num_failing++; - console.log(testCase + '.' + test + ' failed'); + failures.push(testCase + '.' + test); } else { // console.log(testCase + '.' + test + ' passed'); num_passing++; } } } + + console.log('Ran ' + (num_passing + num_failing) + ' tests in ' + elapsed + 's.'); console.log(num_passing + ' test(s) passed'); - console.log(num_failing + ' test(s) failed'); + console.log(num_failing + ' test(s) failed:'); + for (var i = 0; i < failures.length; i++) { + // TODO(danvk): print an auto_test/misc/local URL that runs this test. + console.log(' ' + failures[i] + ' failed.'); + } - if (num_failing == 0) { - console.log('PASS'); - } else { + done_callback(num_failing, num_passing); +}); + +}; + +// Load all "tests/" pages. +var LoadAllManualTests = function(totally_done_callback) { + +var fs = require('fs'); +var tests = fs.list('tests'); +var pages = []; + +function make_barrier_closure(n, fn) { + var calls = 0; + return function() { + calls++; + if (calls == n) { + fn(); + } else { + // console.log('' + calls + ' done, ' + (n - calls) + ' remain'); + } + }; +} + +var tasks = []; +for (var i = 0; i < tests.length; i++) { + if (tests[i].substr(-5) != '.html') continue; + tasks.push(tests[i]); +} +tasks = [ 'independent-series.html' ]; + +var loaded_page = make_barrier_closure(tasks.length, function() { + // Wait 2 secs to allow JS errors to happen after page load. + setTimeout(function() { + var success = 0, failures = 0; + for (var i = 0; i < pages.length; i++) { + if (pages[i].success && !pages[i].hasErrors) { + success++; + } else { + failures++; + } + } + console.log('Successfully loaded ' + success + ' / ' + + (success + failures) + ' test pages.'); + totally_done_callback(failures, success); + }, 2000); +}); + + +for (var i = 0; i < tasks.length; i++) { + var url = 'file://' + fs.absolute('tests/' + tasks[i]); + pages.push(function(path, url) { + var page = require('webpage').create(); + page.success = false; + page.hasErrors = false; + page.onError = function (msg, trace) { + console.log(path + ': ' + msg); + page.hasErrors = true; + trace.forEach(function(item) { + console.log(' ', item.file, ':', item.line); + }); + }; + page.onLoadFinished = function(status) { + if (status == 'success') { + page.success = true; + } + if (!page.done) loaded_page(); + page.done = true; + }; + page.open(url); + return page; + }(tasks[i], url)); +} + +}; + + +// First run all auto_tests. +// If they all pass, load the manual tests. +RunAllAutoTests(function(num_failing, num_passing) { + if (num_failing !== 0) { console.log('FAIL'); + phantom.exit(); + } else { + console.log('PASS'); } - phantom.exit(); + + // This is not yet reliable enough to be useful: + /* + LoadAllManualTests(function(failing, passing) { + if (failing !== 0) { + console.log('FAIL'); + } else { + console.log('PASS'); + } + phantom.exit(); + }); + */ });