UNPKG

5.03 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "tslib", "@theintern/leadfoot/Session", "@theintern/common"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 var tslib_1 = require("tslib");
13 var Session_1 = require("@theintern/leadfoot/Session");
14 var common_1 = require("@theintern/common");
15 function getCoverageData(coverageVariable) {
16 var coverageData = (function () {
17 return this;
18 })()[coverageVariable];
19 return coverageData && JSON.stringify(coverageData);
20 }
21 var ProxiedSession = (function (_super) {
22 tslib_1.__extends(ProxiedSession, _super);
23 function ProxiedSession() {
24 var _this = _super !== null && _super.apply(this, arguments) || this;
25 _this.baseUrl = '';
26 _this.coverageVariable = '';
27 return _this;
28 }
29 Object.defineProperty(ProxiedSession.prototype, "coverageEnabled", {
30 get: function () {
31 return this.executor.config.coverage !== false;
32 },
33 enumerable: true,
34 configurable: true
35 });
36 ProxiedSession.prototype.get = function (url) {
37 var _this = this;
38 if (!/^[A-Za-z][A-Za-z0-9+.-]+:/.test(url)) {
39 if (url.indexOf(this.executor.config.basePath) === 0) {
40 url = url.slice(this.executor.config.basePath.length);
41 }
42 url = this.baseUrl + url;
43 }
44 if (!this.coverageEnabled) {
45 return _super.prototype.get.call(this, url);
46 }
47 return this._getCoverage().finally(function () {
48 return _super.prototype.get.call(_this, url);
49 });
50 };
51 ProxiedSession.prototype.quit = function () {
52 var _this = this;
53 this.executor.log('Quitting', this.sessionId);
54 return this.setHeartbeatInterval(0)
55 .then(function () {
56 if (_this.coverageEnabled) {
57 return _this._getCoverage();
58 }
59 })
60 .finally(function () { return _super.prototype.quit.call(_this); });
61 };
62 ProxiedSession.prototype.setHeartbeatInterval = function (delay) {
63 var _this = this;
64 this._heartbeatIntervalHandle && this._heartbeatIntervalHandle.remove();
65 if (delay) {
66 var sendHeartbeat_1 = function () {
67 var timeoutId;
68 var cancelled = false;
69 var startTime = Date.now();
70 _this._heartbeatIntervalHandle = {
71 remove: function () {
72 cancelled = true;
73 clearTimeout(timeoutId);
74 }
75 };
76 _this.getCurrentUrl()
77 .then(function () {
78 if (!cancelled) {
79 timeoutId = global.setTimeout(sendHeartbeat_1, delay - (Date.now() - startTime));
80 }
81 })
82 .catch(function (error) { return _this.executor.emit('error', error); });
83 };
84 sendHeartbeat_1();
85 }
86 return common_1.Task.resolve();
87 };
88 ProxiedSession.prototype._getCoverage = function () {
89 var _this = this;
90 var shouldGetPromise;
91 if (this.capabilities.brokenExecuteForNonHttpUrl) {
92 shouldGetPromise = common_1.Task.resolve(this.getCurrentUrl().then(function (url) { return /^https?:/i.test(url); }));
93 }
94 else {
95 shouldGetPromise = common_1.Task.resolve(true);
96 }
97 return shouldGetPromise.then(function (shouldGetCoverage) {
98 if (shouldGetCoverage) {
99 return _this.execute(getCoverageData, [
100 _this.coverageVariable
101 ]).then(function (coverageData) {
102 _this.executor.log('Got coverage data for', _this.sessionId);
103 if (coverageData) {
104 return _this.executor.emit('coverage', {
105 sessionId: _this.sessionId,
106 source: 'remote session',
107 coverage: JSON.parse(coverageData)
108 });
109 }
110 });
111 }
112 });
113 };
114 return ProxiedSession;
115 }(Session_1.default));
116 exports.default = ProxiedSession;
117});
118//# sourceMappingURL=ProxiedSession.js.map
\No newline at end of file