UNPKG

ui-omakase-framework

Version:

A comprehensive E2E testing framework library with pre-built Cucumber step definitions and utilities for web automation testing

102 lines (98 loc) 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadConfigWithFallbacks = exports.getJsonFromFile = exports.envNumber = exports.envBoolean = exports.env = void 0; var _path = _interopRequireDefault(require("path")); var _fs = _interopRequireDefault(require("fs")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Enhanced environment parsing with default fallbacks for library usage */ const getJsonFromFile = filePath => { const resolvedPath = _path.default.isAbsolute(filePath) ? filePath : _path.default.resolve(process.cwd(), filePath); if (!_fs.default.existsSync(resolvedPath)) { throw new Error(`Configuration file not found: ${resolvedPath}`); } try { return require(resolvedPath); } catch (error) { throw new Error(`Failed to parse JSON file: ${resolvedPath}. ${error}`); } }; exports.getJsonFromFile = getJsonFromFile; const env = (key, defaultValue) => { const value = process.env[key]; if (value !== undefined) { return value; } if (defaultValue !== undefined) { return defaultValue; } // Provide framework defaults for common configuration keys const frameworkDefaults = { 'NODE_ENV': 'localhost', 'COMMON_CONFIG_FILE': './env/common.env', 'ENV_PATH': './env/', 'HOSTS_URLS_PATH': '/config/hosts.json', 'PAGE_URLS_PATH': '/config/pages.json', 'EMAILS_URLS_PATH': '/config/emails.json', 'ERRORS_URLS_PATH': '/config/errors.json', 'MOCKS_URLS_PATH': '/config/mocks.json', 'PAGE_ELEMENTS_PATH': '/config/mappings/', 'MOCK_PAYLOAD_PATH': '/config/json_payloads/', 'PARALLEL': '1', 'RETRY': '0', 'UI_AUTOMATION_BROWSER': 'chromium', 'HEADLESS': 'true', 'BROWSER_WIDTH': '1920', 'BROWSER_HEIGHT': '1080' }; const defaultVal = frameworkDefaults[key]; if (defaultVal) { return defaultVal; } throw new Error(`No environment variable found for ${key} and no default provided`); }; exports.env = env; const envNumber = (key, defaultValue) => { const stringValue = defaultValue !== undefined ? env(key, defaultValue.toString()) : env(key); const parsed = Number(stringValue); if (isNaN(parsed)) { throw new Error(`Environment variable ${key} is not a valid number: ${stringValue}`); } return parsed; }; exports.envNumber = envNumber; const envBoolean = (key, defaultValue) => { const stringValue = defaultValue !== undefined ? env(key, defaultValue.toString()) : env(key); const normalizedValue = stringValue.toLowerCase().trim(); if (['true', '1', 'yes', 'on'].includes(normalizedValue)) { return true; } if (['false', '0', 'no', 'off'].includes(normalizedValue)) { return false; } throw new Error(`Environment variable ${key} is not a valid boolean: ${stringValue}`); }; /** * Loads configuration with multiple fallback strategies */ exports.envBoolean = envBoolean; const loadConfigWithFallbacks = (primaryPath, fallbackPaths = [], isRequired = true) => { const allPaths = [primaryPath, ...fallbackPaths]; for (const configPath of allPaths) { try { if (_fs.default.existsSync(configPath)) { return getJsonFromFile(configPath); } } catch (error) { console.warn(`Failed to load config from ${configPath}:`, error); } } if (isRequired) { throw new Error(`Could not load required configuration from any of: ${allPaths.join(', ')}`); } return null; }; exports.loadConfigWithFallbacks = loadConfigWithFallbacks;