phantomasWrapper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var async = require('async');
  2. var Q = require('q');
  3. var phantomas = require('phantomas');
  4. var PhantomasWrapper = function() {
  5. 'use strict';
  6. /**
  7. * This is the phantomas launcher. It merges user chosen options into the default options
  8. * Available options :
  9. *
  10. * - timeout : in seconds (default 60)
  11. * - jsDeepAnalysis : should we inspect subrequests in the javascript execution tree?
  12. *
  13. */
  14. this.execute = function(data) {
  15. var deferred = Q.defer();
  16. var task = data.params;
  17. var options = {
  18. // Cusomizable options
  19. timeout: task.options.timeout || 60,
  20. 'js-deep-analysis': task.options.jsDeepAnalysis || false,
  21. // Mandatory
  22. reporter: 'json:pretty',
  23. 'analyze-css': true,
  24. 'skip-modules': [
  25. 'blockDomains', // not needed
  26. 'domComplexity', // overriden
  27. 'domMutations', // not compatible with webkit
  28. 'domQueries', // overriden
  29. 'eventListeners', // overridden
  30. 'filmStrip', // not needed
  31. 'har', // not needed for the moment
  32. 'pageSource', // not needed
  33. 'screenshot', // not needed for the moment
  34. 'waitForSelector', // not needed
  35. 'windowPerformance' // overriden
  36. ].join(','),
  37. 'include-dirs': [
  38. 'phantomas_custom/core',
  39. 'phantomas_custom/modules'
  40. ].join(',')
  41. };
  42. // Output the command line for debugging purpose
  43. console.log('If you want to reproduce the phantomas task only, copy the following command line:');
  44. var optionsString = '';
  45. for (var opt in options) {
  46. optionsString += ' ' + '--' + opt + '=' + options[opt];
  47. }
  48. console.log('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  49. // Kill the application if nothing happens for 10 minutes
  50. var killer = setTimeout(function() {
  51. console.log('Killing the app because the test on ' + task.url + ' was launched 10 minutes ago');
  52. // If in server mode, forever will restart the server
  53. process.exit(1);
  54. }, 600000);
  55. // It's time to launch the test!!!
  56. var triesNumber = 3;
  57. async.retry(triesNumber, function(cb) {
  58. phantomas(task.url, options, function(err, json, results) {
  59. console.log('Returning from Phantomas');
  60. // Adding some YellowLabTools errors here
  61. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  62. err = 1001;
  63. }
  64. if (!err && (!json || !json.metrics)) {
  65. err = 1002;
  66. }
  67. // Don't cancel test if it is a timeout and we've got some results
  68. if (err === 252 && json) {
  69. console.log('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  70. err = null;
  71. }
  72. if (err) {
  73. console.log('Attempt failed. Error code ' + err);
  74. }
  75. cb(err, json);
  76. });
  77. }, function(err, json) {
  78. clearTimeout(killer);
  79. if (err) {
  80. console.log('All ' + triesNumber + ' attemps failed for the test');
  81. deferred.reject(err);
  82. } else {
  83. // Success!!!
  84. deferred.resolve(json);
  85. }
  86. });
  87. return deferred.promise;
  88. };
  89. };
  90. module.exports = new PhantomasWrapper();