UNPKG

artes

Version:

The simplest way to automate UI and API tests using Cucumber-style steps.

214 lines (179 loc) 5.11 kB
const { context } = require("../../hooks/context"); let elements = {}; function addElements(newElements) { elements = { ...elements, ...newElements }; } // async function locatorExistenceChecker(locator){ // const locatorCount = await locator.count(); // console.log(locator, locatorCount) // return locatorCount ==0 ? false : true; // } function selectorSeparator(element) { if (typeof element !== "string") return element; const selector = element?.split("="); const validTypes = [ "xpath", "name", "placeholder", "text", "label", "role", "alt", "title", "testid", ]; if (selector && validTypes.includes(selector[0]?.trim())) { return [ selector[0].trim(), selector[1] !== undefined ? selector[1].trim() : "", ]; } else { return selector.join("="); } } function getSelector(element) { element = resolveVariable(element) const selector = elements?.[element]?.selector || elements?.[element] || element; return resolveVariable(selectorSeparator(selector)); } function getElement(element) { if (!context.page) { throw new Error("Page context is not initialized."); } const selector = getSelector(element); const waitTime = elements[element]?.waitTime * 1000 || 0; let locator; switch (selector[0]) { case "xpath": locator = context.page.locator(`xpath=${selector[1]}`, { exact: true }); break; case "name": locator = context.page.locator(`[name="${selector[1]}"]`, { exact: true, }); break; case "placeholder": locator = context.page.getByPlaceholder(selector[1], { exact: true }); break; case "text": locator = context.page.getByText(selector[1], { exact: true }); break; case "label": locator = context.page.getByLabel(selector[1], { exact: true }); break; case "role": locator = context.page.getByRole(selector[1], { exact: true }); break; case "alt": locator = context.page.getByAltText(selector[1], { exact: true }); break; case "title": locator = context.page.getByTitle(selector[1], { exact: true }); break; case "testid": locator = context.page.getByTestId(selector[1], { exact: true }); break; default: locator = context.page.locator(selector, { exact: true }); break; } return locator; } function extractVarsFromResponse(responseBody, vars, customVarNames) { function getValueByPath(obj, path) { const keys = path.split("."); let current = obj; if (typeof obj === "string") return obj; for (const key of keys) { if (current && typeof current === "object" && key in current) { current = current[key]; } else { return undefined; } } return current; } function pathToCamelCase(path) { const parts = path.split("."); return parts .map((part, index) => { if (index === 0) return part; return part.charAt(0).toUpperCase() + part.slice(1); }) .join(""); } const varPaths = vars.split(",").map(v => v.trim()); let customNames = []; if (!customVarNames) { customNames = varPaths.map(pathToCamelCase); } else if (typeof customVarNames === "string") { customNames = customVarNames.split(",").map(n => n.trim()); } else if (Array.isArray(customVarNames)) { customNames = customVarNames; } else { throw new Error("customVarNames must be a string or an array"); } if (customNames.length !== varPaths.length) { customNames = varPaths.map(pathToCamelCase); } varPaths.forEach((path, index) => { const value = getValueByPath(responseBody, path); if (value !== undefined) { saveVar(value, customNames[index], path); } }); } function saveVar(value, customName, path) { if (!customName) { const flatKey = path .split(".") .map((part, i) => i === 0 ? part : part[0].toUpperCase() + part.slice(1), ) .join(""); context.vars[flatKey] = value; } else { context.vars[customName] = value; } } function resolveVariable(template) { if (typeof template === "string") { return template.replace(/{{\s*(\w+)\s*}}/g, (_, varName) => { let value = context.vars[varName]; if (value !== undefined) { if (typeof value !== "string") { try { value = JSON.stringify(value); } catch { value = String(value); } } return value .replace(/\n/g, "\\n") .replace(/\r/g, "\\r") .replace(/\t/g, "\\t"); } return `{{${varName}}}`; }); } if (Array.isArray(template)) { return template.map((item) => resolveVariable(item)); } if (template && typeof template === "object") { const result = {}; for (const key in template) { result[key] = resolveVariable(template[key]); } return result; } return template; } module.exports = { getElement, addElements, getSelector, extractVarsFromResponse, saveVar, resolveVariable, };