phantomasWrapper.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // It's time to launch the test!!!
  77. const promise = phantomas(task.url, {
  78. 'analyze-css': true
  79. });
  80. // handle the promise
  81. promise.
  82. then(results => {
  83. var json = {
  84. generator: results.getGenerator(),
  85. url: results.getUrl(),
  86. metrics: results.getMetrics(),
  87. offenders: results.getAllOffenders()
  88. };
  89. deferred.resolve(json);
  90. }).
  91. catch(res => {
  92. console.error(res);
  93. deferred.reject('Phantomas failed: ' + res.message);
  94. });
  95. /*var process = phantomas(task.url, options, function(err, json, results) {
  96. var errorCode = err ? parseInt(err.message, 10) : null;
  97. debug('Returning from Phantomas with error %s', errorCode);
  98. // Adding some YellowLabTools errors here
  99. if (json && json.metrics && (!json.metrics.javascriptExecutionTree || !json.offenders.javascriptExecutionTree)) {
  100. errorCode = 1001;
  101. }
  102. if (!errorCode && (!json || !json.metrics)) {
  103. errorCode = 1002;
  104. }
  105. // Don't cancel test if it is a timeout and we've got some results
  106. if (errorCode === 252 && json) {
  107. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  108. errorCode = null;
  109. }
  110. if (errorCode) {
  111. debug('Attempt failed. Error code ' + errorCode);
  112. }
  113. }, function(err, json) {
  114. if (err) {
  115. debug('All ' + triesNumber + ' attemps failed for the test');
  116. deferred.reject(err);
  117. } else {
  118. deferred.resolve(json);
  119. }
  120. });
  121. */
  122. return deferred.promise;
  123. };
  124. };
  125. module.exports = new PhantomasWrapper();