browserStack.js
4.82 KB
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
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/*
* This is an implementation of the Browserstack Driver Provider.
* It is responsible for setting up the account object, tearing
* it down, and setting up the driver correctly.
*/
var https = require("https");
var q = require("q");
var util = require("util");
var exitCodes_1 = require("../exitCodes");
var logger_1 = require("../logger");
var driverProvider_1 = require("./driverProvider");
var logger = new logger_1.Logger('browserstack');
var BrowserStack = (function (_super) {
__extends(BrowserStack, _super);
function BrowserStack(config) {
return _super.call(this, config) || this;
}
/**
* Hook to update the BrowserStack job status.
* @public
* @param {Object} update
* @return {q.promise} A promise that will resolve when the update is complete.
*/
BrowserStack.prototype.updateJob = function (update) {
var _this = this;
var deferredArray = this.drivers_.map(function (driver) {
var deferred = q.defer();
driver.getSession().then(function (session) {
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' +
new Buffer(_this.config_.browserstackUser + ':' + _this.config_.browserstackKey)
.toString('base64')
};
var options = {
hostname: 'www.browserstack.com',
port: 443,
path: '/automate/sessions/' + session.getId() + '.json',
method: 'GET',
headers: headers
};
var req = https.request(options, function (res) {
res.on('data', function (data) {
var info = JSON.parse(data.toString());
if (info && info.automation_session && info.automation_session.browser_url) {
logger.info('BrowserStack results available at ' + info.automation_session.browser_url);
}
else {
logger.info('BrowserStack results available at ' +
'https://www.browserstack.com/automate');
}
});
});
req.end();
req.on('error', function (e) {
logger.info('BrowserStack results available at ' +
'https://www.browserstack.com/automate');
});
var jobStatus = update.passed ? 'completed' : 'error';
options.method = 'PUT';
var update_req = https.request(options, function (res) {
var responseStr = '';
res.on('data', function (data) {
responseStr += data.toString();
});
res.on('end', function () {
logger.info(responseStr);
deferred.resolve();
});
res.on('error', function (e) {
throw new exitCodes_1.BrowserError(logger, 'Error updating BrowserStack pass/fail status: ' + util.inspect(e));
});
});
update_req.write('{"status":"' + jobStatus + '"}');
update_req.end();
});
return deferred.promise;
});
return q.all(deferredArray);
};
/**
* Configure and launch (if applicable) the object's environment.
* @public
* @return {q.promise} A promise which will resolve when the environment is
* ready to test.
*/
BrowserStack.prototype.setupEnv = function () {
var deferred = q.defer();
this.config_.capabilities['browserstack.user'] = this.config_.browserstackUser;
this.config_.capabilities['browserstack.key'] = this.config_.browserstackKey;
this.config_.seleniumAddress = 'http://hub.browserstack.com/wd/hub';
// Append filename to capabilities.name so that it's easier to identify
// tests.
if (this.config_.capabilities.name && this.config_.capabilities.shardTestFiles) {
this.config_.capabilities.name +=
(':' + this.config_.specs.toString().replace(/^.*[\\\/]/, ''));
}
logger.info('Using BrowserStack selenium server at ' + this.config_.seleniumAddress);
deferred.resolve();
return deferred.promise;
};
return BrowserStack;
}(driverProvider_1.DriverProvider));
exports.BrowserStack = BrowserStack;