phantomasWrapper.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Yellow Lab Tools main file
  3. */
  4. var async = require('async');
  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. * Available options :
  11. *
  12. * - timeout : in seconds (default 60)
  13. * - jsDeepAnalysis : should we inspect subrequests in the javascript execution tree (reported durations of main tasks will be slower than usual)
  14. *
  15. */
  16. this.execute = function(task, callback) {
  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. 'skip-modules': [
  24. //'ajaxRequests',
  25. //'alerts',
  26. 'blockDomains',
  27. //'cacheHits',
  28. //'caching',
  29. //'console',
  30. //'cookies',
  31. //'documentHeight',
  32. //'domains',
  33. 'domComplexity',
  34. 'domMutations',
  35. 'domQueries',
  36. 'eventListeners',
  37. 'filmStrip',
  38. //'jQuery',
  39. //'jserrors',
  40. 'har',
  41. //'headers',
  42. //'localStorage',
  43. //'mainRequest',
  44. 'pageSource',
  45. //'redirects',
  46. //'requestsStats',
  47. 'screenshot',
  48. //'staticAssets',
  49. //'timeToFirst',
  50. 'waitForSelector',
  51. 'windowPerformance'
  52. ].join(','),
  53. 'include-dirs': [
  54. 'phantomas_custom/core',
  55. 'phantomas_custom/modules'
  56. ].join(',')
  57. };
  58. // Output the command line for debugging purpose
  59. console.log('If you want to reproduce the phantomas task only, copy the following command line:');
  60. var optionsString = '';
  61. for (var opt in options) {
  62. optionsString += ' ' + '--' + opt + '=' + options[opt];
  63. }
  64. console.log('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  65. // It's time to launch the test!!!
  66. var triesNumber = 3;
  67. async.retry(triesNumber, function(cb) {
  68. phantomas(task.url, options, function(err, json, results) {
  69. console.log('Returning from Phantomas');
  70. // Adding some YellowLabTools errors here
  71. if (!json || !json.metrics || !json.metrics.javascriptExecutionTree) {
  72. err = 1001;
  73. }
  74. if (err) {
  75. console.log('Attempt failed for test id ' + task.testId + '. Error code ' + err);
  76. }
  77. cb(err, {json: json, results: results});
  78. });
  79. }, function(err, data) {
  80. if (err) {
  81. console.log('All ' + triesNumber + ' attemps failed for test id ' + task.testId);
  82. }
  83. callback(err, data.json, data.results);
  84. });
  85. };
  86. };
  87. module.exports = new PhantomasWrapper();