phantomasWrapper.js 6.5 KB

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