phantomasWrapper.js 6.1 KB

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