kamranayub / cypress-browser-permissions

A Cypress plugin to set launched browser preferences including permissions like Geolocation, Notifications, Microphone, etc.
https://kamranicus.com/cypress-browser-permissions
MIT License
64 stars 4 forks source link

Change permissions between tests #44

Closed tuzmusic closed 1 year ago

tuzmusic commented 2 years ago

As far as I can tell from the docs, this allows you to set the permissions before your suite runs. I need the ability to set different permissions for different tests, to test the effect of those different permissions. Does this plugin allow for this?

savchukm commented 1 year ago

I'm not sure but looks like it doesn't allow to do that. Global browserPermissions in cypress.config.js take over any test suite permission:

export default defineConfig({
    e2e: {
            ...,
        env: {
            browserPermissions: {
                geolocation: 'allow',
            }
        },
    ...
})
it(`It should work`, {
    env: {
        browserPermissions: {
            geolocation: 'block',
        }
    }
}, () => {
    console.log(Cypress.env()) // here geolocation is 'block' but actual permission is always 'allow' for all tests
})
})

Cypress 10.7.0 @kamranayub What am I missing here?

kamranayub commented 1 year ago

I don't think that will work because launchOptions are only set when launching the browser. You can't modify launch options after it's been launched.

Instead, you could try passing the CYPRESS_ env override variable values in a separate test script you run separately. Use a different test suite and skip those suites in the other script.

For testing permissions specifically you don't need to disable them 😃 You can mock them.

See this test suite (which has permissions allowed by default at launch, but I test that when they are blocked, the UI still works):

https://github.com/kamranayub/example-carved-rock-fitness-order-tracker/blob/master/e2e/cypress/integration/notifications.spec.js

kamranayub commented 1 year ago

Unless I am missing something, closing for now but definitely willing to reopen if new info comes to light (perhaps with 10.x?).