UNPKG

skutter

Version:

Skutter: Opinionated BDD Browser based test framework

90 lines (89 loc) 3.79 kB
"use strict"; const Lazy = require("lazy.js"); const base_1 = require("./base"); const container_1 = require("./container"); class PageElementSearcher { static find(context, target, throwOnNotFound = true) { if (!context) { throw new Error("[PageElementSearcher].[find] context is not specified."); } if (!target) { throw new Error("[PageElementSearcher].[find] target is not specified."); } let found; let targets = target.split("/"); if (targets.length > 1) { let subTarget = Lazy(targets).rest().toArray().join("/"); if (context instanceof container_1.Container) { found = this.find(context, targets[0], throwOnNotFound); if (found && found.find) { found = found.find(subTarget, throwOnNotFound); } } else { found = Lazy(context).find((value) => { if (value instanceof base_1.Base) { return value.name === targets[0]; } return false; }); if (found && found.find) { found = found.find(subTarget, throwOnNotFound); } } } else { found = Lazy(context).find((value) => { if (value instanceof base_1.Base) { return value.name === target; } return false; }); } if (found) { console.log(`[PageElementSearcher].[find] found "${found.name}" as matching.`); } else { console.log("[PageElementSearcher].[find] Didn\'t find the target."); } if (!found && throwOnNotFound) { let allCurrent = (Lazy(context).keys().toArray()).join(", "); throw new Error(`[PageElementSearcher].[find] Unable to find an element with the name "${target}" in the current context. Available: ${allCurrent}`); } if (found === undefined) { found = null; } return found; } static rewindStack(inspectionStack, target, throwOnNotFound = true) { if (!inspectionStack) { throw new Error("[PageElementSearcher].[rewindStack] inspectionStack is not specified."); } if (!target) { throw new Error("[PageElementSearcher].[rewindStack] target is not specified."); } console.log(`[rewindStack] starting search for "${target}".`); let found = null; while (inspectionStack.topItem && found === null) { let currentInspection = inspectionStack.topItem; console.log(`[PageElementSearcher].[rewindStack] checking context: "${currentInspection.definition.name}".`); if (currentInspection.definition.name === target) { console.log("[PageElementSearcher].[rewindStack] found target, not changing the stack any further."); return; } found = this.find(currentInspection.definition.contents, target, false); if (found === null) { console.log("[PageElementSearcher].[rewindStack] Not found in current stack item, popping back."); inspectionStack.pop(); } else { console.log("[PageElementSearcher].[rewindStack] found an element, breaking."); break; } } if (found === null && throwOnNotFound) { throw new Error(`[PageElementSearcher].[rewindStack] Unable to find an element with the name "${target}" anywhere in the inspection stack.`); } } } exports.PageElementSearcher = PageElementSearcher;