phantomasWrapper.js 5.0 KB

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