run-qunit.js 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Wait until the test condition is true or a timeout occurs. Useful for waiting
  3. * on a server response or for a ui change (fadeIn, etc.) to occur.
  4. *
  5. * @param testFx javascript condition that evaluates to a boolean,
  6. * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
  7. * as a callback function.
  8. * @param onReady what to do when testFx condition is fulfilled,
  9. * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
  10. * as a callback function.
  11. * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
  12. */
  13. function waitFor(testFx, onReady, timeOutMillis) {
  14. var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 30001, //< Default Max Timout is 3s
  15. start = new Date().getTime(),
  16. condition = false,
  17. interval = setInterval(function() {
  18. if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
  19. // If not time-out yet and condition not yet fulfilled
  20. condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
  21. } else {
  22. if(!condition) {
  23. // If condition still not fulfilled (timeout but condition is 'false')
  24. console.log("'waitFor()' timeout");
  25. phantom.exit(1);
  26. } else {
  27. // Condition fulfilled (timeout and/or condition is 'true')
  28. // console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
  29. typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
  30. clearInterval(interval); //< Stop this interval
  31. }
  32. }
  33. }, 100); //< repeat check every 250ms
  34. };
  35. if (phantom.args.length === 0 || phantom.args.length > 2) {
  36. console.log('Usage: run-qunit.js URL');
  37. phantom.exit(1);
  38. }
  39. var page = require('webpage').create();
  40. // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
  41. page.onConsoleMessage = function(msg) {
  42. console.log(msg);
  43. };
  44. page.open(phantom.args[0], function(status){
  45. if (status !== "success") {
  46. console.log("Unable to access network");
  47. phantom.exit(1);
  48. } else {
  49. console.log("-------------");
  50. console.log("Running QUnit tests for: " + phantom.args[0]);
  51. console.log("-------------");
  52. waitFor(function(){
  53. return page.evaluate(function(){
  54. var el = document.getElementById('qunit-testresult');
  55. if (el && el.innerText.match('completed')) {
  56. return true;
  57. }
  58. return false;
  59. });
  60. }, function(){
  61. var failedNum = page.evaluate(function(){
  62. // Print out any failed tests
  63. var failedTests = document.body.querySelectorAll('#qunit-tests > .fail');
  64. for (var i = 0; i < failedTests.length; ++i) {
  65. var failedTest = failedTests[i];
  66. var testName = failedTest.querySelector('.test-name').innerText;
  67. console.log(String.fromCharCode( "0x2717" ) + " Test failed: " + testName);
  68. var failedAsserts = failedTest.getElementsByClassName('fail')
  69. for (var j = 0; j < failedAsserts.length; ++j) {
  70. var failedAssert = failedAsserts[j];
  71. var assertMessage = failedAssert.querySelector('.test-message').innerText;
  72. console.log(" " + String.fromCharCode( "0x21B3" ) + " Failed assertion: " + assertMessage);
  73. };
  74. };
  75. var el = document.getElementById('qunit-testresult');
  76. console.log("-------------");
  77. console.log(el.innerText);
  78. console.log("-------------");
  79. try {
  80. return el.getElementsByClassName('failed')[0].innerHTML;
  81. } catch (e) { }
  82. return 10000;
  83. });
  84. phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);
  85. });
  86. }
  87. });