UNPKG

skutter

Version:

Skutter: Opinionated BDD Browser based test framework

76 lines (75 loc) 3.48 kB
"use strict"; const iskutter_library_1 = require("../framework/iskutter-library"); const skutter_library_1 = require("../framework/skutter-library"); const iskutter_dictionary_1 = require("../framework/iskutter-dictionary"); const definition_finder_1 = require("../framework/page-structure/definition-finder"); const stack_1 = require("../framework/stack"); const html_actions_1 = require("../actions/html-actions"); class InspectionSteps { constructor() { let dictionary = new iskutter_dictionary_1.SkutterDictionary() .define("quotedWordOrWords", /"([^"]+)"/) .define("nonQuotedWord", /([^\s"]+)/); this.lib = new skutter_library_1.SkutterLibrary(dictionary); } createLibrary() { let lib = this.lib; lib.when(new iskutter_library_1.WhenDefinition([ "I am inspecting the $quotedWordOrWords structure", "I am inspecting the $nonQuotedWord structure"]), function (structureName, next) { let contextCopy = this.context; let structureDefinition = definition_finder_1.DefinitionFinder.getDefinition(this.context, structureName); if (!structureDefinition) { throw new Error(`Unable to find structure "${structureName}" in stack or page.`); } let stackTopItem = contextCopy.inspectionStack.topItem; if (stackTopItem) { console.log("Structure definition exists in stack."); let currentElement = stackTopItem.element; let innerElement = currentElement.element(structureDefinition.selector); contextCopy.inspectionStack.push({ element: innerElement, definition: structureDefinition }); next(); } else { console.log("Structure definition exists in Page root."); html_actions_1.HtmlActions.getElement(contextCopy, structureDefinition.selector, true) .then(function (htmlElement) { if (stackTopItem === undefined || stackTopItem === null) { contextCopy.inspectionStack = new stack_1.Stack(); } contextCopy.inspectionStack.push({ element: htmlElement, definition: structureDefinition }); }) .then(function succeeded() { next(); }, function failed(error) { next(error); }); } }); lib.when(new iskutter_library_1.WhenDefinition([ "I am no longer inspecting the $quotedWordOrWords structure", "I am no longer inspecting the $nonQuotedWord structure"]), function (structureName, next) { let _this = this; let stackTopItem = _this.context.inspectionStack.topItem; while (stackTopItem !== null) { if (stackTopItem.definition.name === structureName) { _this.context.inspectionStack.pop(); break; } else { this.context.inspectionStack.pop(); stackTopItem = _this.context.inspectionStack.topItem; } } next(); }); return this.lib; } } exports.default = InspectionSteps;