phantomasWrapper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, // until Phantomas 2.1
  43. 'ignore-ssl-errors': true // for Phantomas >= 2.2
  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. var debugCmd = 'DEBUG=* node node_modules/phantomas/bin/phantomas.js --url ' + task.url;
  51. Object.keys(options).forEach(function(key) {
  52. if (key !== 'wait-for-network-idle' && options[key] !== null) {
  53. debugCmd += ' --' + key + ' ' + options[key];
  54. }
  55. });
  56. debug('If you want to run Phantomas alone for debugging purpose, this is the command: %s', debugCmd);
  57. // It's time to launch the test!!!
  58. const promise = phantomas(task.url, options);
  59. // handle the promise
  60. promise.
  61. then(results => {
  62. var json = {
  63. generator: results.getGenerator(),
  64. url: results.getUrl(),
  65. metrics: results.getMetrics(),
  66. offenders: results.getAllOffenders()
  67. };
  68. deferred.resolve(json);
  69. }).
  70. catch(res => {
  71. console.error(res);
  72. deferred.reject('Phantomas failed: ' + res.message);
  73. });
  74. promise.on('milestone', function(event) {
  75. if (event === 'domReady' || event === 'domComplete') {
  76. deferred.notify(event);
  77. }
  78. });
  79. return deferred.promise;
  80. };
  81. };
  82. module.exports = new PhantomasWrapper();