phantomasWrapper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 viewportOption = null;
  16. // Setting screen dimensions for desktop devices only.
  17. // Phone and tablet dimensions are dealt by Phantomas.
  18. if (task.options.device === 'desktop') {
  19. // Similar to an old non-retina Macbook Air 13"
  20. viewportOption = '1280x800x1';
  21. } else if (task.options.device === 'desktop-hd') {
  22. // Similar to a retina Macbook Pro 16"
  23. viewportOption = '1536x960x2';
  24. }
  25. var options = {
  26. // Cusomizable options
  27. 'timeout': task.options.timeout || 60,
  28. 'user-agent': (task.options.device === 'desktop') ? 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) YLT Chrome/85.0.4183.121 Safari/537.36' : null,
  29. 'tablet': (task.options.device === 'tablet'),
  30. 'phone': (task.options.device === 'phone'),
  31. 'screenshot': task.options.screenshot || false,
  32. 'viewport': viewportOption,
  33. 'wait-for-network-idle': true,
  34. //'wait-for-selector': task.options.waitForSelector,
  35. 'cookie': task.options.cookie,
  36. 'auth-user': task.options.authUser,
  37. 'auth-pass': task.options.authPass,
  38. 'block-domain': task.options.blockDomain,
  39. 'allow-domain': task.options.allowDomain,
  40. 'no-externals': task.options.noExternals,
  41. // Mandatory
  42. 'analyze-css': true,
  43. 'ignore-ssl-errors': true
  44. };
  45. // Proxy option can't be set to null or undefined...
  46. // this is why it's set now and not in the object above
  47. if (task.options.proxy) {
  48. options.proxy = task.options.proxy;
  49. }
  50. // It's time to launch the test!!!
  51. const promise = phantomas(task.url, options);
  52. // handle the promise
  53. promise.
  54. then(results => {
  55. var json = {
  56. generator: results.getGenerator(),
  57. url: results.getUrl(),
  58. metrics: results.getMetrics(),
  59. offenders: results.getAllOffenders()
  60. };
  61. deferred.resolve(json);
  62. }).
  63. catch(res => {
  64. console.error(res);
  65. deferred.reject('Phantomas failed: ' + res.message);
  66. });
  67. /*var process = phantomas(task.url, options, function(err, json, results) {
  68. var errorCode = err ? parseInt(err.message, 10) : null;
  69. debug('Returning from Phantomas with error %s', errorCode);
  70. // Adding some YellowLabTools errors here
  71. if (json && json.metrics && (!json.metrics.javascriptExecutionTree || !json.offenders.javascriptExecutionTree)) {
  72. errorCode = 1001;
  73. }
  74. if (!errorCode && (!json || !json.metrics)) {
  75. errorCode = 1002;
  76. }
  77. // Don't cancel test if it is a timeout and we've got some results
  78. if (errorCode === 252 && json) {
  79. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  80. errorCode = null;
  81. }
  82. if (errorCode) {
  83. debug('Attempt failed. Error code ' + errorCode);
  84. }
  85. }, function(err, json) {
  86. if (err) {
  87. debug('All ' + triesNumber + ' attemps failed for the test');
  88. deferred.reject(err);
  89. } else {
  90. deferred.resolve(json);
  91. }
  92. });
  93. */
  94. return deferred.promise;
  95. };
  96. };
  97. module.exports = new PhantomasWrapper();