122 lines
16 KiB
JavaScript
122 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 events_1 = require("events");
|
||
|
const phase_1 = __importDefault(require("./phase"));
|
||
|
const testcafe_hammerhead_1 = require("testcafe-hammerhead");
|
||
|
const marker_symbol_1 = __importDefault(require("./marker-symbol"));
|
||
|
const nanoid_1 = require("nanoid");
|
||
|
class Role extends events_1.EventEmitter {
|
||
|
constructor(loginUrl, initFn, options = {}) {
|
||
|
super();
|
||
|
this[marker_symbol_1.default] = true;
|
||
|
this.id = (0, nanoid_1.nanoid)(7);
|
||
|
this.phase = loginUrl ? phase_1.default.uninitialized : phase_1.default.initialized;
|
||
|
this.loginUrl = loginUrl;
|
||
|
this._initFn = initFn;
|
||
|
this.opts = options;
|
||
|
this.redirectUrl = null;
|
||
|
this.stateSnapshot = testcafe_hammerhead_1.StateSnapshot.empty();
|
||
|
this.initErr = null;
|
||
|
}
|
||
|
async _storeStateSnapshot(testRun) {
|
||
|
var _a;
|
||
|
if (this.initErr)
|
||
|
return;
|
||
|
this.stateSnapshot = await testRun.getStateSnapshot();
|
||
|
await ((_a = testRun === null || testRun === void 0 ? void 0 : testRun.compilerService) === null || _a === void 0 ? void 0 : _a.updateRoleProperty({
|
||
|
roleId: this.id,
|
||
|
name: 'stateSnapshot',
|
||
|
value: this.stateSnapshot,
|
||
|
}));
|
||
|
}
|
||
|
_wrapTestFn(testRun) {
|
||
|
this._initFn = () => {
|
||
|
var _a;
|
||
|
return (_a = testRun.compilerService) === null || _a === void 0 ? void 0 : _a.executeRoleInitFn({
|
||
|
testRunId: testRun.id,
|
||
|
roleId: this.id,
|
||
|
});
|
||
|
};
|
||
|
}
|
||
|
async _setInitError(err, testRun) {
|
||
|
var _a;
|
||
|
this.initErr = err;
|
||
|
await ((_a = testRun === null || testRun === void 0 ? void 0 : testRun.compilerService) === null || _a === void 0 ? void 0 : _a.updateRoleProperty({
|
||
|
roleId: this.id,
|
||
|
name: 'initErr',
|
||
|
value: this.initErr,
|
||
|
}));
|
||
|
}
|
||
|
async _executeInitFn(testRun) {
|
||
|
if (this.initErr)
|
||
|
return;
|
||
|
try {
|
||
|
if (testRun.compilerService)
|
||
|
this._wrapTestFn(testRun);
|
||
|
let fn = () => this._initFn(testRun);
|
||
|
fn = testRun.decoratePreventEmitActionEvents(fn, { prevent: false });
|
||
|
fn = testRun.decorateDisableDebugBreakpoints(fn, { disable: false });
|
||
|
await fn();
|
||
|
}
|
||
|
catch (err) {
|
||
|
await this._setInitError(err, testRun);
|
||
|
}
|
||
|
}
|
||
|
async _switchToCleanRun(testRun) {
|
||
|
try {
|
||
|
await testRun.switchToCleanRun(this.loginUrl);
|
||
|
}
|
||
|
catch (err) {
|
||
|
await this._setInitError(err, testRun);
|
||
|
}
|
||
|
}
|
||
|
async initialize(testRun) {
|
||
|
var _a;
|
||
|
this.phase = phase_1.default.pendingInitialization;
|
||
|
await this._switchToCleanRun(testRun);
|
||
|
await this._executeInitFn(testRun);
|
||
|
await this._storeStateSnapshot(testRun);
|
||
|
if (this.opts.preserveUrl)
|
||
|
await this.setCurrentUrlAsRedirectUrl(testRun);
|
||
|
this.phase = phase_1.default.initialized;
|
||
|
await ((_a = testRun.compilerService) === null || _a === void 0 ? void 0 : _a.updateRoleProperty({
|
||
|
roleId: this.id,
|
||
|
name: 'phase',
|
||
|
value: this.phase,
|
||
|
}));
|
||
|
this.emit('initialized');
|
||
|
}
|
||
|
async setCurrentUrlAsRedirectUrl(testRun) {
|
||
|
var _a;
|
||
|
const currentUrl = await testRun.getCurrentUrl();
|
||
|
if (this.opts.preserveUrl)
|
||
|
this.redirectUrl = currentUrl;
|
||
|
else {
|
||
|
this.redirectUrl = this.redirectUrl || {};
|
||
|
this.redirectUrl[testRun.test.id] = currentUrl;
|
||
|
}
|
||
|
await ((_a = testRun.compilerService) === null || _a === void 0 ? void 0 : _a.updateRoleProperty({
|
||
|
roleId: this.id,
|
||
|
name: 'redirectUrl',
|
||
|
value: this.redirectUrl,
|
||
|
}));
|
||
|
}
|
||
|
static from(init) {
|
||
|
if (!init)
|
||
|
return null;
|
||
|
const serializedRole = init;
|
||
|
const role = new Role(serializedRole.loginUrl, serializedRole._initFn, serializedRole.opts);
|
||
|
role.id = serializedRole.id;
|
||
|
role.phase = serializedRole.phase;
|
||
|
role.redirectUrl = serializedRole.redirectUrl;
|
||
|
role.stateSnapshot = serializedRole.stateSnapshot;
|
||
|
role.initErr = serializedRole.initErr;
|
||
|
return role;
|
||
|
}
|
||
|
}
|
||
|
exports.default = Role;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicm9sZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9yb2xlL3JvbGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSxtQ0FBc0M7QUFDdEMsb0RBQWdDO0FBQ2hDLDZEQUFvRDtBQUNwRCxvRUFBeUM7QUFDekMsbUNBQWdDO0FBUWhDLE1BQXFCLElBQUssU0FBUSxxQkFBWTtJQVcxQyxZQUFvQixRQUF1QixFQUFFLE1BQXVCLEVBQUUsT0FBTyxHQUFHLEVBQUU7UUFDOUUsS0FBSyxFQUFFLENBQUM7UUFFUixJQUFJLENBQUMsdUJBQVUsQ0FBQyxHQUFLLElBQUksQ0FBQztRQUMxQixJQUFJLENBQUMsRUFBRSxHQUFjLElBQUEsZUFBTSxFQUFDLENBQUMsQ0FBQyxDQUFDO1FBQy9CLElBQUksQ0FBQyxLQUFLLEdBQVcsUUFBUSxDQUFDLENBQUMsQ0FBQyxlQUFTLENBQUMsYUFBYSxDQUFDLENBQUMsQ0FBQyxlQUFTLENBQUMsV0FBVyxDQUFDO1FBQ2hGLEl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