123 lines
16 KiB
JavaScript
123 lines
16 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const path_1 = __importDefault(require("path"));
|
||
|
const testcafe_hammerhead_1 = require("testcafe-hammerhead");
|
||
|
const _1 = __importDefault(require("./"));
|
||
|
const ACTIVE_SESSIONS_MAP = {};
|
||
|
const UPLOADS_DIR_NAME = '_uploads_';
|
||
|
// NOTE: Proxyless cookie implementation doesn't require client-server communication.
|
||
|
// This stub was created to reduce conditional logic in connected classes.
|
||
|
class ProxylessCookieStub {
|
||
|
getClientString() {
|
||
|
return '';
|
||
|
}
|
||
|
takePendingSyncCookies() {
|
||
|
return [];
|
||
|
}
|
||
|
}
|
||
|
class SessionController extends testcafe_hammerhead_1.Session {
|
||
|
constructor(uploadRoots, options) {
|
||
|
super(uploadRoots, options);
|
||
|
this.currentTestRun = null;
|
||
|
}
|
||
|
// Hammerhead payload
|
||
|
async getPayloadScript() {
|
||
|
return this.currentTestRun.getPayloadScript();
|
||
|
}
|
||
|
async getIframePayloadScript() {
|
||
|
return this.currentTestRun.getIframePayloadScript();
|
||
|
}
|
||
|
// Hammerhead handlers
|
||
|
handleServiceMessage(msg, serverInfo) {
|
||
|
if (this.currentTestRun[msg.cmd])
|
||
|
return super.handleServiceMessage.call(this.currentTestRun, msg, serverInfo);
|
||
|
return super.handleServiceMessage(msg, serverInfo);
|
||
|
}
|
||
|
getAuthCredentials() {
|
||
|
return this.currentTestRun.getAuthCredentials();
|
||
|
}
|
||
|
handleFileDownload() {
|
||
|
return this.currentTestRun.handleFileDownload();
|
||
|
}
|
||
|
handleAttachment(data) {
|
||
|
return this.currentTestRun.handleAttachment(data);
|
||
|
}
|
||
|
handlePageError(ctx, err) {
|
||
|
return this.currentTestRun.handlePageError(ctx, err);
|
||
|
}
|
||
|
createCookies() {
|
||
|
return this.options.proxyless ? new ProxylessCookieStub() : super.createCookies();
|
||
|
}
|
||
|
// API
|
||
|
static getSession(testRun) {
|
||
|
let sessionInfo = ACTIVE_SESSIONS_MAP[testRun.browserConnection.id];
|
||
|
if (!sessionInfo || !testRun.disablePageReloads) {
|
||
|
if (sessionInfo && sessionInfo.url)
|
||
|
SessionController.closeSession(testRun);
|
||
|
let session = null;
|
||
|
if (testRun.test.isLegacy)
|
||
|
session = testRun;
|
||
|
else {
|
||
|
const fixtureDir = path_1.default.dirname(testRun.test.fixture.path);
|
||
|
const uploadRoots = [
|
||
|
path_1.default.resolve(UPLOADS_DIR_NAME),
|
||
|
path_1.default.resolve(fixtureDir, UPLOADS_DIR_NAME),
|
||
|
fixtureDir,
|
||
|
];
|
||
|
const options = {
|
||
|
disablePageCaching: testRun.disablePageCaching,
|
||
|
allowMultipleWindows: _1.default.isMultipleWindowsAllowed(testRun),
|
||
|
requestTimeout: testRun.requestTimeout,
|
||
|
proxyless: testRun.opts.proxyless,
|
||
|
};
|
||
|
if (options.allowMultipleWindows)
|
||
|
options.windowId = testRun.browserConnection.activeWindowId;
|
||
|
session = new SessionController(uploadRoots, options);
|
||
|
session.currentTestRun = testRun;
|
||
|
}
|
||
|
sessionInfo = {
|
||
|
session: session,
|
||
|
proxy: null,
|
||
|
url: null,
|
||
|
};
|
||
|
ACTIVE_SESSIONS_MAP[testRun.browserConnection.id] = sessionInfo;
|
||
|
}
|
||
|
else if (!testRun.test.isLegacy)
|
||
|
sessionInfo.session.currentTestRun = testRun;
|
||
|
return sessionInfo.session;
|
||
|
}
|
||
|
static getSessionUrl(testRun, proxy) {
|
||
|
let sessionInfo = ACTIVE_SESSIONS_MAP[testRun.browserConnection.id];
|
||
|
if (!sessionInfo || testRun.test.isLegacy) {
|
||
|
SessionController.getSession(testRun);
|
||
|
sessionInfo = ACTIVE_SESSIONS_MAP[testRun.browserConnection.id];
|
||
|
}
|
||
|
if (!sessionInfo.url) {
|
||
|
const pageUrl = testRun.test.pageUrl;
|
||
|
const externalProxyHost = testRun.opts.proxy;
|
||
|
let externalProxySettings = null;
|
||
|
if (externalProxyHost) {
|
||
|
externalProxySettings = {
|
||
|
url: externalProxyHost,
|
||
|
bypassRules: testRun.opts.proxyBypass,
|
||
|
};
|
||
|
}
|
||
|
sessionInfo.proxy = proxy;
|
||
|
sessionInfo.url = proxy.openSession(pageUrl, sessionInfo.session, externalProxySettings);
|
||
|
}
|
||
|
return sessionInfo.url;
|
||
|
}
|
||
|
static closeSession(testRun) {
|
||
|
const sessionInfo = ACTIVE_SESSIONS_MAP[testRun.browserConnection.id];
|
||
|
if (!sessionInfo || !sessionInfo.url || !sessionInfo.proxy)
|
||
|
return;
|
||
|
sessionInfo.proxy.closeSession(sessionInfo.session);
|
||
|
delete ACTIVE_SESSIONS_MAP[testRun.browserConnection.id];
|
||
|
}
|
||
|
}
|
||
|
exports.default = SessionController;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2Vzc2lvbi1jb250cm9sbGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3Rlc3QtcnVuL3Nlc3Npb24tY29udHJvbGxlci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLGdEQUF3QjtBQUN4Qiw2REFBOEM7QUFDOUMsMENBQXlCO0FBR3pCLE1BQU0sbUJBQW1CLEdBQUcsRUFBRSxDQUFDO0FBQy9CLE1BQU0sZ0JBQWdCLEdBQUcsV0FBVyxDQUFDO0FBRXJDLHFGQUFxRjtBQUNyRiwwRUFBMEU7QUFDMUUsTUFBTSxtQkFBbUI7SUFDckIsZUFBZTtRQUNYLE9BQU8sRUFBRSxDQUFDO0lBQ2QsQ0FBQztJQUVELHNCQUFzQjtRQUNsQixPQUFPLEVBQUUsQ0FBQztJQUNkLENBQUM7Q0FDSjtBQUVELE1BQXFCLGlCQUF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