clientmanager.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. var util = require('util');
  2. var http = require('http');
  3. var events = require('events');
  4. var Q = require('q');
  5. var Nightwatch = require('../index.js');
  6. function ClientManager() {
  7. events.EventEmitter.call(this);
  8. this.setMaxListeners(0);
  9. }
  10. util.inherits(ClientManager, events.EventEmitter);
  11. ClientManager.prototype.init = function(opts) {
  12. try {
  13. this['@client'] = Nightwatch.client(opts);
  14. } catch (err) {
  15. console.log(err.stack);
  16. this.emit('error', err, false);
  17. return;
  18. }
  19. var self = this;
  20. this.options = opts;
  21. this['@client'].once('selenium:session_create', function() {
  22. var capabilities = this.api.capabilities || {};
  23. var browserName = (capabilities.browserName && capabilities.browserName.toUpperCase()) || '';
  24. var browserVersion = this.api.capabilities.version || this.api.capabilities.browserVersion;
  25. var platformVersion = this.api.capabilities.platform || this.api.capabilities.platformVersion;
  26. self.options.report_prefix = browserName + '_' + browserVersion + '_' + platformVersion + '_';
  27. });
  28. return this;
  29. };
  30. ClientManager.prototype.start = function(done) {
  31. var self = this;
  32. this.resetTerminated();
  33. this['@client'].once('nightwatch:finished', function(results, errors) {
  34. self.emit('complete', results, errors);
  35. if (done) {
  36. if (results.failed > 0 || results.errors > 0) {
  37. done(results.lastError);
  38. results.lastError = null;
  39. } else {
  40. done();
  41. }
  42. }
  43. });
  44. this['@client'].once('error', function(error, data) {
  45. var errorMsg = error || data;
  46. if (errorMsg instanceof http.IncomingMessage) {
  47. errorMsg = {
  48. statusCode : error.statusCode,
  49. headers : error.headers
  50. };
  51. }
  52. var result = {
  53. message: 'Connection refused! Is selenium server started?\n',
  54. data : errorMsg
  55. };
  56. self.emit('error', result, false);
  57. });
  58. this['@client'].start();
  59. return this;
  60. };
  61. ClientManager.prototype.get = function() {
  62. return this['@client'];
  63. };
  64. ClientManager.prototype.set = function(prop, value) {
  65. this['@client'][prop] = value;
  66. return this;
  67. };
  68. ClientManager.prototype.publishTestResults = function(testcase, results) {
  69. if (!this['@client'].api.currentTest) {
  70. return this;
  71. }
  72. results = results || {};
  73. var currentTestSuite = this['@client'].api.currentTest.results;
  74. currentTestSuite.passed += results.passed;
  75. currentTestSuite.failed += results.failed;
  76. currentTestSuite.errors += results.errors;
  77. currentTestSuite.skipped += results.skipped;
  78. currentTestSuite.tests += results.tests.length;
  79. currentTestSuite.testcases = currentTestSuite.testcases || {};
  80. currentTestSuite.testcases[testcase] = {
  81. passed : results.passed,
  82. failed : results.failed,
  83. errors : results.errors,
  84. skipped : results.skipped,
  85. tests : results.tests.length,
  86. assertions : results.tests,
  87. stackTrace : results.stackTrace
  88. };
  89. return this;
  90. };
  91. ClientManager.prototype.results = function(type, value) {
  92. if (typeof value == 'undefined' && typeof type == 'undefined') {
  93. return this['@client'].results;
  94. }
  95. if (typeof value == 'undefined') {
  96. return this['@client'].results[type] || 0;
  97. }
  98. this['@client'].results[type] = value;
  99. return this;
  100. };
  101. ClientManager.prototype.errors = function() {
  102. return this['@client'].errors;
  103. };
  104. ClientManager.prototype.handleException = function(err) {
  105. return this['@client'].handleException(err);
  106. };
  107. ClientManager.prototype.clearGlobalResult = function() {
  108. return this['@client'].clearResult();
  109. };
  110. ClientManager.prototype.terminated = function() {
  111. return this['@client'].terminated;
  112. };
  113. ClientManager.prototype.terminate = function() {
  114. this['@client'].terminate();
  115. return this;
  116. };
  117. ClientManager.prototype.setLocateStrategy = function() {
  118. this['@client'].setLocateStrategy();
  119. return this;
  120. };
  121. ClientManager.prototype.resetTerminated = function() {
  122. this['@client'].resetTerminated();
  123. return this;
  124. };
  125. ClientManager.prototype.print = function(startTime) {
  126. return this['@client'].printResult(startTime);
  127. };
  128. ClientManager.prototype.api = function(key, value) {
  129. if (key && (typeof value != 'undefined')) {
  130. this['@client'].api[key] = value;
  131. }
  132. return this['@client'].api;
  133. };
  134. ClientManager.prototype.globals = function(key, value) {
  135. if (key) {
  136. if (typeof value != 'undefined') {
  137. this['@client'].api.globals[key] = value;
  138. return this;
  139. }
  140. return this['@client'].api.globals[key];
  141. }
  142. return this['@client'].api.globals;
  143. };
  144. ClientManager.prototype.resetQueue = function() {
  145. this['@client'].queue.reset();
  146. return this;
  147. };
  148. ClientManager.prototype.restartQueue = function(onComplete) {
  149. this.resetQueue();
  150. this['@client'].queue.run(onComplete);
  151. };
  152. ClientManager.prototype.shouldRestartQueue = function() {
  153. return this['@client'] && this['@client'].queue.list().length > 0;
  154. };
  155. ClientManager.prototype.checkQueue = function() {
  156. var deferred = Q.defer();
  157. if (this.shouldRestartQueue()) {
  158. this.restartQueue(function() {
  159. deferred.resolve();
  160. });
  161. } else {
  162. deferred.resolve();
  163. }
  164. return deferred.promise;
  165. };
  166. ClientManager.prototype.endSessionOnFail = function(value) {
  167. if (typeof value == 'undefined') {
  168. return this['@client'].endSessionOnFail();
  169. }
  170. this['@client'].endSessionOnFail(value);
  171. return this;
  172. };
  173. ClientManager.prototype.skipTestcasesOnFail = function() {
  174. return this.api().options.skip_testcases_on_fail;
  175. };
  176. module.exports = ClientManager;