Code coverage report for preceptor-reporter/lib/reporter/jenkinsSauceLabs.js

Statements: 88.1% (37 / 42)      Branches: 50% (16 / 32)      Functions: 100% (5 / 5)      Lines: 88.1% (37 / 42)      Ignored: none     

All files » preceptor-reporter/lib/reporter/ » jenkinsSauceLabs.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132      1 1                 1                 1   1 1 1     1 1   1 1                               1     1 1 1 1     1 1   1       1                             1     1 1 1 1     1 1   1           1                     1     1     1 1   1     1       1         1  
// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
 
var AbstractReporter = require('../abstractReporter');
var JenkinsSauceLabsMessenger = require('../messenger/jenkinsSauceLabs');
 
/**
 * @class JenkinsSauceLabsReporter
 * @extends AbstractReporter
 * @constructor
 *
 * @property {JenkinsSauceLabsMessenger} _messenger
 */
var JenkinsSauceLabsReporter = AbstractReporter.extend(
 
	{
		/**
		 * Initializes the instance
		 *
		 * @method initialize
		 */
		initialize: function () {
			this.__super();
 
			this._messenger = new JenkinsSauceLabsMessenger();
			this._messenger.on('message', function (text, options) {
				this.console(options.id, options.msgType, text);
			}.bind(this));
 
			Eif (this.getOptions().progress === undefined) {
				this.getOptions().progress = false;
			}
			Eif (this.getOptions().output === undefined) {
				this.getOptions().output = true;
			}
		},
 
		/**
		 * Gets the web-driver session id
		 *
		 * It tries to get it from a couple different places:
		 * - itemData of the root as "sessionId"
		 * - reporter configuration as "sessionId"
		 * - environment variable "SELENIUM_SESSION_ID"
		 *
		 * #method getSessionId
		 * @return {string}
		 */
		getSessionId: function () {
			var sessionId,
				tree;
 
			Eif (!sessionId) {
				tree = this.getContainer().getTree();
				Eif (tree && tree.data) {
					sessionId = tree.data.sessionId;
				}
			}
			Eif (!sessionId) {
				sessionId = this.getConfiguration().sessionId;
			}
			Iif (!sessionId) {
				sessionId = process.env.SELENIUM_SESSION_ID;
			}
 
			return sessionId;
		},
 
		/**
		 * Gets the job-name, binding the name to the session
		 *
		 * It tries to get it from a couple different places:
		 * - itemData of the root as "jobName"
		 * - reporter configuration as "jobName"
		 * - environment variables with format "APP_NAME (#BUILD_NUMBER)"
		 *
		 * @method getJobName
		 * @return {string}
		 */
		getJobName: function () {
			var sessionId,
				tree;
 
			Eif (!sessionId) {
				tree = this.getContainer().getTree();
				Eif (tree && tree.data) {
					sessionId = tree.data.jobName;
				}
			}
			Eif (!sessionId) {
				sessionId = this.getConfiguration().jobName;
			}
			Iif (!sessionId) {
				if (process.env.APP_NAME && process.env.BUILD_NUMBER) {
					sessionId = process.env.APP_NAME + "(#" + process.env.BUILD_NUMBER + ")";
				}
			}
 
			return sessionId;
		},
 
 
		/**
		 * Called when reporting stops
		 *
		 * @method stop
		 */
		stop: function () {
 
			var sessionId,
				jobName;
 
			this.__super();
 
			// Get information required
			sessionId = this.getSessionId();
			jobName = this.getJobName();
 
			Iif (!sessionId) {
				throw new Error('Cannot find sessionId.');
 
			} else Iif (!jobName) {
				throw new Error('Cannot find jobName.');
 
			} else {
				this._messenger.sendBuildInfo(sessionId, jobName, { id:undefined, msgType:'stop' });
			}
		}
	});
 
module.exports = JenkinsSauceLabsReporter;