phantomasWrapper.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. 'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
  22. // Mandatory
  23. reporter: 'json:pretty',
  24. 'analyze-css': true,
  25. 'skip-modules': [
  26. 'analyzeCss', // overriden
  27. 'blockDomains', // not needed
  28. 'domComplexity', // overriden
  29. 'domMutations', // not compatible with webkit
  30. 'domQueries', // overriden
  31. 'eventListeners', // overridden
  32. 'filmStrip', // not needed
  33. 'har', // not needed for the moment
  34. 'pageSource', // not needed
  35. 'screenshot', // not needed for the moment
  36. 'waitForSelector', // not needed
  37. 'windowPerformance' // overriden
  38. ].join(','),
  39. 'include-dirs': [
  40. 'phantomas_custom/core',
  41. 'phantomas_custom/modules'
  42. ].join(',')
  43. };
  44. // Output the command line for debugging purpose
  45. console.log('If you want to reproduce the phantomas task only, copy the following command line:');
  46. var optionsString = '';
  47. for (var opt in options) {
  48. var value = options[opt];
  49. if ((typeof value === 'string' || value instanceof String) && value.indexOf(' ') >= 0) {
  50. value = '"' + value + '"';
  51. }
  52. optionsString += ' ' + '--' + opt + '=' + value;
  53. }
  54. console.log('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  55. // Kill the application if nothing happens for 10 minutes
  56. var killer = setTimeout(function() {
  57. console.log('Killing the server because the test ' + task.testId + ' on ' + task.url + ' was launched 10 minutes ago');
  58. // Forever will restart the server
  59. process.exit(1);
  60. }, 600000);
  61. // It's time to launch the test!!!
  62. var triesNumber = 3;
  63. async.retry(triesNumber, function(cb) {
  64. phantomas(task.url, options, function(err, json, results) {
  65. console.log('Returning from Phantomas');
  66. // Adding some YellowLabTools errors here
  67. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  68. err = 1001;
  69. }
  70. if (!err && (!json || !json.metrics)) {
  71. err = 1002;
  72. }
  73. // Don't cancel test if it is a timeout and we've got some results
  74. if (err === 252 && json) {
  75. console.log('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  76. err = null;
  77. }
  78. if (err) {
  79. console.log('Attempt failed for test id ' + task.testId + '. Error code ' + err);
  80. }
  81. cb(err, {json: json, results: results});
  82. });
  83. }, function(err, data) {
  84. clearTimeout(killer);
  85. if (err) {
  86. console.log('All ' + triesNumber + ' attemps failed for test id ' + task.testId);
  87. }
  88. callback(err, data.json, data.results);
  89. });
  90. };
  91. };
  92. module.exports = new PhantomasWrapper();