phantomasWrapper.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 'timeout': task.options.timeout || 45,
  18. 'js-deep-analysis': task.options.jsDeepAnalysis || false,
  19. 'user-agent': (task.options.device === 'desktop') ? 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) 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. // Mandatory
  28. 'reporter': 'json:pretty',
  29. 'analyze-css': true,
  30. 'skip-modules': [
  31. 'blockDomains', // not needed
  32. 'domHiddenContent', // overriden
  33. 'domMutations', // not compatible with webkit
  34. 'domQueries', // overriden
  35. 'events', // overridden
  36. 'filmStrip', // not needed
  37. 'har', // not needed for the moment
  38. 'javaScriptBottlenecks', // needs to be launched after custom module scopeYLT
  39. 'jQuery', // overridden
  40. 'jserrors', // overridden
  41. 'pageSource', // not needed
  42. 'windowPerformance' // overriden
  43. ].join(','),
  44. 'include-dirs': [
  45. path.join(__dirname, 'custom_modules/core'),
  46. path.join(__dirname, 'custom_modules/modules')
  47. ].join(',')
  48. };
  49. // Output the command line for debugging purpose
  50. debug('If you want to reproduce the phantomas task only, copy the following command line:');
  51. var optionsString = '';
  52. for (var opt in options) {
  53. var value = options[opt];
  54. if ((typeof value === 'string' || value instanceof String) && value.indexOf(' ') >= 0) {
  55. value = '"' + value + '"';
  56. }
  57. if (value === true) {
  58. optionsString += ' ' + '--' + opt;
  59. } else if (value === false || value === null) {
  60. // Nothing
  61. } else {
  62. optionsString += ' ' + '--' + opt + '=' + value;
  63. }
  64. }
  65. debug('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  66. // Kill the application if nothing happens
  67. var phantomasPid;
  68. var killer = setTimeout(function() {
  69. debug('Killing the app because the test on %s was launched %d seconds ago', task.url, 5*options.timeout);
  70. // If in server mode, forever will restart the server
  71. // Kill the Phantomas process first
  72. if (phantomasPid) {
  73. ps.kill(phantomasPid, function(err) {
  74. if (err) {
  75. debug('Could not kill Phantomas process %s', phantomasPid);
  76. }
  77. else {
  78. debug('Phantomas process %s was correctly killed', phantomasPid);
  79. }
  80. // Then suicide.
  81. process.exit(1);
  82. });
  83. }
  84. }, 5*options.timeout*1000);
  85. // It's time to launch the test!!!
  86. var triesNumber = 2;
  87. async.retry(triesNumber, function(cb) {
  88. var process = phantomas(task.url, options, function(err, json, results) {
  89. debug('Returning from Phantomas');
  90. // Adding some YellowLabTools errors here
  91. if (json && json.metrics && (!json.metrics.javascriptExecutionTree || !json.offenders.javascriptExecutionTree)) {
  92. err = 1001;
  93. }
  94. if (!err && (!json || !json.metrics)) {
  95. err = 1002;
  96. }
  97. // Don't cancel test if it is a timeout and we've got some results
  98. if (err === 252 && json) {
  99. debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  100. err = null;
  101. }
  102. if (err) {
  103. debug('Attempt failed. Error code ' + err);
  104. }
  105. cb(err, json);
  106. });
  107. phantomasPid = process.pid;
  108. }, function(err, json) {
  109. clearTimeout(killer);
  110. if (err) {
  111. debug('All ' + triesNumber + ' attemps failed for the test');
  112. deferred.reject(err);
  113. } else {
  114. deferred.resolve(json);
  115. }
  116. });
  117. return deferred.promise;
  118. };
  119. };
  120. module.exports = new PhantomasWrapper();