UNPKG

cypress-browser-permissions

Version:

A Cypress plugin package to handle setting common browser permissions like notifications, geolocation, images, and more

50 lines 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBrowserLaunchOptionsPermissionsPath = exports.getBrowserPermissionsFromEnv = exports.toPermissionState = void 0; const lodash_1 = require("lodash"); const types_1 = require("./types"); const constants_1 = require("./constants"); function toPermissionState(value) { const maybeNumber = (0, lodash_1.toNumber)(value); if ((0, lodash_1.isNumber)(maybeNumber) && maybeNumber >= 0 && maybeNumber <= 2) { return maybeNumber; } const lowerValue = String(value).toLowerCase(); if (lowerValue === 'allow') { return types_1.PermissionState.allow; } else if (lowerValue === 'block') { return types_1.PermissionState.block; } else if (lowerValue === 'ask') { return types_1.PermissionState.ask; } else { throw new TypeError('Cannot determine permission state from value: ' + value); } } exports.toPermissionState = toPermissionState; function getBrowserPermissionsFromEnv(env) { const permissionsRecord = (0, lodash_1.chain)(env[constants_1.PLUGIN_ENV_VAR]) .mapKeys((_, key) => (0, lodash_1.toLower)(key)) .mapValues((value) => toPermissionState(value)) .value(); return permissionsRecord; } exports.getBrowserPermissionsFromEnv = getBrowserPermissionsFromEnv; function getBrowserLaunchOptionsPermissionsPath(browserFamily, permission) { const rootPath = constants_1.PREFERENCES_ROOT_PATH_BY_FAMILY[browserFamily]; const containerPath = constants_1.PERMISSIONS_PREF_CONTAINER_BY_FAMILY[browserFamily]; const valuePath = constants_1.PERMISSIONS_PREF_NAME_BY_FAMILY[browserFamily][permission]; if (rootPath && containerPath && valuePath) { if (browserFamily === 'firefox') { return `${rootPath}.["${containerPath}.${valuePath}"]`; } else { return `${rootPath}.${containerPath}.${valuePath}`; } } return undefined; } exports.getBrowserLaunchOptionsPermissionsPath = getBrowserLaunchOptionsPermissionsPath; //# sourceMappingURL=helpers.js.map