runner.js 3.9 KB

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