phantomasWrapper.js 5.0 KB

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