UNPKG

@o3r/testing

Version:

The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.

144 lines • 6.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostMessageInterceptor = void 0; var tslib_1 = require("tslib"); var fs = require("node:fs"); var path = require("node:path"); var protractor_1 = require("protractor"); /** * Controls the postMessage interceptor * @deprecated Will be removed in v13, please use Playwright instead */ var PostMessageInterceptor = /** @class */ (function () { function PostMessageInterceptor() { this.GET_INSTANCE = 'window.postMessageInterceptor.getInstance()'; } /** * Executes a script in the app * @param methodName * @param {...any} args */ PostMessageInterceptor.prototype.execInstanceMethod = function (methodName) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, protractor_1.browser.executeScript.apply(protractor_1.browser, tslib_1.__spreadArray(["".concat(this.GET_INSTANCE, ".").concat(methodName, "(...arguments);")], args, false))]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** * Executes an async script in the app * @param methodName * @param {...any} args */ PostMessageInterceptor.prototype.execInstanceAsyncMethod = function (methodName) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, protractor_1.browser.executeAsyncScript.apply(protractor_1.browser, tslib_1.__spreadArray(["".concat(this.GET_INSTANCE, ".").concat(methodName, "(...arguments);")], args, false))]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** * Initializes the PostMessageInterceptor in the app * NOTE: It does not start listening right away. You need to call listen() */ PostMessageInterceptor.prototype.initialize = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var fetchManager; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: fetchManager = fs.readFileSync(path.resolve(process.cwd(), 'node_modules', '@o3r', 'testing', 'tools', 'protractor', 'post-message-interceptor', '_post-message-interceptor.js'), { encoding: 'utf8' }); return [4 /*yield*/, protractor_1.browser.executeScript(fetchManager)]; case 1: _a.sent(); return [4 /*yield*/, this.execInstanceMethod('init')]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Stops the PostMessageInterceptor. */ PostMessageInterceptor.prototype.stop = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.execInstanceMethod('stop')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Listens for post messages * @param conditionFn an optional boolean function that will evaluate if the message should be saved or not in the stack */ PostMessageInterceptor.prototype.listen = function (conditionFn) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.execInstanceMethod('listen', conditionFn)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Stops listening for post messages, resets the stack and clears the condition */ PostMessageInterceptor.prototype.stopListening = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.execInstanceMethod('stopListening')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Get all the post messages * @param timeoutInterval Interval in milliseconds between two checks * @param retries How many retries to be executed (-1 for infinite retries) */ PostMessageInterceptor.prototype.getMessages = function () { return tslib_1.__awaiter(this, arguments, void 0, function (timeoutInterval, retries) { if (timeoutInterval === void 0) { timeoutInterval = 100; } if (retries === void 0) { retries = -1; } return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.execInstanceAsyncMethod('getMessages', timeoutInterval, retries)]; case 1: return [2 /*return*/, (_a.sent())]; } }); }); }; return PostMessageInterceptor; }()); exports.PostMessageInterceptor = PostMessageInterceptor; //# sourceMappingURL=post-message-interceptor.js.map