How to use ShouldIsolatePermissionsBetweenBrowserContexts method of PuppeteerSharp.Tests.PageTests.BrowserContextOverridePermissionsTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.PageTests.BrowserContextOverridePermissionsTests.ShouldIsolatePermissionsBetweenBrowserContexts

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

BrowserContextOverridePermissionsTests.cs

Source: BrowserContextOverridePermissionsTests.cs Github

copy
1using System.Threading.Tasks;
2using PuppeteerSharp.Tests.Attributes;
3using PuppeteerSharp.Xunit;
4using Xunit;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.PageTests
8{
9    [Collection(TestConstants.TestFixtureCollectionName)]
10    public class BrowserContextOverridePermissionsTests : PuppeteerPageBaseTest
11    {
12        public BrowserContextOverridePermissionsTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        private Task<string> GetPermissionAsync(Page page, string name)
17            => page.EvaluateFunctionAsync<string>(
18                "name => navigator.permissions.query({ name }).then(result => result.state)",
19                name);
20
21        [PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should be prompt by default")]
22        [PuppeteerFact]
23        public async Task ShouldBePromptByDefault()
24        {
25            await Page.GoToAsync(TestConstants.EmptyPage);
26            Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
27        }
28
29        [PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should deny permission when not listed")]
30        [SkipBrowserFact(skipFirefox: true)]
31        public async Task ShouldDenyPermissionWhenNotListed()
32        {
33            await Page.GoToAsync(TestConstants.EmptyPage);
34            await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
35            Assert.Equal("denied", await GetPermissionAsync(Page, "geolocation"));
36        }
37
38        [PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should grant permission when listed")]
39        [SkipBrowserFact(skipFirefox: true)]
40        public async Task ShouldGrantPermissionWhenListed()
41        {
42            await Page.GoToAsync(TestConstants.EmptyPage);
43            await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
44            {
45                OverridePermission.Geolocation
46            });
47            Assert.Equal("granted", await GetPermissionAsync(Page, "geolocation"));
48        }
49
50        [PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should reset permissions")]
51        [SkipBrowserFact(skipFirefox: true)]
52        public async Task ShouldResetPermissions()
53        {
54            await Page.GoToAsync(TestConstants.EmptyPage);
55            await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
56            {
57                OverridePermission.Geolocation
58            });
59            Assert.Equal("granted", await GetPermissionAsync(Page, "geolocation"));
60            await Context.ClearPermissionOverridesAsync();
61            Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
62        }
63
64        [PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should trigger permission onchange")]
65        [SkipBrowserFact(skipFirefox: true)]
66        public async Task ShouldTriggerPermissionOnchange()
67        {
68            await Page.GoToAsync(TestConstants.EmptyPage);
69            await Page.EvaluateFunctionAsync(@"() => {
70                window.events = [];
71                return navigator.permissions.query({ name: 'geolocation'}).then(function(result) {
72                    window.events.push(result.state);
73                    result.onchange = function() {
74                        window.events.push(result.state);
75                    };
76                });
77            }");
78            Assert.Equal(new string[] { "prompt" }, await Page.EvaluateExpressionAsync<string[]>("window.events"));
79            await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
80            Assert.Equal(new string[] { "prompt", "denied" }, await Page.EvaluateExpressionAsync<string[]>("window.events"));
81            await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
82            {
83                OverridePermission.Geolocation
84            });
85            Assert.Equal(
86                new string[] { "prompt", "denied", "granted" },
87                await Page.EvaluateExpressionAsync<string[]>("window.events"));
88            await Context.ClearPermissionOverridesAsync();
89            Assert.Equal(
90                new string[] { "prompt", "denied", "granted", "prompt" },
91                await Page.EvaluateExpressionAsync<string[]>("window.events"));
92        }
93
94        [PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should isolate permissions between browser contexts")]
95        [SkipBrowserFact(skipFirefox: true)]
96        public async Task ShouldIsolatePermissionsBetweenBrowserContexts()
97        {
98            await Page.GoToAsync(TestConstants.EmptyPage);
99            var otherContext = await Browser.CreateIncognitoBrowserContextAsync();
100            var otherPage = await otherContext.NewPageAsync();
101            await otherPage.GoToAsync(TestConstants.EmptyPage);
102            Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
103            Assert.Equal("prompt", await GetPermissionAsync(otherPage, "geolocation"));
104
105            await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
106            await otherContext.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { OverridePermission.Geolocation });
107            Assert.Equal("denied", await GetPermissionAsync(Page, "geolocation"));
108            Assert.Equal("granted", await GetPermissionAsync(otherPage, "geolocation"));
109
110            await Context.ClearPermissionOverridesAsync();
111            Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
112            Assert.Equal("granted", await GetPermissionAsync(otherPage, "geolocation"));
113
114            await otherContext.CloseAsync();
115        }
116    }
117}
118
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger ShouldIsolatePermissionsBetweenBrowserContexts code on LambdaTest Cloud Grid

Execute automation tests with ShouldIsolatePermissionsBetweenBrowserContexts on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)