phantomasWrapper.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var async = require('async');
  2. var Q = require('q');
  3. var ps = require('ps-node');
  4. var debug = require('debug')('ylt:phantomaswrapper');
  5. var phantomas = require('phantomas');
  6. var PhantomasWrapper = function() {
  7. 'use strict';
  8. /**
  9. * This is the phantomas launcher. It merges user chosen options into the default options
  10. * Available options :
  11. *
  12. * - timeout : in seconds (default 60)
  13. * - jsDeepAnalysis : should we inspect subrequests in the javascript execution tree?
  14. *
  15. */
  16. this.execute = function(data) {
  17. var deferred = Q.defer();
  18. var task = data.params;
  19. var options = {
  20. // Cusomizable options
  21. timeout: task.options.timeout || 60,
  22. 'js-deep-analysis': task.options.jsDeepAnalysis || false,
  23. // Mandatory
  24. reporter: 'json:pretty',
  25. 'analyze-css': true,
  26. 'skip-modules': [
  27. 'blockDomains', // not needed
  28. 'domMutations', // not compatible with webkit
  29. 'domQueries', // overriden
  30. 'eventListeners', // overridden
  31. 'filmStrip', // not needed
  32. 'har', // not needed for the moment
  33. 'javaScriptBottlenecks', // needs to be launched after custom module scopeYLT,
  34. 'jserrors', // overridden
  35. 'pageSource', // not needed
  36. 'screenshot', // not needed for the moment
  37. 'waitForSelector', // not needed
  38. 'windowPerformance' // overriden
  39. ].join(','),
  40. 'include-dirs': [
  41. 'lib/tools/phantomas/custom_modules/core',
  42. 'lib/tools/phantomas/custom_modules/modules'
  43. ].join(',')
  44. };
  45. // Output the command line for debugging purpose
  46. debug('If you want to reproduce the phantomas task only, copy the following command line:');
  47. var optionsString = '';
  48. for (var opt in options) {
  49. optionsString += ' ' + '--' + opt + '=' + options[opt];
  50. }
  51. debug('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  52. // Kill the application if nothing happens
  53. var phantomasPid;
  54. var killer = setTimeout(function() {
  55. debug('Killing the app because the test on %s was launched %d seconds ago', task.url, 5*options.timeout);
  56. // If in server mode, forever will restart the server
  57. // Kill the Phantomas process first
  58. if (phantomasPid) {
  59. ps.kill(phantomasPid, function(err) {
  60. if (err) {
  61. debug('Could not kill Phantomas process %s', phantomasPid);
  62. }
  63. else {
  64. debug('Phantomas process %s was correctly killed', phantomasPid);
  65. }
  66. // Then suicide.
  67. process.exit(1);
  68. });
  69. }
  70. }, 5*options.timeout*1000);
  71. // It's time to launch the test!!!
  72. var triesNumber = 2;
  73. async.retry(triesNumber, function(cb) {
  74. var process = phantomas(task.url, options, function(err, json, results) {
  75. debug('Returning from Phantomas');
  76. // Adding some YellowLabTools errors here
  77. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  78. err = 1001;
  79. }
  80. if (!err && (!json || !json.metrics)) {
  81. err = 1002;
  82. }
  83. // Don't cancel test if it is a timeout and we've got some results
  84. if (err === 252 && json) {
  85. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  86. err = null;
  87. }
  88. if (err) {
  89. debug('Attempt failed. Error code ' + err);
  90. }
  91. cb(err, json);
  92. });
  93. phantomasPid = process.pid;
  94. }, function(err, json) {
  95. clearTimeout(killer);
  96. if (err) {
  97. debug('All ' + triesNumber + ' attemps failed for the test');
  98. deferred.reject(err);
  99. } else {
  100. // Success!!!
  101. deferred.resolve(json);
  102. }
  103. });
  104. return deferred.promise;
  105. };
  106. };
  107. module.exports = new PhantomasWrapper();