phantomasWrapper.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. 'cacheHits',
  27. 'caching',
  28. 'console',
  29. 'cookies',
  30. 'documentHeight',
  31. 'domains',
  32. 'domComplexity',
  33. 'domMutations',
  34. 'domQueries',
  35. 'filmStrip',
  36. 'jQuery',
  37. 'jserrors',
  38. 'har',
  39. 'headers',
  40. 'localStorage',
  41. 'mainRequest',
  42. 'pageSource',
  43. 'redirects',
  44. 'requestsStats',
  45. 'screenshot',
  46. 'staticAssets',
  47. 'timeToFirst',
  48. 'waitForSelector'
  49. ].join(','),
  50. 'include-dirs': [
  51. 'phantomas_custom/core',
  52. 'phantomas_custom/modules'
  53. ].join(',')
  54. };
  55. // Output the command line for debugging purpose
  56. console.log('If you want to reproduce the phantomas with command line:');
  57. var optionsString = '';
  58. for (var opt in options) {
  59. optionsString += ' ' + '--' + opt + '=' + options[opt];
  60. }
  61. console.log('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  62. // It's time to launch the test!!!
  63. var triesNumber = 3;
  64. async.retry(triesNumber, function(cb) {
  65. phantomas(task.url, options, function(err, json, results) {
  66. console.log('Returning from Phantomas');
  67. // Adding some YellowLabTools errors here
  68. if (!json.metrics.javascriptExecutionTree) {
  69. err = 1001;
  70. }
  71. if (err) {
  72. console.log('Attempt failed for test id ' + task.testId + '. Error code ' + err);
  73. }
  74. cb(err, {json: json, results: results});
  75. });
  76. }, function(err, data) {
  77. if (err) {
  78. console.log('All ' + triesNumber + ' attemps failed for test id ' + task.testId);
  79. }
  80. callback(err, data.json, data.results);
  81. });
  82. };
  83. };
  84. module.exports = new PhantomasWrapper();