phantomasWrapper.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. */
  12. this.execute = function(data) {
  13. var deferred = Q.defer();
  14. var task = data.params;
  15. var options = {
  16. // Cusomizable options
  17. 'timeout': task.options.timeout || 60,
  18. 'js-deep-analysis': task.options.jsDeepAnalysis || false,
  19. 'user-agent': (task.options.device === 'desktop') ? 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36' : null,
  20. 'tablet': (task.options.device === 'tablet'),
  21. 'phone': (task.options.device === 'phone'),
  22. 'screenshot': task.options.screenshot || false,
  23. // Mandatory
  24. 'reporter': 'json:pretty',
  25. 'analyze-css': true,
  26. 'skip-modules': [
  27. 'blockDomains', // not needed
  28. 'domHiddenContent', // overriden
  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. 'jQuery', // overridden
  36. 'jserrors', // overridden
  37. 'pageSource', // not needed
  38. 'waitForSelector', // not needed
  39. 'windowPerformance' // overriden
  40. ].join(','),
  41. 'include-dirs': [
  42. path.join(__dirname, 'custom_modules/core'),
  43. path.join(__dirname, '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. var value = options[opt];
  51. if ((typeof value === 'string' || value instanceof String) && value.indexOf(' ') >= 0) {
  52. value = '"' + value + '"';
  53. }
  54. if (value === true) {
  55. optionsString += ' ' + '--' + opt;
  56. } else if (value === false || value === null) {
  57. // Nothing
  58. } else {
  59. optionsString += ' ' + '--' + opt + '=' + value;
  60. }
  61. }
  62. debug('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  63. // Kill the application if nothing happens
  64. var phantomasPid;
  65. var killer = setTimeout(function() {
  66. debug('Killing the app because the test on %s was launched %d seconds ago', task.url, 5*options.timeout);
  67. // If in server mode, forever will restart the server
  68. // Kill the Phantomas process first
  69. if (phantomasPid) {
  70. ps.kill(phantomasPid, function(err) {
  71. if (err) {
  72. debug('Could not kill Phantomas process %s', phantomasPid);
  73. }
  74. else {
  75. debug('Phantomas process %s was correctly killed', phantomasPid);
  76. }
  77. // Then suicide.
  78. process.exit(1);
  79. });
  80. }
  81. }, 5*options.timeout*1000);
  82. // It's time to launch the test!!!
  83. var triesNumber = 2;
  84. async.retry(triesNumber, function(cb) {
  85. var process = phantomas(task.url, options, function(err, json, results) {
  86. debug('Returning from Phantomas');
  87. // Adding some YellowLabTools errors here
  88. if (json && json.metrics && (!json.metrics.javascriptExecutionTree || !json.offenders.javascriptExecutionTree)) {
  89. err = 1001;
  90. }
  91. if (!err && (!json || !json.metrics)) {
  92. err = 1002;
  93. }
  94. // Don't cancel test if it is a timeout and we've got some results
  95. if (err === 252 && json) {
  96. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  97. err = null;
  98. }
  99. if (err) {
  100. debug('Attempt failed. Error code ' + err);
  101. }
  102. cb(err, json);
  103. });
  104. phantomasPid = process.pid;
  105. }, function(err, json) {
  106. clearTimeout(killer);
  107. if (err) {
  108. debug('All ' + triesNumber + ' attemps failed for the test');
  109. deferred.reject(err);
  110. } else {
  111. deferred.resolve(json);
  112. }
  113. });
  114. return deferred.promise;
  115. };
  116. };
  117. module.exports = new PhantomasWrapper();