phantomasWrapper.js 6.7 KB

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