phantomasWrapper.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var async = require('async');
  2. var Q = require('q');
  3. var ps = require('ps-node');
  4. var debug = require('debug')('ylt:phantomaswrapper');
  5. var phantomas = require('phantomas');
  6. var PhantomasWrapper = function() {
  7. 'use strict';
  8. /**
  9. * This is the phantomas launcher. It merges user chosen options into the default options
  10. * Available options :
  11. *
  12. * - timeout : in seconds (default 60)
  13. * - jsDeepAnalysis : should we inspect subrequests in the javascript execution tree?
  14. *
  15. */
  16. this.execute = function(data) {
  17. var deferred = Q.defer();
  18. var task = data.params;
  19. var options = {
  20. // Cusomizable options
  21. timeout: task.options.timeout || 60,
  22. 'js-deep-analysis': task.options.jsDeepAnalysis || false,
  23. // Mandatory
  24. reporter: 'json:pretty',
  25. 'analyze-css': true,
  26. 'skip-modules': [
  27. 'blockDomains', // not needed
  28. 'domMutations', // not compatible with webkit
  29. 'domQueries', // overriden
  30. 'eventListeners', // overridden
  31. 'filmStrip', // not needed
  32. 'har', // not needed for the moment
  33. 'pageSource', // not needed
  34. 'screenshot', // not needed for the moment
  35. 'waitForSelector', // not needed
  36. 'windowPerformance' // overriden
  37. ].join(','),
  38. 'include-dirs': [
  39. 'lib/tools/phantomas/custom_modules/core',
  40. 'lib/tools/phantomas/custom_modules/modules'
  41. ].join(',')
  42. };
  43. // Output the command line for debugging purpose
  44. debug('If you want to reproduce the phantomas task only, copy the following command line:');
  45. var optionsString = '';
  46. for (var opt in options) {
  47. optionsString += ' ' + '--' + opt + '=' + options[opt];
  48. }
  49. debug('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  50. // Kill the application if nothing happens
  51. var phantomasPid;
  52. var killer = setTimeout(function() {
  53. debug('Killing the app because the test on %s was launched %d seconds ago', task.url, 5*options.timeout);
  54. // If in server mode, forever will restart the server
  55. // Kill the Phantomas process first
  56. if (phantomasPid) {
  57. ps.kill(phantomasPid, function(err) {
  58. if (err) {
  59. debug('Could not kill Phantomas process %s', phantomasPid);
  60. }
  61. else {
  62. debug('Phantomas process %s was correctly killed', phantomasPid);
  63. }
  64. // Then suicide.
  65. process.exit(1);
  66. });
  67. }
  68. }, 5*options.timeout*1000);
  69. // It's time to launch the test!!!
  70. var triesNumber = 2;
  71. async.retry(triesNumber, function(cb) {
  72. var process = phantomas(task.url, options, function(err, json, results) {
  73. debug('Returning from Phantomas');
  74. // Adding some YellowLabTools errors here
  75. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  76. err = 1001;
  77. }
  78. if (!err && (!json || !json.metrics)) {
  79. err = 1002;
  80. }
  81. // Don't cancel test if it is a timeout and we've got some results
  82. if (err === 252 && json) {
  83. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  84. err = null;
  85. }
  86. if (err) {
  87. debug('Attempt failed. Error code ' + err);
  88. }
  89. cb(err, json);
  90. });
  91. phantomasPid = process.pid;
  92. }, function(err, json) {
  93. clearTimeout(killer);
  94. if (err) {
  95. debug('All ' + triesNumber + ' attemps failed for the test');
  96. deferred.reject(err);
  97. } else {
  98. // Success!!!
  99. deferred.resolve(json);
  100. }
  101. });
  102. return deferred.promise;
  103. };
  104. };
  105. module.exports = new PhantomasWrapper();