All Projects → kamranayub → cypress-browser-permissions

kamranayub / cypress-browser-permissions

Licence: MIT license
A Cypress plugin to set launched browser preferences including permissions like Geolocation, Notifications, Microphone, etc.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to cypress-browser-permissions

cypress-dotenv
Cypress plugin that enables compatability with dotenv
Stars: ✭ 47 (+17.5%)
Mutual labels:  cypress, cypress-plugin
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (+0%)
Mutual labels:  e2e, cypress
cypress-plugin-stripe-elements
A small Cypress plugin that assists you in filling in Stripe Elements inputs
Stars: ✭ 22 (-45%)
Mutual labels:  cypress, cypress-plugin
cypress-watch-and-reload
Reloads Cypress when one of the watched files changes
Stars: ✭ 46 (+15%)
Mutual labels:  cypress, cypress-plugin
cypress-page-object
Represent the screens of your website as a series of objects in your Cypress test suite
Stars: ✭ 23 (-42.5%)
Mutual labels:  e2e, cypress
cypress-xhr-responses-recording
No description or website provided.
Stars: ✭ 19 (-52.5%)
Mutual labels:  e2e, cypress
Recorder
A browser extension that generates Cypress, Playwright and Puppeteer test scripts from your interactions 🖱 ⌨
Stars: ✭ 277 (+592.5%)
Mutual labels:  e2e, cypress
cypress-xpath
Adds XPath command to Cypress test runner
Stars: ✭ 145 (+262.5%)
Mutual labels:  cypress, cypress-plugin
cypress-maildev
Cypress Maildev is a bunch of Cypress commands in order to test your messages (SMS and Emails) by using Maildev REST API.
Stars: ✭ 19 (-52.5%)
Mutual labels:  e2e, cypress
cygger
Boilerplate generator for API Testing from Swagger to Cypress
Stars: ✭ 20 (-50%)
Mutual labels:  e2e, cypress
cypress-browserify-preprocessor
Cypress preprocessor for bundling JavaScript via browserify
Stars: ✭ 23 (-42.5%)
Mutual labels:  cypress, cypress-plugin
odoo-cypress
Odoo Framework E2E Testing using Cypress
Stars: ✭ 19 (-52.5%)
Mutual labels:  e2e, cypress
cypress-get-it
Get elements by data attribute by creating a Cy command on the fly
Stars: ✭ 23 (-42.5%)
Mutual labels:  cypress, cypress-plugin
react-ssr-advanced-seed
🔮 React SSR Advanced Seed (Typescript + nestJS + React SSR + React Native + Docker)
Stars: ✭ 76 (+90%)
Mutual labels:  e2e
django-improved-permissions
Django application made to make django's default permission system more robust.
Stars: ✭ 14 (-65%)
Mutual labels:  permissions
wargabantuwarga.com
Inisiatif warga untuk berbagi informasi seputar fasilitas kesehatan dan alat kesehatan untuk COVID-19.
Stars: ✭ 533 (+1232.5%)
Mutual labels:  cypress
orchestrator
The orchestrator executes all cypress specs across parallel docker containers based on a configuration file.
Stars: ✭ 61 (+52.5%)
Mutual labels:  cypress
cypress-routines
Easily write scalable Node.js setup code for Cypress
Stars: ✭ 43 (+7.5%)
Mutual labels:  cypress
instrument-cra
Little module for CRA applications to instrument code without ejecting react-scripts
Stars: ✭ 61 (+52.5%)
Mutual labels:  cypress
svelte-starter-kit
Svelte starter kit — router, state management and testing included.
Stars: ✭ 16 (-60%)
Mutual labels:  cypress

cypress-browser-permissions

GitHub code size in bytes GitHub repo size npm npm npm npm NPM npm GitHub last commit npm collaborators

A Cypress plugin to manage browser launch permissions for various APIs such as Notifications, Geolocation, Cookies, Images, and more.

Video of notification being displayed

These APIs can be controlled using browser profile preferences which this plugin will generate and pass for you, as well as resetting them for each test run (otherwise they will be persisted).

This enables you to effectively test permissions-based APIs in continuous integration environments and in headed browsers without prompts. 🎉

Table of Contents

Usage

👋 Read the dev.to introduction post for a quick start guide and an example!

Install the package

npm

npm i cypress-browser-permissions --save-dev

yarn

yarn install cypress-browser-permissions --save-dev

Import and initialize the plugin

In cypress/plugins/index.js:

CommonJS

const { cypressBrowserPermissionsPlugin } = require('cypress-browser-permissions')

module.exports = (on, config) => {
  // The plugin may modify the Cypress config, so be sure
  // to return it
  config = cypressBrowserPermissionsPlugin(on, config)

  //
  // Any existing plugins you are using
  //

  return config
}

