phantomasWrapper.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. 'pageSource', // not needed
  35. 'screenshot', // not needed for the moment
  36. 'waitForSelector', // not needed
  37. 'windowPerformance' // overriden
  38. ].join(','),
  39. 'include-dirs': [
  40. 'lib/tools/phantomas/custom_modules/core',
  41. 'lib/tools/phantomas/custom_modules/modules'
  42. ].join(',')
  43. };
  44. // Output the command line for debugging purpose
  45. debug('If you want to reproduce the phantomas task only, copy the following command line:');
  46. var optionsString = '';
  47. for (var opt in options) {
  48. optionsString += ' ' + '--' + opt + '=' + options[opt];
  49. }
  50. debug('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  51. // Kill the application if nothing happens
  52. var phantomasPid;
  53. var killer = setTimeout(function() {
  54. debug('Killing the app because the test on %s was launched %d seconds ago', task.url, 5*options.timeout);
  55. // If in server mode, forever will restart the server
  56. // Kill the Phantomas process first
  57. if (phantomasPid) {
  58. ps.kill(phantomasPid, function(err) {
  59. if (err) {
  60. debug('Could not kill Phantomas process %s', phantomasPid);
  61. }
  62. else {
  63. debug('Phantomas process %s was correctly killed', phantomasPid);
  64. }
  65. // Then suicide.
  66. process.exit(1);
  67. });
  68. }
  69. }, 5*options.timeout*1000);
  70. // It's time to launch the test!!!
  71. var triesNumber = 2;
  72. async.retry(triesNumber, function(cb) {
  73. var process = phantomas(task.url, options, function(err, json, results) {
  74. debug('Returning from Phantomas');
  75. // Adding some YellowLabTools errors here
  76. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  77. err = 1001;
  78. }
  79. if (!err && (!json || !json.metrics)) {
  80. err = 1002;
  81. }
  82. // Don't cancel test if it is a timeout and we've got some results
  83. if (err === 252 && json) {
  84. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  85. err = null;
  86. }
  87. if (err) {
  88. debug('Attempt failed. Error code ' + err);
  89. }
  90. cb(err, json);
  91. });
  92. phantomasPid = process.pid;
  93. }, function(err, json) {
  94. clearTimeout(killer);
  95. if (err) {
  96. debug('All ' + triesNumber + ' attemps failed for the test');
  97. deferred.reject(err);
  98. } else {
  99. // Success!!!
  100. deferred.resolve(json);
  101. }
  102. });
  103. return deferred.promise;
  104. };
  105. };
  106. module.exports = new PhantomasWrapper();