UNPKG

testplane

Version:

Tests framework based on mocha and wdio

192 lines 7.72 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initCommandHistory = exports.runGroup = exports.runWithoutHistory = exports.shouldPropagateFn = void 0; const types_1 = require("util/types"); const callstack_1 = require("./callstack"); const cmds = __importStar(require("./commands")); const utils_1 = require("./utils"); const types_2 = require("../../types"); const rrweb_1 = require("./rrweb"); const shouldNotWrapCommand = (commandName) => ["addCommand", "overwriteCommand", "extendOptions", "setMeta", "getMeta", "runStep"].includes(commandName); const shouldPropagateFn = (parentNode, currentNode) => (0, utils_1.isGroup)(parentNode) || (0, utils_1.isGroup)(currentNode); exports.shouldPropagateFn = shouldPropagateFn; const mkHistoryNode = ({ name, args, elementScope, key, overwrite, isGroup }) => { const map = { [types_2.TestStepKey.Name]: name, [types_2.TestStepKey.Args]: (0, utils_1.normalizeCommandArgs)(name, args), [types_2.TestStepKey.Scope]: cmds.createScope(elementScope ?? false), [types_2.TestStepKey.Key]: key ?? Symbol(), }; if (overwrite) { map[types_2.TestStepKey.IsOverwritten] = Boolean(overwrite); } if (isGroup) { map[types_2.TestStepKey.IsGroup] = true; } return map; }; const runWithoutHistory = async ({ callstack }, fn) => { callstack.setIsInBypassMode(true); try { return await fn(); } finally { callstack.setIsInBypassMode(false); } }; exports.runWithoutHistory = runWithoutHistory; const runWithHistoryHooks = ({ session, callstack, nodeData, fn, config }) => { nodeData.key = nodeData.key ?? Symbol(); if (callstack.isInBypassMode) { return fn(); } let rrwebPromise = null; try { const timeTravelMode = config.timeTravel.mode; const isRetry = (session.executionContext?.ctx?.attempt ?? 0) > 0; const shouldRecord = (0, utils_1.shouldRecordSnapshots)(timeTravelMode, isRetry); if (shouldRecord && process.send && session.executionContext?.ctx?.currentTest) { rrwebPromise = (0, rrweb_1.installRrwebAndCollectEvents)(session, callstack) .then(rrwebEvents => { const rrwebEventsFiltered = (0, rrweb_1.filterEvents)(rrwebEvents); (0, rrweb_1.sendFilteredEvents)(session, rrwebEventsFiltered); }) .catch(e => { console.warn("An error occurred during capturing snapshots in browser.", e); }); } } catch (e) { console.warn("An error occurred during capturing snapshots in browser.", e); } return (0, utils_1.runWithHooks)({ before: () => { callstack.enter(mkHistoryNode(nodeData)); }, fn: () => { const result = fn(); if (rrwebPromise && (0, types_1.isPromise)(result)) { return Promise.all([result, rrwebPromise]).then(([fnResult]) => fnResult); } return result; }, after: () => callstack.leave(nodeData.key), error: () => callstack.markError(exports.shouldPropagateFn), }); }; const overwriteAddCommand = (session, callstack, config) => { session.overwriteCommand("addCommand", (origCommand, name, wrapper, elementScope) => { if (shouldNotWrapCommand(name)) { return origCommand(name, wrapper, elementScope); } function decoratedWrapper(...args) { return runWithHistoryHooks({ session, callstack, nodeData: { name, args, elementScope, overwrite: false }, fn: () => wrapper.apply(this, args), config, }); } return origCommand(name, decoratedWrapper, elementScope); }); }; const overwriteOverwriteCommand = (session, callstack, config) => { session.overwriteCommand("overwriteCommand", (origCommand, name, wrapper, elementScope) => { if (shouldNotWrapCommand(name)) { return origCommand(name, wrapper, elementScope); } function decoratedWrapper(origFn, ...args) { return runWithHistoryHooks({ session, callstack, nodeData: { name, args, elementScope, overwrite: true }, fn: () => wrapper.apply(this, [origFn, ...args]), config, }); } // eslint-disable-next-line @typescript-eslint/no-explicit-any return origCommand(name, decoratedWrapper, elementScope); }); }; const overwriteCommands = ({ session, callstack, commands, elementScope, config }) => { commands.forEach(name => { function decoratedWrapper(origFn, ...args) { return runWithHistoryHooks({ session, callstack, nodeData: { name, args, elementScope, overwrite: false }, fn: () => origFn(...args), config, }); } // eslint-disable-next-line @typescript-eslint/no-explicit-any session.overwriteCommand(name, decoratedWrapper, elementScope); }); }; const overwriteBrowserCommands = (session, callstack, config) => overwriteCommands({ session, callstack, commands: cmds.getBrowserCommands().filter(cmd => !shouldNotWrapCommand(cmd)), elementScope: false, config, }); const overwriteElementCommands = (session, callstack, config) => overwriteCommands({ session, callstack, commands: cmds.getElementCommands(), elementScope: true, config, }); const runGroup = ({ session, callstack, config }, name, fn) => { if (!callstack) { return fn(); } return runWithHistoryHooks({ session, callstack, nodeData: { name, args: [], isGroup: true }, fn, config, }); }; exports.runGroup = runGroup; const overwriteRunStepCommand = (session, callstack, config) => { session.overwriteCommand("runStep", (origCommand, stepName, stepCb) => { return (0, exports.runGroup)({ session, callstack, config }, stepName, () => origCommand(stepName, stepCb)); }); }; const initCommandHistory = (session, config) => { const callstack = new callstack_1.Callstack(); overwriteAddCommand(session, callstack, config); overwriteBrowserCommands(session, callstack, config); overwriteElementCommands(session, callstack, config); overwriteOverwriteCommand(session, callstack, config); overwriteRunStepCommand(session, callstack, config); return callstack; }; exports.initCommandHistory = initCommandHistory; //# sourceMappingURL=index.js.map