Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix browserContext.clearPermissions #1077

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion examples/grant_permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
53 changes: 53 additions & 0 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,56 @@ 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()
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
require.False(t, hasPermission(tb, p, "geolocation"))
})

t.Run("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.GrantPermissions([]string{"geolocation"}, common.NewGrantPermissionsOptions())
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
require.True(t, hasPermission(tb, p, "geolocation"))

err = bCtx.ClearPermissions()
assert.NoError(t, err)
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
require.False(t, hasPermission(tb, p, "geolocation"))
})
}