phantomasWrapper.js 3.1 KB

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