phantomasWrapper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var async = require('async');
  2. var Q = require('q');
  3. var debug = require('debug')('ylt:phantomaswrapper');
  4. var phantomas = require('phantomas');
  5. var PhantomasWrapper = function() {
  6. 'use strict';
  7. /**
  8. * This is the phantomas launcher. It merges user chosen options into the default options
  9. * Available options :
  10. *
  11. * - timeout : in seconds (default 60)
  12. * - jsDeepAnalysis : should we inspect subrequests in the javascript execution tree?
  13. *
  14. */
  15. this.execute = function(data) {
  16. var deferred = Q.defer();
  17. var task = data.params;
  18. var options = {
  19. // Cusomizable options
  20. timeout: task.options.timeout || 60,
  21. 'js-deep-analysis': task.options.jsDeepAnalysis || false,
  22. // Mandatory
  23. reporter: 'json:pretty',
  24. 'analyze-css': true,
  25. 'skip-modules': [
  26. 'blockDomains', // not needed
  27. 'domComplexity', // overriden
  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. 'phantomas_custom/core',
  40. 'phantomas_custom/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 for 10 minutes
  51. var killer = setTimeout(function() {
  52. debug('Killing the app because the test on %s was launched %d seconds ago', task.url, 3*options.timeout);
  53. // If in server mode, forever will restart the server
  54. process.exit(1);
  55. }, 3*options.timeout*60*1000);
  56. // It's time to launch the test!!!
  57. var triesNumber = 2;
  58. async.retry(triesNumber, function(cb) {
  59. phantomas(task.url, options, function(err, json, results) {
  60. debug('Returning from Phantomas');
  61. // Adding some YellowLabTools errors here
  62. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  63. err = 1001;
  64. }
  65. if (!err && (!json || !json.metrics)) {
  66. err = 1002;
  67. }
  68. // Don't cancel test if it is a timeout and we've got some results
  69. if (err === 252 && json) {
  70. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  71. err = null;
  72. }
  73. if (err) {
  74. debug('Attempt failed. Error code ' + err);
  75. }
  76. cb(err, json);
  77. });
  78. }, function(err, json) {
  79. clearTimeout(killer);
  80. if (err) {
  81. debug('All ' + triesNumber + ' attemps failed for the test');
  82. deferred.reject(err);
  83. } else {
  84. // Success!!!
  85. deferred.resolve(json);
  86. }
  87. });
  88. return deferred.promise;
  89. };
  90. };
  91. module.exports = new PhantomasWrapper();