phantomasWrapper.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 || 120,
  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. 'cookie': task.options.cookie,
  35. 'auth-user': task.options.authUser,
  36. 'auth-pass': task.options.authPass,
  37. 'block-domain': task.options.blockDomain,
  38. 'allow-domain': task.options.allowDomain,
  39. 'no-externals': task.options.noExternals,
  40. // Mandatory
  41. 'analyze-css': true,
  42. 'ignoreSslErrors': true
  43. };
  44. // Proxy option can't be set to null or undefined...
  45. // this is why it's set now and not in the object above
  46. if (task.options.proxy) {
  47. options.proxy = task.options.proxy;
  48. }
  49. var debugCmd = 'DEBUG=* node node_modules/phantomas/bin/phantomas.js --url ' + task.url;
  50. Object.keys(options).forEach(function(key) {
  51. if (key !== 'wait-for-network-idle' && options[key] !== null) {
  52. debugCmd += ' --' + key + ' ' + options[key];
  53. }
  54. });
  55. debug('If you want to run Phantomas alone for debugging purpose, this is the command: %s', debugCmd);
  56. // It's time to launch the test!!!
  57. const promise = phantomas(task.url, options);
  58. // handle the promise
  59. promise.
  60. then(results => {
  61. var json = {
  62. generator: results.getGenerator(),
  63. url: results.getUrl(),
  64. metrics: results.getMetrics(),
  65. offenders: results.getAllOffenders()
  66. };
  67. deferred.resolve(json);
  68. }).
  69. catch(res => {
  70. console.error(res);
  71. deferred.reject('Phantomas failed: ' + res.message);
  72. });
  73. promise.on('milestone', function(event) {
  74. if (event === 'domReady' || event === 'domComplete') {
  75. deferred.notify(event);
  76. }
  77. });
  78. return deferred.promise;
  79. };
  80. };
  81. module.exports = new PhantomasWrapper();