phantomasWrapper.js 6.4 KB

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