UNPKG

4.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*
4 * This is an implementation of the Browserstack Driver Provider.
5 * It is responsible for setting up the account object, tearing
6 * it down, and setting up the driver correctly.
7 */
8const https = require("https");
9const q = require("q");
10const util = require("util");
11const exitCodes_1 = require("../exitCodes");
12const logger_1 = require("../logger");
13const driverProvider_1 = require("./driverProvider");
14let logger = new logger_1.Logger('browserstack');
15class BrowserStack extends driverProvider_1.DriverProvider {
16 constructor(config) {
17 super(config);
18 }
19 /**
20 * Hook to update the BrowserStack job status.
21 * @public
22 * @param {Object} update
23 * @return {q.promise} A promise that will resolve when the update is complete.
24 */
25 updateJob(update) {
26 let deferredArray = this.drivers_.map((driver) => {
27 let deferred = q.defer();
28 driver.getSession().then((session) => {
29 let headers = {
30 'Content-Type': 'application/json',
31 'Authorization': 'Basic ' +
32 new Buffer(this.config_.browserstackUser + ':' + this.config_.browserstackKey)
33 .toString('base64')
34 };
35 let options = {
36 hostname: 'www.browserstack.com',
37 port: 443,
38 path: '/automate/sessions/' + session.getId() + '.json',
39 method: 'GET',
40 headers: headers
41 };
42 let req = https.request(options, (res) => {
43 res.on('data', (data) => {
44 let info = JSON.parse(data.toString());
45 if (info && info.automation_session && info.automation_session.browser_url) {
46 logger.info('BrowserStack results available at ' + info.automation_session.browser_url);
47 }
48 else {
49 logger.info('BrowserStack results available at ' +
50 'https://www.browserstack.com/automate');
51 }
52 });
53 });
54 req.end();
55 req.on('error', (e) => {
56 logger.info('BrowserStack results available at ' +
57 'https://www.browserstack.com/automate');
58 });
59 let jobStatus = update.passed ? 'completed' : 'error';
60 options.method = 'PUT';
61 let update_req = https.request(options, (res) => {
62 let responseStr = '';
63 res.on('data', (data) => {
64 responseStr += data.toString();
65 });
66 res.on('end', () => {
67 logger.info(responseStr);
68 deferred.resolve();
69 });
70 res.on('error', (e) => {
71 throw new exitCodes_1.BrowserError(logger, 'Error updating BrowserStack pass/fail status: ' + util.inspect(e));
72 });
73 });
74 update_req.write('{"status":"' + jobStatus + '"}');
75 update_req.end();
76 });
77 return deferred.promise;
78 });
79 return q.all(deferredArray);
80 }
81 /**
82 * Configure and launch (if applicable) the object's environment.
83 * @return {q.promise} A promise which will resolve when the environment is
84 * ready to test.
85 */
86 setupDriverEnv() {
87 let deferred = q.defer();
88 this.config_.capabilities['browserstack.user'] = this.config_.browserstackUser;
89 this.config_.capabilities['browserstack.key'] = this.config_.browserstackKey;
90 this.config_.seleniumAddress = 'http://hub.browserstack.com/wd/hub';
91 // Append filename to capabilities.name so that it's easier to identify
92 // tests.
93 if (this.config_.capabilities.name && this.config_.capabilities.shardTestFiles) {
94 this.config_.capabilities.name +=
95 (':' + this.config_.specs.toString().replace(/^.*[\\\/]/, ''));
96 }
97 logger.info('Using BrowserStack selenium server at ' + this.config_.seleniumAddress);
98 deferred.resolve();
99 return deferred.promise;
100 }
101}
102exports.BrowserStack = BrowserStack;
103//# sourceMappingURL=browserStack.js.map
\No newline at end of file