run-jasmine2.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. var system = require('system');
  3. /**
  4. * Wait until the test condition is true or a timeout occurs. Useful for waiting
  5. * on a server response or for a ui change (fadeIn, etc.) to occur.
  6. *
  7. * @param testFx javascript condition that evaluates to a boolean,
  8. * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
  9. * as a callback function.
  10. * @param onReady what to do when testFx condition is fulfilled,
  11. * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
  12. * as a callback function.
  13. * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
  14. */
  15. function waitFor(testFx, onReady, timeOutMillis) {
  16. var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timeout is 3s
  17. start = new Date().getTime(),
  18. condition = false,
  19. interval = setInterval(function() {
  20. if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
  21. // If not time-out yet and condition not yet fulfilled
  22. condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
  23. } else {
  24. if(!condition) {
  25. // If condition still not fulfilled (timeout but condition is 'false')
  26. console.log("'waitFor()' timeout");
  27. phantom.exit(1);
  28. } else {
  29. // Condition fulfilled (timeout and/or condition is 'true')
  30. console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
  31. typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
  32. clearInterval(interval); //< Stop this interval
  33. }
  34. }
  35. }, 100); //< repeat check every 100ms
  36. };
  37. if (system.args.length !== 2) {
  38. console.log('Usage: run-jasmine2.js URL');
  39. phantom.exit(1);
  40. }
  41. var page = require('webpage').create();
  42. // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
  43. page.onConsoleMessage = function(msg) {
  44. console.log(msg);
  45. };
  46. page.open(system.args[1], function(status){
  47. if (status !== "success") {
  48. console.log("Unable to access network");
  49. phantom.exit();
  50. } else {
  51. waitFor(function(){
  52. return page.evaluate(function(){
  53. return (document.body.querySelector('.symbolSummary .pending') === null &&
  54. document.body.querySelector('.duration') !== null);
  55. });
  56. }, function(){
  57. var exitCode = page.evaluate(function(){
  58. console.log('');
  59. var title = 'Jasmine';
  60. var version = document.body.querySelector('.version').innerText;
  61. var duration = document.body.querySelector('.duration').innerText;
  62. var banner = title + ' ' + version + ' ' + duration;
  63. console.log(banner);
  64. var list = document.body.querySelectorAll('.results > .failures > .spec-detail.failed');
  65. if (list && list.length > 0) {
  66. console.log('');
  67. console.log(list.length + ' test(s) FAILED:');
  68. for (i = 0; i < list.length; ++i) {
  69. var el = list[i],
  70. desc = el.querySelector('.description'),
  71. msg = el.querySelector('.messages > .result-message');
  72. console.log('');
  73. console.log(desc.innerText);
  74. console.log(msg.innerText);
  75. console.log('');
  76. }
  77. return 1;
  78. } else {
  79. console.log(document.body.querySelector('.alert > .bar.passed,.alert > .bar.skipped').innerText);
  80. return 0;
  81. }
  82. });
  83. phantom.exit(exitCode);
  84. });
  85. }
  86. });