phantomasWrapper.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. var value = options[opt];
  48. if ((typeof value === 'string' || value instanceof String) && value.indexOf(' ') >= 0) {
  49. value = '"' + value + '"';
  50. }
  51. optionsString += ' ' + '--' + opt + '=' + value;
  52. }
  53. console.log('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose');
  54. // Kill the application if nothing happens for 10 minutes
  55. var killer = setTimeout(function() {
  56. console.log('Killing the server because the test ' + task.testId + ' on ' + task.url + ' was launched 10 minutes ago');
  57. // Forever will restart the server
  58. process.exit(1);
  59. }, 600000);
  60. // It's time to launch the test!!!
  61. var triesNumber = 3;
  62. async.retry(triesNumber, function(cb) {
  63. phantomas(task.url, options, function(err, json, results) {
  64. console.log('Returning from Phantomas');
  65. // Adding some YellowLabTools errors here
  66. if (json && json.metrics && !json.metrics.javascriptExecutionTree) {
  67. err = 1001;
  68. }
  69. if (!err && (!json || !json.metrics)) {
  70. err = 1002;
  71. }
  72. // Don't cancel test if it is a timeout and we've got some results
  73. if (err === 252 && json) {
  74. console.log('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.');
  75. err = null;
  76. }
  77. if (err) {
  78. console.log('Attempt failed for test id ' + task.testId + '. Error code ' + err);
  79. }
  80. cb(err, {json: json, results: results});
  81. });
  82. }, function(err, data) {
  83. clearTimeout(killer);
  84. if (err) {
  85. console.log('All ' + triesNumber + ' attemps failed for test id ' + task.testId);
  86. }
  87. callback(err, data.json, data.results);
  88. });
  89. };
  90. };
  91. module.exports = new PhantomasWrapper();