UNPKG

cypress-browser-permissions

Version:

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

60 lines 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cypressBrowserPermissionsPlugin = exports.modifyAndTransformPluginEnv = exports.onBeforeBrowserLaunch = void 0; const lodash_1 = require("lodash"); const types_1 = require("./types"); const helpers_1 = require("./helpers"); const constants_1 = require("./constants"); function resetBrowserPermissions(browser, launchOptions) { const rootPath = constants_1.PREFERENCES_ROOT_PATH_BY_FAMILY[browser.family]; switch (browser.family) { case 'chromium': { const containerPath = constants_1.PERMISSIONS_PREF_CONTAINER_BY_FAMILY.chromium; (0, lodash_1.unset)(launchOptions, `${rootPath}.${containerPath}`); break; } case 'firefox': { const containerPath = constants_1.PERMISSIONS_PREF_CONTAINER_BY_FAMILY.firefox; Object.keys(launchOptions[rootPath]) .filter((pref) => pref.startsWith(containerPath)) .forEach((pref) => { delete launchOptions[rootPath][pref]; }); break; } } } function onBeforeBrowserLaunch(config) { return (browser, launchOptions) => { const requestedPermissions = (0, helpers_1.getBrowserPermissionsFromEnv)(config.env); resetBrowserPermissions(browser, launchOptions); (0, lodash_1.forOwn)(requestedPermissions, (value, permission) => { const path = (0, helpers_1.getBrowserLaunchOptionsPermissionsPath)(browser.family, permission); if (path) { (0, lodash_1.set)(launchOptions, path, value); console.info(`[browserPermissions] permission '${permission}' => '${types_1.PermissionState[value]}'`); } }); return launchOptions; }; } exports.onBeforeBrowserLaunch = onBeforeBrowserLaunch; function modifyAndTransformPluginEnv({ env }) { const envOverrides = (0, lodash_1.pickBy)(env, (_, key) => (0, lodash_1.camelCase)(key).startsWith(constants_1.PLUGIN_ENV_VAR) && (0, lodash_1.camelCase)(key) !== constants_1.PLUGIN_ENV_VAR); const permissionOverrides = (0, lodash_1.mapKeys)(envOverrides, (_, key) => { return (0, lodash_1.camelCase)((0, lodash_1.camelCase)(key).replace(constants_1.PLUGIN_ENV_VAR, '')); }); env.browserPermissions = { ...env.browserPermissions, ...permissionOverrides, }; (0, lodash_1.keys)(envOverrides).forEach((key) => (0, lodash_1.unset)(env, key)); } exports.modifyAndTransformPluginEnv = modifyAndTransformPluginEnv; function cypressBrowserPermissionsPlugin(on, config) { modifyAndTransformPluginEnv(config); on('before:browser:launch', onBeforeBrowserLaunch(config)); return config; } exports.cypressBrowserPermissionsPlugin = cypressBrowserPermissionsPlugin; //# sourceMappingURL=plugin.js.map