runner-from-html.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function Runner( options ){
  2. console.debug("Constructing runner for "+options.runner+".");
  3. var name, path,
  4. runner = options.runner;
  5. if(typeof runner == 'object'){
  6. name = runner.name;
  7. path = runner.runner;
  8. } else {
  9. name = runner;
  10. path = runner;
  11. }
  12. this._options = {
  13. reporter: options.jasmineReporter || {},
  14. path: require('path').normalize(path),
  15. name: name
  16. };
  17. };
  18. Runner.prototype._getScriptPaths = function(pathToHtml, callback){
  19. var path = require('path');
  20. require('jsdom').env({
  21. html: pathToHtml,
  22. scripts: [],
  23. done: function(errors,window){
  24. if(errors){
  25. console.error('Error when constructing DOM to get script paths for runner.');
  26. console.debug(errors);
  27. exit(1);
  28. }
  29. var basePath = path.dirname(pathToHtml);
  30. var scripts = window.document.getElementsByTagName('script');
  31. var scriptPaths = []
  32. for(var i = 0; i < scripts.length; i++){
  33. var script = scripts[i];
  34. var src = script.getAttribute('src');
  35. if(src) {
  36. var scriptPath = path.normalize(basePath + "/" + src);
  37. scriptPaths.push(scriptPath);
  38. }
  39. }
  40. callback(scriptPaths);
  41. }
  42. });
  43. };
  44. Runner.prototype._executeRunner = function(pathToHtml, scripts, reporter, callback){
  45. console.debug("Constructing runner with following scripts: ");
  46. for(var i = 0; i < scripts.length; i++) console.debug(" - " + scripts[i]);
  47. require('jsdom').env({
  48. html: pathToHtml,
  49. scripts: scripts,
  50. done: function(errors,window){
  51. if(errors){
  52. console.error('Error when constructing DOM for runner.');
  53. console.debug(errors);
  54. exit(1);
  55. }
  56. // Executed when the DOM has finished construction
  57. if(errors) console.error('Error construction DOM for tests: ',errors);
  58. window.jasmine.getEnv().addReporter(reporter);
  59. window.jasmine.getEnv().addReporter({
  60. reportRunnerResults : function(){
  61. if(callback) callback();
  62. }
  63. });
  64. console.debug("Running runner in Jasmine.");
  65. window.jasmine.getEnv().execute();
  66. }
  67. });
  68. };
  69. Runner.prototype.run = function(callback){
  70. var path = this._options.path,
  71. name = this._options.name,
  72. reporter = this._options.reporter,
  73. that=this;
  74. console.debug("Running runner " + name);
  75. if(reporter.reportStartingGroup) reporter.reportStartingGroup(name);
  76. this._getScriptPaths(path, function(scripts){
  77. that._executeRunner(path,scripts,reporter,function(){
  78. callback();
  79. });
  80. });
  81. };
  82. exports.create = function(options){
  83. return new Runner(options);
  84. }