phantomasWrapper.js 4.6 KB

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