run-jasmine.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 : 3001, //< Default Max Timeout 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 100ms
  34. };
  35. if (phantom.args.length === 0 || phantom.args.length > 2) {
  36. console.log('Usage: run-jasmine.js URL');
  37. phantom.exit();
  38. }
  39. var page = typeof require!='undefined'? require('webpage').create() : typeof WebPage!='undefined' ? new WebPage() : null;
  40. if(page){
  41. // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
  42. page.onConsoleMessage = function(msg) {
  43. console.log(msg);
  44. };
  45. page.open(phantom.args[0], function(status){
  46. if (status !== "success") {
  47. console.log("Unable to access network");
  48. phantom.exit();
  49. }
  50. else {
  51. waitFor(
  52. function(){//waiting for this to return true
  53. return page.evaluate(function(){
  54. var runner = document.body.querySelector('.runner');
  55. if(!runner){
  56. return !!runner;
  57. }
  58. return !!runner.querySelector('.description');
  59. });
  60. },
  61. function(){
  62. page.evaluate( function() {
  63. var suites = document.body.querySelectorAll('.suite');
  64. for (var i = 0; i < suites.length; i++){
  65. var suite = suites[i];
  66. var suiteName = suite.querySelector('.description').innerText;
  67. var passOrFail = suite.className.indexOf('passed') != -1 ? "Passed" : "Failed!";
  68. console.log('Suite: '+suiteName+'\t'+passOrFail);
  69. console.log('--------------------------------------------------------');
  70. var specs = suite.querySelectorAll('.spec');
  71. for (var j = 0; j < specs.length; j++){
  72. var spec = specs[j];
  73. var passed = spec.className.indexOf('passed') != -1;
  74. var specName = spec.querySelector('.description').innerText;
  75. var passOrFail = passed ? 'Passed' : "Failed!"
  76. console.log('\t'+specName+'\t'+passOrFail);
  77. if(!passed){
  78. console.log('\t\t-> Message: '+spec.querySelector('.resultMessage.fail').innerText);
  79. var trace = spec.querySelector('.stackTrace');
  80. console.log('\t\t-> Stack: '+(trace!=null ? trace.innerText : 'not supported by phantomJS yet'));
  81. }
  82. }
  83. console.log('');
  84. }
  85. var runner = document.body.querySelector('.runner');
  86. console.log('--------------------------------------------------------');
  87. console.log('Finished: '+runner.querySelector('.description').innerText);
  88. });
  89. console.log('');
  90. phantom.exit();
  91. },
  92. 300001
  93. );
  94. }
  95. });
  96. }
  97. else {
  98. console.log('Could not create WebPage');
  99. phantom.exit();
  100. }