UNPKG

playwright-cucumber-ts-steps

Version:

A collection of reusable Playwright step definitions for Cucumber in TypeScript, designed to streamline end-to-end testing across web, API, and mobile applications.

73 lines (72 loc) 2.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveValue = resolveValue; exports.resolveLoginValue = resolveLoginValue; exports.deriveSessionName = deriveSessionName; exports.resolveSessionPath = resolveSessionPath; exports.normalizeDeviceName = normalizeDeviceName; // src/helpers/utils/resolveUtils.ts const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const test_1 = require("@playwright/test"); // Dynamic resolver function resolveValue(input) { // Uppercase = environment variable (e.g. TEST_USER) if (/^[A-Z0-9_]+$/.test(input)) { const envVal = process.env[input]; if (!envVal) { throw new Error(`Environment variable ${input} not found.`); } return envVal; } // Dot = JSON reference (e.g. userData.email) if (input.includes(".")) { const [fileName, fieldName] = input.split("."); const jsonPath = path_1.default.resolve("e2e/support/helper/test-data", `${fileName}.json`); if (fs_1.default.existsSync(jsonPath)) { const json = JSON.parse(fs_1.default.readFileSync(jsonPath, "utf-8")); const value = json[fieldName]; if (value !== undefined) return value; throw new Error(`Field "${fieldName}" not found in ${fileName}.json`); } } // Default to hardcoded value return input; } function resolveLoginValue(raw, world) { // ✅ Alias: @aliasName if (raw.startsWith("@")) { return world.data[raw.slice(1)]; } // ✅ JSON: user.json:key if (raw.includes(".json:")) { const [filename, key] = raw.split(".json:"); const filePath = path_1.default.resolve("test-data", `${filename}.json`); if (!fs_1.default.existsSync(filePath)) { throw new Error(`JSON fixture not found: ${filename}.json`); } const fileData = JSON.parse(fs_1.default.readFileSync(filePath, "utf-8")); return fileData[key]; } // ✅ Fallback to raw value return raw; } // Determine session name from email or username function deriveSessionName(emailOrUser, fallback = "default") { if (!emailOrUser) return `${fallback}User`; const base = emailOrUser.includes("@") ? emailOrUser.split("@")[0] : emailOrUser; return `${base}User`; } function resolveSessionPath(world, name) { const dir = path_1.default.resolve(world.data.artifactDir || "test-artifacts", "auth-cookies"); fs_1.default.mkdirSync(dir, { recursive: true }); return path_1.default.resolve(dir, name.endsWith(".json") ? name : `${name}.json`); } function normalizeDeviceName(name) { return (Object.keys(test_1.devices).find((device) => device.toLowerCase().replace(/\s+/g, "") === name.toLowerCase().replace(/[-_\s]+/g, "")) || ""); }