|
| 1 | +/* |
| 2 | + * PhantomJS Runner QUnit Plugin 1.2.0 |
| 3 | + * |
| 4 | + * PhantomJS binaries: http://phantomjs.org/download.html |
| 5 | + * Requires PhantomJS 1.6+ (1.7+ recommended) |
| 6 | + * |
| 7 | + * Run with: |
| 8 | + * phantomjs runner.js [url-of-your-qunit-testsuite] |
| 9 | + * |
| 10 | + * e.g. |
| 11 | + * phantomjs runner.js http://localhost/qunit/test/index.html |
| 12 | + */ |
| 13 | + |
| 14 | +/*global phantom:false, require:false, console:false, window:false, QUnit:false */ |
| 15 | + |
| 16 | +(function() { |
| 17 | + 'use strict'; |
| 18 | + |
| 19 | + var url, page, timeout, |
| 20 | + args = require('system').args; |
| 21 | + |
| 22 | + // arg[0]: scriptName, args[1...]: arguments |
| 23 | + if (args.length < 2 || args.length > 3) { |
| 24 | + console.error('Usage:\n phantomjs runner.js [url-of-your-qunit-testsuite] [timeout-in-seconds]'); |
| 25 | + phantom.exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + url = args[1]; |
| 29 | + page = require('webpage').create(); |
| 30 | + if (args[2] !== undefined) { |
| 31 | + timeout = parseInt(args[2], 10); |
| 32 | + } |
| 33 | + |
| 34 | + // Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`) |
| 35 | + page.onConsoleMessage = function(msg) { |
| 36 | + console.log(msg); |
| 37 | + }; |
| 38 | + |
| 39 | + page.onInitialized = function() { |
| 40 | + page.evaluate(addLogging); |
| 41 | + }; |
| 42 | + |
| 43 | + page.onCallback = function(message) { |
| 44 | + var result, |
| 45 | + failed; |
| 46 | + |
| 47 | + if (message) { |
| 48 | + if (message.name === 'QUnit.done') { |
| 49 | + result = message.data; |
| 50 | + failed = !result || !result.total || result.failed; |
| 51 | + |
| 52 | + if (!result.total) { |
| 53 | + console.error('No tests were executed. Are you loading tests asynchronously?'); |
| 54 | + } |
| 55 | + |
| 56 | + phantom.exit(failed ? 1 : 0); |
| 57 | + } |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + page.open(url, function(status) { |
| 62 | + if (status !== 'success') { |
| 63 | + console.error('Unable to access network: ' + status); |
| 64 | + phantom.exit(1); |
| 65 | + } else { |
| 66 | + // Cannot do this verification with the 'DOMContentLoaded' handler because it |
| 67 | + // will be too late to attach it if a page does not have any script tags. |
| 68 | + var qunitMissing = page.evaluate(function() { return (typeof QUnit === 'undefined' || !QUnit); }); |
| 69 | + if (qunitMissing) { |
| 70 | + console.error('The `QUnit` object is not present on this page.'); |
| 71 | + phantom.exit(1); |
| 72 | + } |
| 73 | + |
| 74 | + // Set a timeout on the test running, otherwise tests with async problems will hang forever |
| 75 | + if (typeof timeout === 'number') { |
| 76 | + setTimeout(function() { |
| 77 | + console.error('The specified timeout of ' + timeout + ' seconds has expired. Aborting...'); |
| 78 | + phantom.exit(1); |
| 79 | + }, timeout * 1000); |
| 80 | + } |
| 81 | + |
| 82 | + // Do nothing... the callback mechanism will handle everything! |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + function addLogging() { |
| 87 | + window.document.addEventListener('DOMContentLoaded', function() { |
| 88 | + var currentTestAssertions = []; |
| 89 | + |
| 90 | + QUnit.log(function(details) { |
| 91 | + var response; |
| 92 | + |
| 93 | + // Ignore passing assertions |
| 94 | + if (details.result) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + response = details.message || ''; |
| 99 | + |
| 100 | + if (typeof details.expected !== 'undefined') { |
| 101 | + if (response) { |
| 102 | + response += ', '; |
| 103 | + } |
| 104 | + |
| 105 | + response += 'expected: ' + details.expected + ', but was: ' + details.actual; |
| 106 | + } |
| 107 | + |
| 108 | + if (details.source) { |
| 109 | + response += "\n" + details.source; |
| 110 | + } |
| 111 | + |
| 112 | + currentTestAssertions.push('Failed assertion: ' + response); |
| 113 | + }); |
| 114 | + |
| 115 | + QUnit.testDone(function(result) { |
| 116 | + var i, |
| 117 | + len, |
| 118 | + name = ''; |
| 119 | + |
| 120 | + if (result.module) { |
| 121 | + name += result.module + ': '; |
| 122 | + } |
| 123 | + name += result.name; |
| 124 | + |
| 125 | + if (result.failed) { |
| 126 | + console.log('\n' + 'Test failed: ' + name); |
| 127 | + |
| 128 | + for (i = 0, len = currentTestAssertions.length; i < len; i++) { |
| 129 | + console.log(' ' + currentTestAssertions[i]); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + currentTestAssertions.length = 0; |
| 134 | + }); |
| 135 | + |
| 136 | + QUnit.done(function(result) { |
| 137 | + console.log('\n' + 'Took ' + result.runtime + 'ms to run ' + result.total + ' tests. ' + result.passed + ' passed, ' + result.failed + ' failed.'); |
| 138 | + |
| 139 | + if (typeof window.callPhantom === 'function') { |
| 140 | + window.callPhantom({ |
| 141 | + 'name': 'QUnit.done', |
| 142 | + 'data': result |
| 143 | + }); |
| 144 | + } |
| 145 | + }); |
| 146 | + }, false); |
| 147 | + } |
| 148 | +})(); |
0 commit comments