phantomasWrapper.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 'engine': task.options.phantomasEngine || 'webkit',
  18. 'timeout': task.options.timeout || 30,
  19. 'js-deep-analysis': task.options.jsDeepAnalysis || false,
  20. 'user-agent': (task.options.device === 'desktop') ? 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) YLT Chrome/27.0.1453.110 Safari/537.36' : null,
  21. 'tablet': (task.options.device === 'tablet'),
  22. 'phone': (task.options.device === 'phone'),
  23. 'screenshot': task.options.screenshot || false,
  24. 'wait-for-selector': task.options.waitForSelector,
  25. 'cookie': task.options.cookie,
  26. 'auth-user': task.options.authUser,
  27. 'auth-pass': task.options.authPass,
  28. 'block-domain': task.options.blockDomain,
  29. 'allow-domain': task.options.allowDomain,
  30. 'no-externals': task.options.noExternals,
  31. // Mandatory
  32. 'reporter': 'json:pretty',
  33. 'analyze-css': true,
  34. 'skip-modules': [
  35. 'domHiddenContent', // overriden
  36. 'domMutations', // not compatible with webkit
  37. 'domQueries', // overriden
  38. 'events', // overridden
  39. 'filmStrip', // not needed
  40. 'har', // not needed for the moment
  41. 'javaScriptBottlenecks', // needs to be launched after custom module scopeYLT
  42. 'jQuery', // overridden
  43. 'jserrors', // overridden
  44. 'lazyLoadableImages', //overriden
  45. 'pageSource', // not needed
  46. 'windowPerformance' // overriden
  47. ].join(','),
  48. 'include-dirs': [
  49. path.join(__dirname, 'custom_modules/core'),
  50. path.join(__dirname, 'custom_modules/modules')
  51. ].join(',')
  52. };
  53. // Output the command line for debugging purpose
  54. debug('If you want to reproduce the phantomas task only, copy the following command line:');
  55. var optionsString = '';
  56. for (var opt in options) {
  57. var value = options[opt];
  58. if ((typeof value === 'string' || value instanceof String) && value.indexOf(' ') >= 0) {
  59. value = '"' + value + '"';
  60. }
  61. if (value === true) {
  62. optionsString += ' ' + '--' + opt;
  63. } else if (value === false || value === null || value === undefined) {
  64. // Nothing
  65. } else {
  66. optionsString += ' ' + '--' + opt + '=' + value;
  67. }
  68. }
  69. debug('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  70. var phantomasPid;
  71. var isKilled = false;
  72. // Kill phantomas if nothing happens
  73. var killer = setTimeout(function() {
  74. console.log('Killing phantomas because the test on ' + task.url + ' was launched ' + 5*options.timeout + ' seconds ago');
  75. if (phantomasPid) {
  76. ps.kill(phantomasPid, function(err) {
  77. if (err) {
  78. debug('Could not kill Phantomas process %s', phantomasPid);
  79. // Suicide
  80. process.exit(1);
  81. // If in server mode, forever will restart the server
  82. }
  83. debug('Phantomas process %s was correctly killed', phantomasPid);
  84. // Then mark the test as failed
  85. // Error 1003 = Phantomas not answering
  86. deferred.reject(1003);
  87. isKilled = true;
  88. });
  89. } else {
  90. // Suicide
  91. process.exit(1);
  92. }
  93. }, 5*options.timeout*1000);
  94. // It's time to launch the test!!!
  95. var triesNumber = 2;
  96. var currentTry = 0;
  97. async.retry(triesNumber, function(cb) {
  98. currentTry ++;
  99. var process = phantomas(task.url, options, function(err, json, results) {
  100. var errorCode = err ? parseInt(err.message, 10) : null;
  101. if (isKilled) {
  102. debug('Process was killed, too late Phantomas, sorry...');
  103. return;
  104. }
  105. debug('Returning from Phantomas with error %s', errorCode);
  106. // Adding some YellowLabTools errors here
  107. if (json && json.metrics && (!json.metrics.javascriptExecutionTree || !json.offenders.javascriptExecutionTree)) {
  108. errorCode = 1001;
  109. }
  110. if (!errorCode && (!json || !json.metrics)) {
  111. errorCode = 1002;
  112. }
  113. // Don't cancel test if it is a timeout and we've got some results
  114. if (errorCode === 252 && json) {
  115. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  116. errorCode = null;
  117. }
  118. if (errorCode) {
  119. debug('Attempt failed. Error code ' + errorCode);
  120. }
  121. cb(errorCode, json);
  122. });
  123. phantomasPid = process.pid;
  124. }, function(err, json) {
  125. clearTimeout(killer);
  126. if (err) {
  127. debug('All ' + triesNumber + ' attemps failed for the test');
  128. deferred.reject(err);
  129. } else {
  130. deferred.resolve(json);
  131. }
  132. });
  133. return deferred.promise;
  134. };
  135. };
  136. module.exports = new PhantomasWrapper();