phantomasWrapper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var async = require('async');
  2. var Q = require('q');
  3. var path = require('path');
  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. */
  11. this.execute = function(data) {
  12. var deferred = Q.defer();
  13. var task = data.params;
  14. var viewportOption = null;
  15. // Setting screen dimensions for desktop devices only.
  16. // Phone and tablet dimensions are dealt by Phantomas.
  17. if (task.options.device === 'desktop') {
  18. // Similar to an old non-retina Macbook Air 13"
  19. viewportOption = '1280x800x1';
  20. } else if (task.options.device === 'desktop-hd') {
  21. // Similar to a retina Macbook Pro 16"
  22. viewportOption = '1536x960x2';
  23. }
  24. var options = {
  25. // Cusomizable options
  26. 'timeout': task.options.timeout || 120,
  27. '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,
  28. 'tablet': (task.options.device === 'tablet'),
  29. 'phone': (task.options.device === 'phone'),
  30. 'screenshot': task.options.screenshot || false,
  31. 'viewport': viewportOption,
  32. 'wait-for-network-idle': true,
  33. 'cookie': task.options.cookie,
  34. 'auth-user': task.options.authUser,
  35. 'auth-pass': task.options.authPass,
  36. 'block-domain': task.options.blockDomain,
  37. 'allow-domain': task.options.allowDomain,
  38. 'no-externals': task.options.noExternals,
  39. // Mandatory
  40. 'analyze-css': true,
  41. 'ignoreSslErrors': true, // until Phantomas 2.1
  42. 'ignore-ssl-errors': true // for Phantomas >= 2.2
  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();