From 73e4ee95cf679bc705f7a98944805c4a3d733712 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Thu, 19 Oct 2023 16:11:59 +0100 Subject: [PATCH 1/4] Fix clearPermissions Add the missing cdp executor which will be used to perform the cdp request. Related: /~https://github.com/grafana/xk6-browser/pull/1040 --- common/browser_context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/browser_context.go b/common/browser_context.go index 8991fa72b..133f02982 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -187,7 +187,7 @@ func (b *BrowserContext) ClearPermissions() error { b.logger.Debugf("BrowserContext:ClearPermissions", "bctxid:%v", b.id) action := cdpbrowser.ResetPermissions().WithBrowserContextID(b.id) - if err := action.Do(b.ctx); err != nil { + if err := action.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil { return fmt.Errorf("clearing permissions: %w", err) } From e2412d183b12f37a110d6fc10cca14bcd86f683d Mon Sep 17 00:00:00 2001 From: ankur22 Date: Thu, 19 Oct 2023 16:13:10 +0100 Subject: [PATCH 2/4] Update the grant_permissions.js script Add a clearPermissions command which will ensure that we don't regress the fix for clearPermissions. --- examples/grant_permission.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/grant_permission.js b/examples/grant_permission.js index c81db914d..0df1a78db 100644 --- a/examples/grant_permission.js +++ b/examples/grant_permission.js @@ -26,8 +26,9 @@ export default async function() { const page = context.newPage(); try { - await page.goto('http://whatsmyuseragent.org/'); + await page.goto('https://test.k6.io/'); page.screenshot({ path: `example-chromium.png` }); + context.clearPermissions(); } finally { page.close(); } From befd7fa09a23681f30e1c0276d2368097214a62a Mon Sep 17 00:00:00 2001 From: ankur22 Date: Thu, 19 Oct 2023 16:14:05 +0100 Subject: [PATCH 3/4] Add integration test for clearPermissions --- tests/browser_context_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/browser_context_test.go b/tests/browser_context_test.go index bfe561408..8f1a35164 100644 --- a/tests/browser_context_test.go +++ b/tests/browser_context_test.go @@ -841,3 +841,32 @@ func TestBrowserContextGrantPermissions(t *testing.T) { }) } } + +func TestBrowserContextClearPermissions(t *testing.T) { + t.Parallel() + + t.Run("no_permissions_set", func(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + bCtx, err := tb.NewContext(nil) + require.NoError(t, err) + + err = bCtx.ClearPermissions() + assert.NoError(t, err) + }) + + t.Run("permissions_set", func(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + bCtx, err := tb.NewContext(nil) + require.NoError(t, err) + + err = bCtx.GrantPermissions([]string{"geolocation"}, common.NewGrantPermissionsOptions()) + require.NoError(t, err) + + err = bCtx.ClearPermissions() + assert.NoError(t, err) + }) +} From 8c31905786330392001ce9aaac7cc6a98be48693 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Mon, 23 Oct 2023 15:05:14 +0100 Subject: [PATCH 4/4] Add permission assertions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds code to assert on whether the permissions have been granted before and after certain actions. Co-authored-by: İnanç Gümüş --- tests/browser_context_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/browser_context_test.go b/tests/browser_context_test.go index 8f1a35164..c629e6582 100644 --- a/tests/browser_context_test.go +++ b/tests/browser_context_test.go @@ -845,15 +845,33 @@ func TestBrowserContextGrantPermissions(t *testing.T) { func TestBrowserContextClearPermissions(t *testing.T) { t.Parallel() + hasPermission := func(tb *testBrowser, p *common.Page, perm string) bool { + t.Helper() + + js := fmt.Sprintf(` + (perm) => navigator.permissions.query( + { name: %q } + ).then(result => result.state) + `, perm) + v := p.Evaluate(tb.toGojaValue(js)) + + return tb.asGojaValue(v).String() == "granted" + } + t.Run("no_permissions_set", func(t *testing.T) { t.Parallel() tb := newTestBrowser(t) bCtx, err := tb.NewContext(nil) require.NoError(t, err) + p, err := bCtx.NewPage() + require.NoError(t, err) + + require.False(t, hasPermission(tb, p, "geolocation")) err = bCtx.ClearPermissions() assert.NoError(t, err) + require.False(t, hasPermission(tb, p, "geolocation")) }) t.Run("permissions_set", func(t *testing.T) { @@ -862,11 +880,17 @@ func TestBrowserContextClearPermissions(t *testing.T) { tb := newTestBrowser(t) bCtx, err := tb.NewContext(nil) require.NoError(t, err) + p, err := bCtx.NewPage() + require.NoError(t, err) + + require.False(t, hasPermission(tb, p, "geolocation")) err = bCtx.GrantPermissions([]string{"geolocation"}, common.NewGrantPermissionsOptions()) require.NoError(t, err) + require.True(t, hasPermission(tb, p, "geolocation")) err = bCtx.ClearPermissions() assert.NoError(t, err) + require.False(t, hasPermission(tb, p, "geolocation")) }) }