ES2015

import { cypressBrowserPermissionsPlugin } from 'cypress-browser-permissions'

module.exports = (on, config) => {
  // The plugin may modify the Cypress config, so be sure
  // to return it
  config = cypressBrowserPermissionsPlugin(on, config)

  //
  // Any existing plugins you are using
  //

  return config
}

Setting Permissions

Setting permissions should work in Chromium (Google Chrome, Microsoft Edge Chromium) and Firefox. They won't take effect in other browser families.

Permissions can be set using Cypress environment variables. The plugin reads permissions from Cypress.env.browserPermissions and supports all the existing ways to set Cypress environment variables.

Example of enabling permissions

In cypress.json

In cypress.json, set the env.browserPermissions property with a map of permissions:

{
  "env": {
    "browserPermissions": {
      "notifications": "allow",
      "geolocation": "allow",
      "camera": "block",
      "microphone": "block",
      "images": "allow",
      "javascript": "allow",
      "popups": "ask",
      "plugins": "ask",
      "cookies": "allow"
    }
  }
}

In cypress.env.json

In cypress.env.json, it follows the same convention:

{
  "browserPermissions": {
    "notifications": "allow",
    "geolocation": "allow",
    "camera": "block",
    "microphone": "block",
    "images": "allow",
    "javascript": "allow",
    "popups": "ask",
    "plugins": "ask",
    "cookies": "allow"
  }
}

Via cypress open or cypress run

Since the configuration is nested, you must pass in the permissions as a stringified JSON object:

$ cypress run  --env '{\"browserPermissions\": {\"notifications\": 1}}'
$ cypress open --env '{\"browserPermissions\": {\"notifications\": 1}}'

Via machine environment variables

By default, Cypress cannot handle nested variable objects but this plugin will correctly find environment variables that match what it expects and will translate them properly for you automatically:

CYPRESS_browser_permissions_notifications=allow cypress run

Remember: When passing Cypress env vars from the outside, such as from a script, prefix them with CYPRESS_ e.g. CYPRESS_browser_permissions_notifications=allow. Cypress automatically strips the prefix when passing to Cypress.env

Supported Permissions

These are the supported permission names of the plugin:

Chrome / Edge (Chromium)
  • notifications
  • geolocation
  • camera
  • microphone
  • images
  • popups
  • javascript
  • cookies
  • plugins
Firefox
  • notifications
  • geolocation
  • camera
  • microphone
  • images

Supported Values

Values for a permission can be any of the following:

  • 0 or ask - The default permission, which is to prompt the user
  • 1 or allow - Allow the permission
  • 2 or block - Block the permission

Checking Permissions

In your Cypress test suites, you can import permissions helpers from the the package.

Usage Example

my-test.spec.js

import { isPermissionAllowed, isPermissionBlocked, isPermissionAsk } from 'cypress-browser-permissions'

describe('my site', () => {
  before(() => cy.visit('/'))

  isPermissionAllowed('notifications') &&
    it('should show desktop notification', () => {
      /* ... */
    })

  isPermissionBlocked('notifications') &&
    it('should warn user desktop notifications are disabled', () => {
      /* ... */
    })

  isPermissionAsk('notifications') &&
    it('should prompt user to allow desktop notifications', () => {
      /* ... */
    })
})

Also see cypress/integration/ folder for e2e examples.

API Reference

See API reference for documented methods.

Resetting Permissions

This plugin automatically resets each supported permission to the browser default for each test run since otherwise profile preferences are persisted across sessions, which may not be what you intend.

Details

How It Works

Cypress can pass preferences when launching browsers. This plugin adds a small abstraction over this low-level API to take care of setting the permission-related preferences in different browsers, mostly Chrome/Chromium and Firefox.

You can listen to the before:browser:launch event in your own Cypress application to add any additional preferences.

Chrome / Edge / Chromium Preferences

Documented in pref_names, the permission-related preferences are grouped under profile.managed_default_content_settings.

These modify the "managed" settings, such as when group policy is enforced. In the Chrome settings, there is a way to add specific sites to allow / block lists, and this may be possible to do with the plugin if that is stored in the profile data structure.

Firefox

In about:config within Firefox, search for permissions.default to list permissions.

Notably, Firefox does not have some permissions related to JavaScript, Cookies, Plugins, and Popups but those may be managed with other settings.

Credits

Thanks to BrowserStack for documenting some of these permissions as well as these StackOverflow posts:

In Web Driver testing, these are passed under capabilities, such as shown in the test-runner configuration and then passing as shown here.

MIT License

See LICENSE


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].