runner.js 3.0 KB

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