UNPKG

@uuv/assistant

Version:

UUV Helper used to improve the life of testers and developers by generating cucumber phrases from the GUI.

139 lines (138 loc) 6 kB
"use strict"; /** * Software Name : UUV * * SPDX-License-Identifier: MIT * * This software is distributed under the MIT License, * see the "LICENSE" file for more details * * Authors: NJAKO MOLOM Louis Fredice & SERVICAL Stanley * Software description: Make test writing fast, understandable by any human * understanding English or French. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Translator = void 0; const dom_accessibility_api_1 = require("dom-accessibility-api"); const model_1 = require("./model"); const dictionary_1 = require("@uuv/dictionary"); class Translator { constructor() { this.dictionary = new dictionary_1.EnDictionary(); } translate(htmlElem) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const accessibleRole = (0, dom_accessibility_api_1.getRole)(htmlElem); const accessibleName = (0, dom_accessibility_api_1.computeAccessibleName)(htmlElem); this.selectedHtmlElem = htmlElem; let response = { suggestion: undefined, steps: [] }; if (htmlElem.getAttribute("type") !== "hidden") { if (accessibleRole && accessibleName) { const isInputHtmlOrTextArea = htmlElem instanceof HTMLInputElement || htmlElem instanceof HTMLTextAreaElement; const content = isInputHtmlOrTextArea ? /* eslint-disable @typescript-eslint/no-explicit-any */ htmlElem.value : (_a = htmlElem.getAttribute("value")) !== null && _a !== void 0 ? _a : (_c = (_b = htmlElem.firstChild) === null || _b === void 0 ? void 0 : _b.textContent) === null || _c === void 0 ? void 0 : _c.trim(); if (content) { response = this.getSentenceFromAccessibleRoleAndNameAndContent(accessibleRole, accessibleName, content); } else { response = this.getSentenceFromAccessibleRoleAndName(accessibleRole, accessibleName); } } else { const domSelector = Translator.getSelector(htmlElem); response = this.getSentenceFromDomSelector(domSelector, htmlElem); response.suggestion = new model_1.Suggestion(); } } return response; }); } /* eslint-disable @typescript-eslint/no-explicit-any */ static getSelector(element) { const path = []; while (element.nodeType === Node.ELEMENT_NODE) { let selector = element.nodeName.toLowerCase(); if (element.id) { selector += "#" + element.id; path.unshift(selector); break; } else if (element.getAttribute("data-testid")) { selector += `[data-testid=${element.getAttribute("data-testid")}]`; path.unshift(selector); break; } else { let sibling = element.previousSibling; let index = 1; while (sibling) { if (sibling.nodeType === Node.ELEMENT_NODE && sibling.nodeName.toLowerCase() === selector) { index++; } sibling = sibling.previousSibling; } if (index > 1 || element.nextSibling) { selector += ":nth-of-type(" + index + ")"; } } path.unshift(selector); element = element.parentNode; } return path.join(" > "); } initResponse() { return { suggestion: undefined, steps: [] }; } computeSentenceFromKeyRoleAndName(computedKey, accessibleRole, accessibleName) { return this.dictionary.getRoleBasedSentencesTemplate() .filter(value => value.key === computedKey) .map(sentence => { return this.dictionary.computeSentence({ sentence, accessibleRole, parameters: [accessibleName] }); })[0]; } computeSentenceFromKeyRoleNameAndContent(computedKey, accessibleRole, accessibleName, content) { return this.dictionary.getRoleBasedSentencesTemplate() .filter(value => value.key === computedKey) .map(sentence => { return this.dictionary.computeSentence({ sentence, accessibleRole, parameters: [accessibleName, content] }); })[0]; } computeSentenceFromKeyAndSelector(computedKey, selector) { return this.dictionary.getBaseSentences() .filter((el) => el.key === computedKey) .map((el) => el.wording.replace("{string}", `"${selector}"`))[0]; } computeSentenceFromKeyAndContent(computedKey, content) { return this.computeSentenceFromKeyAndSelector(computedKey, content); } getSentenceFromKey(key) { return this.dictionary.getBaseSentences().filter((el) => el.key === key)[0]; } } exports.Translator = Translator;