run_from_console.casper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var Spooky = require('spooky');
  2. var path = require('path');
  3. var phantom_path = require('phantomjs').path;
  4. var casper_path = path.resolve(__dirname, '../node_modules/casperjs/bin/casperjs');
  5. process.env.PHANTOMJS_EXECUTABLE = phantom_path;
  6. var casper_opts = {
  7. child: {
  8. transport: 'http',
  9. command: casper_path
  10. },
  11. casper: {
  12. logLevel: 'debug',
  13. verbose: true
  14. }
  15. };
  16. var provide_emitter = function(file_paths, debug_port) {
  17. if (debug_port) {
  18. casper_opts.child['remote-debugger-port'] = debug_port;
  19. var debug_url = ('https://localhost:' + debug_port +
  20. '/webkit/inspector/inspector.html?page=');
  21. console.info('[remote-debugger] Navigate to ' + debug_url + '1 and ' +
  22. 'run `__run();` in the console to continue loading.' +
  23. '\n[remote-debugger] Navigate to ' + debug_url + '2 to ' +
  24. 'view the actual page source.\n' +
  25. '[remote-debugger] Use the `debugger;` statement to ' +
  26. 'trigger an initial breakpoint.');
  27. }
  28. var spooky = new Spooky(casper_opts, function(err) {
  29. if (err) {
  30. if (err.stack) console.warn(err.stack);
  31. else console.warn(err);
  32. return;
  33. }
  34. spooky.start('about:blank');
  35. file_paths.forEach(function(file_path, path_ind) {
  36. spooky.thenOpen('file://'+file_path);
  37. spooky.waitFor(function() {
  38. return this.getGlobal('__mocha_done') === true;
  39. },
  40. [{ path_ind: path_ind }, function() {
  41. var res_json = {
  42. file_ind: path_ind
  43. };
  44. res_json.num_tests = this.evaluate(function() { return document.querySelectorAll('li.test').length; });
  45. res_json.num_passes = this.evaluate(function() { return document.querySelectorAll('li.test.pass').length; });
  46. res_json.num_fails = this.evaluate(function() { return document.querySelectorAll('li.test.fail').length; });
  47. res_json.num_slow = this.evaluate(function() { return document.querySelectorAll('li.test.pass:not(.fast):not(.pending)').length; });
  48. res_json.num_skipped = this.evaluate(function () { return document.querySelectorAll('li.test.pending').length; });
  49. res_json.duration = this.evaluate(function() { return document.querySelector('li.duration em').textContent; });
  50. res_json.suites = this.evaluate(function() {
  51. var traverse_node = function(elem) {
  52. var res;
  53. if (elem.classList.contains('suite')) {
  54. res = {
  55. type: 'suite',
  56. name: elem.querySelector('h1').textContent,
  57. has_subfailures: elem.querySelectorAll('li.test.fail').length > 0,
  58. };
  59. var child_elems = elem.querySelector('ul').children;
  60. res.children = Array.prototype.map.call(child_elems, traverse_node);
  61. return res;
  62. }
  63. else {
  64. var h2_content = elem.querySelector('h2').childNodes;
  65. res = {
  66. type: 'test',
  67. text: h2_content[0].textContent,
  68. };
  69. if (elem.classList.contains('pass')) {
  70. res.pass = true;
  71. if (elem.classList.contains('pending')) {
  72. res.slow = false;
  73. res.skipped = true;
  74. }
  75. else {
  76. res.slow = !elem.classList.contains('fast');
  77. res.skipped = false;
  78. res.duration = h2_content[1].textContent;
  79. }
  80. }
  81. else {
  82. res.error = elem.querySelector('pre.error').textContent;
  83. }
  84. return res;
  85. }
  86. };
  87. var top_suites = document.querySelectorAll('#mocha-report > li.suite');
  88. return Array.prototype.map.call(top_suites, traverse_node);
  89. });
  90. res_json.replay = this.evaluate(function() { return document.querySelector('a.replay').textContent; });
  91. this.emit('test_ready', res_json);
  92. }]);
  93. });
  94. spooky.run();
  95. });
  96. return spooky;
  97. };
  98. module.exports = {
  99. provide_emitter: provide_emitter,
  100. name: 'SpookyJS (CapserJS on PhantomJS)'
  101. };