phantomasWrapper.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 'analyze-css': true,
  24. 'skip-modules': [
  25. //'ajaxRequests',
  26. //'alerts',
  27. 'blockDomains',
  28. //'cacheHits',
  29. //'caching',
  30. //'console',
  31. //'cookies',
  32. //'documentHeight',
  33. //'domains',
  34. 'domComplexity',
  35. 'domMutations',
  36. 'domQueries',
  37. 'eventListeners',
  38. 'filmStrip',
  39. //'jQuery',
  40. //'jserrors',
  41. 'har',
  42. //'headers',
  43. //'localStorage',
  44. //'mainRequest',
  45. 'pageSource',
  46. //'redirects',
  47. //'requestsStats',
  48. 'screenshot',
  49. //'staticAssets',
  50. //'timeToFirst',
  51. 'waitForSelector',
  52. 'windowPerformance'
  53. ].join(','),
  54. 'include-dirs': [
  55. 'phantomas_custom/core',
  56. 'phantomas_custom/modules'
  57. ].join(',')
  58. };
  59. // Output the command line for debugging purpose
  60. console.log('If you want to reproduce the phantomas task only, copy the following command line:');
  61. var optionsString = '';
  62. for (var opt in options) {
  63. optionsString += ' ' + '--' + opt + '=' + options[opt];
  64. }
  65. console.log('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  66. // Kill the application if nothing happens for 10 minutes
  67. var killer = setTimeout(function() {
  68. console.log('Killing the server because the test ' + task.testId + ' on ' + task.url + ' was launched 10 minutes ago');
  69. // Forever will restart the server
  70. process.exit(1);
  71. }, 600000);
  72. // It's time to launch the test!!!
  73. var triesNumber = 3;
  74. async.retry(triesNumber, function(cb) {
  75. phantomas(task.url, options, function(err, json, results) {
  76. console.log('Returning from Phantomas');
  77. // Adding some YellowLabTools errors here
  78. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  79. err = 1001;
  80. }
  81. if (!json || !json.metrics) {
  82. err = 1002;
  83. }
  84. // Don't cancel test if it is a timeout and we've got some results
  85. if (err === 252 && json) {
  86. console.log('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  87. err = null;
  88. }
  89. if (err) {
  90. console.log('Attempt failed for test id ' + task.testId + '. Error code ' + err);
  91. }
  92. cb(err, {json: json, results: results});
  93. });
  94. }, function(err, data) {
  95. clearTimeout(killer);
  96. if (err) {
  97. console.log('All ' + triesNumber + ' attemps failed for test id ' + task.testId);
  98. }
  99. callback(err, data.json, data.results);
  100. });
  101. };
  102. };
  103. module.exports = new PhantomasWrapper();