phantomasWrapper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. 'blockDomains', // not needed
  27. 'domComplexity', // overriden
  28. 'domMutations', // not compatible with webkit
  29. 'domQueries', // overriden
  30. 'eventListeners', // overridden
  31. 'filmStrip', // not needed
  32. 'har', // not needed for the moment
  33. 'pageSource', // not needed
  34. 'screenshot', // not needed for the moment
  35. 'waitForSelector', // not needed
  36. 'windowPerformance' // overriden
  37. ].join(','),
  38. 'include-dirs': [
  39. 'phantomas_custom/core',
  40. 'phantomas_custom/modules'
  41. ].join(',')
  42. };
  43. // Output the command line for debugging purpose
  44. console.log('If you want to reproduce the phantomas task only, copy the following command line:');
  45. var optionsString = '';
  46. for (var opt in options) {
  47. optionsString += ' ' + '--' + opt + '=' + options[opt];
  48. }
  49. console.log('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  50. // Kill the application if nothing happens for 10 minutes
  51. var killer = setTimeout(function() {
  52. console.log('Killing the server because the test ' + task.testId + ' on ' + task.url + ' was launched 10 minutes ago');
  53. // Forever will restart the server
  54. process.exit(1);
  55. }, 600000);
  56. // It's time to launch the test!!!
  57. var triesNumber = 3;
  58. async.retry(triesNumber, function(cb) {
  59. phantomas(task.url, options, function(err, json, results) {
  60. console.log('Returning from Phantomas');
  61. // Adding some YellowLabTools errors here
  62. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  63. err = 1001;
  64. }
  65. if (!err && (!json || !json.metrics)) {
  66. err = 1002;
  67. }
  68. // Don't cancel test if it is a timeout and we've got some results
  69. if (err === 252 && json) {
  70. console.log('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  71. err = null;
  72. }
  73. if (err) {
  74. console.log('Attempt failed for test id ' + task.testId + '. Error code ' + err);
  75. }
  76. cb(err, {json: json, results: results});
  77. });
  78. }, function(err, data) {
  79. clearTimeout(killer);
  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();