end.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var util = require('util');
  2. var events = require('events');
  3. var Utils = require('../../util/utils.js');
  4. var Logger = require('../../util/logger.js');
  5. /**
  6. * Ends the session. Uses session protocol command.
  7. *
  8. * ```
  9. * this.demoTest = function (browser) {
  10. * browser.end();
  11. * };
  12. * ```
  13. *
  14. * @method end
  15. * @param {function} [callback] Optional callback function to be called when the command finishes.
  16. * @see session
  17. * @api commands
  18. */
  19. function End() {
  20. events.EventEmitter.call(this);
  21. }
  22. util.inherits(End, events.EventEmitter);
  23. End.prototype.command = function(callback) {
  24. var self = this;
  25. var client = this.client;
  26. if (client.sessionId) {
  27. if (this.testFailuresExist() && this.shouldTakeScreenshot()) {
  28. var fileNamePath = Utils.getScreenshotFileName(client.api.currentTest, false, client.options.screenshots.path);
  29. Logger.info('We have failures in "' + client.api.currentTest.name + '". Taking screenshot...');
  30. client.api.saveScreenshot(fileNamePath, function(result, err) {
  31. if (err || result.status !== 0) {
  32. Logger.warn('Error saving screenshot...', err || result);
  33. }
  34. });
  35. }
  36. client.api.session('delete', function(result) {
  37. client.sessionId = client.api.sessionId = null;
  38. self.complete(callback, result);
  39. });
  40. } else {
  41. setImmediate(function() {
  42. self.complete(callback, null);
  43. });
  44. }
  45. return this.client.api;
  46. };
  47. End.prototype.testFailuresExist = function() {
  48. return this.client.results.errors > 0 || this.client.results.failed > 0;
  49. };
  50. End.prototype.shouldTakeScreenshot = function() {
  51. return this.client.options.screenshots.enabled && this.client.options.screenshots.on_failure;
  52. };
  53. End.prototype.complete = function(callback, result) {
  54. if (typeof callback === 'function') {
  55. callback.call(this, result);
  56. }
  57. this.emit('complete');
  58. };
  59. module.exports = End;