phantomasWrapper.js 5.0 KB

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