feat(permissions): make origin optional (#1406)

This commit is contained in:
Pavel Feldman 2020-03-17 15:32:50 -07:00 committed by GitHub
parent 840146273e
commit e4225adcc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 117 additions and 63 deletions

View file

@ -208,7 +208,7 @@ Indicates that the browser is connected.
- `longitude` <[number]> Longitude between -180 and 180. - `longitude` <[number]> Longitude between -180 and 180.
- `accuracy` <[number]> Optional non-negative accuracy value. - `accuracy` <[number]> Optional non-negative accuracy value.
- `locale` <?[string]> Specify user locale, for example `en-GB`, `de-DE`, etc. Locale will affect `navigator.language` value, `Accept-Language` request header value as well as number and date formatting rules. - `locale` <?[string]> Specify user locale, for example `en-GB`, `de-DE`, etc. Locale will affect `navigator.language` value, `Accept-Language` request header value as well as number and date formatting rules.
- `permissions` <[Object]> A map from origin keys to permissions values. See [browserContext.setPermissions](#browsercontextsetpermissionsorigin-permissions) for more details. - `permissions` <[Array]<[string]>> A list of permissions to grant to all pages in this context. See [browserContext.grantPermissions](#browsercontextgrantpermissionspermissions-options) for more details.
- `extraHTTPHeaders` <[Object]> An object containing additional HTTP headers to be sent with every request. All header values must be strings. - `extraHTTPHeaders` <[Object]> An object containing additional HTTP headers to be sent with every request. All header values must be strings.
- `offline` <[boolean]> Whether to emulate network being offline. Defaults to `false`. - `offline` <[boolean]> Whether to emulate network being offline. Defaults to `false`.
- `httpCredentials` <[Object]> Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). - `httpCredentials` <[Object]> Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
@ -246,7 +246,7 @@ Creates a new browser context. It won't share cookies/cache with other browser c
- `longitude` <[number]> Longitude between -180 and 180. - `longitude` <[number]> Longitude between -180 and 180.
- `accuracy` <[number]> Optional non-negative accuracy value. - `accuracy` <[number]> Optional non-negative accuracy value.
- `locale` <?[string]> Specify user locale, for example `en-GB`, `de-DE`, etc. Locale will affect `navigator.language` value, `Accept-Language` request header value as well as number and date formatting rules. - `locale` <?[string]> Specify user locale, for example `en-GB`, `de-DE`, etc. Locale will affect `navigator.language` value, `Accept-Language` request header value as well as number and date formatting rules.
- `permissions` <[Object]> A map from origin keys to permissions values. See [browserContext.setPermissions](#browsercontextsetpermissionsorigin-permissions) for more details. - `permissions` <[Array]<[string]>> A list of permissions to grant to all pages in this context. See [browserContext.grantPermissions](#browsercontextgrantpermissionspermissions-options) for more details.
- `extraHTTPHeaders` <[Object]> An object containing additional HTTP headers to be sent with every request. All header values must be strings. - `extraHTTPHeaders` <[Object]> An object containing additional HTTP headers to be sent with every request. All header values must be strings.
- `offline` <[boolean]> Whether to emulate network being offline. Defaults to `false`. - `offline` <[boolean]> Whether to emulate network being offline. Defaults to `false`.
- `httpCredentials` <[Object]> Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). - `httpCredentials` <[Object]> Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
@ -290,6 +290,7 @@ await context.close();
- [browserContext.close()](#browsercontextclose) - [browserContext.close()](#browsercontextclose)
- [browserContext.cookies([urls])](#browsercontextcookiesurls) - [browserContext.cookies([urls])](#browsercontextcookiesurls)
- [browserContext.exposeFunction(name, playwrightFunction)](#browsercontextexposefunctionname-playwrightfunction) - [browserContext.exposeFunction(name, playwrightFunction)](#browsercontextexposefunctionname-playwrightfunction)
- [browserContext.grantPermissions(permissions[][, options])](#browsercontextgrantpermissionspermissions-options)
- [browserContext.newPage()](#browsercontextnewpage) - [browserContext.newPage()](#browsercontextnewpage)
- [browserContext.pages()](#browsercontextpages) - [browserContext.pages()](#browsercontextpages)
- [browserContext.route(url, handler)](#browsercontextrouteurl-handler) - [browserContext.route(url, handler)](#browsercontextrouteurl-handler)
@ -299,7 +300,6 @@ await context.close();
- [browserContext.setGeolocation(geolocation)](#browsercontextsetgeolocationgeolocation) - [browserContext.setGeolocation(geolocation)](#browsercontextsetgeolocationgeolocation)
- [browserContext.setHTTPCredentials(httpCredentials)](#browsercontextsethttpcredentialshttpcredentials) - [browserContext.setHTTPCredentials(httpCredentials)](#browsercontextsethttpcredentialshttpcredentials)
- [browserContext.setOffline(offline)](#browsercontextsetofflineoffline) - [browserContext.setOffline(offline)](#browsercontextsetofflineoffline)
- [browserContext.setPermissions(origin, permissions[])](#browsercontextsetpermissionsorigin-permissions)
- [browserContext.waitForEvent(event[, optionsOrPredicate])](#browsercontextwaitforeventevent-optionsorpredicate) - [browserContext.waitForEvent(event[, optionsOrPredicate])](#browsercontextwaitforeventevent-optionsorpredicate)
<!-- GEN:stop --> <!-- GEN:stop -->
@ -381,7 +381,7 @@ Clears all permission overrides for the browser context.
```js ```js
const context = await browser.newContext(); const context = await browser.newContext();
context.setPermissions('https://example.com', ['clipboard-read']); await context.grantPermissions(['clipboard-read']);
// do stuff .. // do stuff ..
context.clearPermissions(); context.clearPermissions();
``` ```
@ -447,6 +447,31 @@ const crypto = require('crypto');
})(); })();
``` ```
#### browserContext.grantPermissions(permissions[][, options])
- `permissions` <[Array]<[string]>> A permission or an array of permissions to grant. Permissions can be one of the following values:
- `'*'`
- `'geolocation'`
- `'midi'`
- `'midi-sysex'` (system-exclusive midi)
- `'notifications'`
- `'push'`
- `'camera'`
- `'microphone'`
- `'background-sync'`
- `'ambient-light-sensor'`
- `'accelerometer'`
- `'gyroscope'`
- `'magnetometer'`
- `'accessibility-events'`
- `'clipboard-read'`
- `'clipboard-write'`
- `'payment-handler'`
- `options` <[Object]>
- `origin` <[string]> The [origin] to grant permissions to, e.g. "https://example.com".
- returns: <[Promise]>
Grants specified permissions to the browser context. Only grants corresponding permissions to the given origin if specified.
#### browserContext.newPage() #### browserContext.newPage()
- returns: <[Promise]<[Page]>> - returns: <[Promise]<[Page]>>
@ -544,27 +569,6 @@ To disable authentication, pass `null`.
- `offline` <[boolean]> Whether to emulate network being offline for the browser context. - `offline` <[boolean]> Whether to emulate network being offline for the browser context.
- returns: <[Promise]> - returns: <[Promise]>
#### browserContext.setPermissions(origin, permissions[])
- `origin` <[string]> The [origin] to grant permissions to, e.g. "https://example.com".
- `permissions` <[Array]<[string]>> An array of permissions to grant. All permissions that are not listed here will be automatically denied. Permissions can be one of the following values:
- `'geolocation'`
- `'midi'`
- `'midi-sysex'` (system-exclusive midi)
- `'notifications'`
- `'push'`
- `'camera'`
- `'microphone'`
- `'background-sync'`
- `'ambient-light-sensor'`
- `'accelerometer'`
- `'gyroscope'`
- `'magnetometer'`
- `'accessibility-events'`
- `'clipboard-read'`
- `'clipboard-write'`
- `'payment-handler'`
- returns: <[Promise]>
#### browserContext.waitForEvent(event[, optionsOrPredicate]) #### browserContext.waitForEvent(event[, optionsOrPredicate])
- `event` <[string]> Event name, same one would pass into `browserContext.on(event)`. - `event` <[string]> Event name, same one would pass into `browserContext.on(event)`.
- `optionsOrPredicate` <[Function]|[Object]> Either a predicate that receives an event or an options object. - `optionsOrPredicate` <[Function]|[Object]> Either a predicate that receives an event or an options object.
@ -577,7 +581,7 @@ is fired.
```js ```js
const context = await browser.newContext(); const context = await browser.newContext();
await context.setPermissions('https://html5demos.com', ['geolocation']); await context.grantPermissions(['geolocation']);
``` ```
### class: Page ### class: Page
@ -3824,6 +3828,7 @@ const backgroundPage = await backroundPageTarget.page();
- [browserContext.close()](#browsercontextclose) - [browserContext.close()](#browsercontextclose)
- [browserContext.cookies([urls])](#browsercontextcookiesurls) - [browserContext.cookies([urls])](#browsercontextcookiesurls)
- [browserContext.exposeFunction(name, playwrightFunction)](#browsercontextexposefunctionname-playwrightfunction) - [browserContext.exposeFunction(name, playwrightFunction)](#browsercontextexposefunctionname-playwrightfunction)
- [browserContext.grantPermissions(permissions[][, options])](#browsercontextgrantpermissionspermissions-options)
- [browserContext.newPage()](#browsercontextnewpage) - [browserContext.newPage()](#browsercontextnewpage)
- [browserContext.pages()](#browsercontextpages) - [browserContext.pages()](#browsercontextpages)
- [browserContext.route(url, handler)](#browsercontextrouteurl-handler) - [browserContext.route(url, handler)](#browsercontextrouteurl-handler)
@ -3833,7 +3838,6 @@ const backgroundPage = await backroundPageTarget.page();
- [browserContext.setGeolocation(geolocation)](#browsercontextsetgeolocationgeolocation) - [browserContext.setGeolocation(geolocation)](#browsercontextsetgeolocationgeolocation)
- [browserContext.setHTTPCredentials(httpCredentials)](#browsercontextsethttpcredentialshttpcredentials) - [browserContext.setHTTPCredentials(httpCredentials)](#browsercontextsethttpcredentialshttpcredentials)
- [browserContext.setOffline(offline)](#browsercontextsetofflineoffline) - [browserContext.setOffline(offline)](#browsercontextsetofflineoffline)
- [browserContext.setPermissions(origin, permissions[])](#browsercontextsetpermissionsorigin-permissions)
- [browserContext.waitForEvent(event[, optionsOrPredicate])](#browsercontextwaitforeventevent-optionsorpredicate) - [browserContext.waitForEvent(event[, optionsOrPredicate])](#browsercontextwaitforeventevent-optionsorpredicate)
<!-- GEN:stop --> <!-- GEN:stop -->

View file

@ -9,7 +9,7 @@
"main": "index.js", "main": "index.js",
"playwright": { "playwright": {
"chromium_revision": "750417", "chromium_revision": "750417",
"firefox_revision": "1042", "firefox_revision": "1043",
"webkit_revision": "1179" "webkit_revision": "1179"
}, },
"scripts": { "scripts": {

View file

@ -32,7 +32,7 @@ export type BrowserContextOptions = {
locale?: string, locale?: string,
timezoneId?: string, timezoneId?: string,
geolocation?: types.Geolocation, geolocation?: types.Geolocation,
permissions?: { [key: string]: string[] }, permissions?: string[],
extraHTTPHeaders?: network.Headers, extraHTTPHeaders?: network.Headers,
offline?: boolean, offline?: boolean,
httpCredentials?: types.Credentials, httpCredentials?: types.Credentials,
@ -46,7 +46,7 @@ export interface BrowserContext {
cookies(urls?: string | string[]): Promise<network.NetworkCookie[]>; cookies(urls?: string | string[]): Promise<network.NetworkCookie[]>;
addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>; addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
clearCookies(): Promise<void>; clearCookies(): Promise<void>;
setPermissions(origin: string, permissions: string[]): Promise<void>; grantPermissions(permissions: string[], options?: { origin?: string }): Promise<void>;
clearPermissions(): Promise<void>; clearPermissions(): Promise<void>;
setGeolocation(geolocation: types.Geolocation | null): Promise<void>; setGeolocation(geolocation: types.Geolocation | null): Promise<void>;
setExtraHTTPHeaders(headers: network.Headers): Promise<void>; setExtraHTTPHeaders(headers: network.Headers): Promise<void>;
@ -67,6 +67,7 @@ export abstract class BrowserContextBase extends platform.EventEmitter implement
_closed = false; _closed = false;
private readonly _closePromise: Promise<Error>; private readonly _closePromise: Promise<Error>;
private _closePromiseFulfill: ((error: Error) => void) | undefined; private _closePromiseFulfill: ((error: Error) => void) | undefined;
private _permissions = new Map<string, string[]>();
constructor(options: BrowserContextOptions) { constructor(options: BrowserContextOptions) {
super(); super();
@ -92,8 +93,8 @@ export abstract class BrowserContextBase extends platform.EventEmitter implement
abstract cookies(...urls: string[]): Promise<network.NetworkCookie[]>; abstract cookies(...urls: string[]): Promise<network.NetworkCookie[]>;
abstract addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>; abstract addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
abstract clearCookies(): Promise<void>; abstract clearCookies(): Promise<void>;
abstract setPermissions(origin: string, permissions: string[]): Promise<void>; abstract _doGrantPermissions(origin: string, permissions: string[]): Promise<void>;
abstract clearPermissions(): Promise<void>; abstract _doClearPermissions(): Promise<void>;
abstract setGeolocation(geolocation: types.Geolocation | null): Promise<void>; abstract setGeolocation(geolocation: types.Geolocation | null): Promise<void>;
abstract setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void>; abstract setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void>;
abstract setExtraHTTPHeaders(headers: network.Headers): Promise<void>; abstract setExtraHTTPHeaders(headers: network.Headers): Promise<void>;
@ -103,6 +104,24 @@ export abstract class BrowserContextBase extends platform.EventEmitter implement
abstract route(url: types.URLMatch, handler: network.RouteHandler): Promise<void>; abstract route(url: types.URLMatch, handler: network.RouteHandler): Promise<void>;
abstract close(): Promise<void>; abstract close(): Promise<void>;
async grantPermissions(permissions: string[], options?: { origin?: string }) {
let origin = '*';
if (options && options.origin) {
const url = new URL(options.origin);
origin = url.origin;
}
const existing = new Set(this._permissions.get(origin) || []);
permissions.forEach(p => existing.add(p));
const list = [...existing.values()];
this._permissions.set(origin, list);
await this._doGrantPermissions(origin, list);
}
async clearPermissions() {
this._permissions.clear();
await this._doClearPermissions();
}
setDefaultNavigationTimeout(timeout: number) { setDefaultNavigationTimeout(timeout: number) {
this._timeoutSettings.setDefaultNavigationTimeout(timeout); this._timeoutSettings.setDefaultNavigationTimeout(timeout);
} }

View file

@ -252,8 +252,8 @@ export class CRBrowserContext extends BrowserContextBase {
} }
async _initialize() { async _initialize() {
const entries = Object.entries(this._options.permissions || {}); if (this._options.permissions)
await Promise.all(entries.map(entry => this.setPermissions(entry[0], entry[1]))); await this.grantPermissions(this._options.permissions);
if (this._options.geolocation) if (this._options.geolocation)
await this.setGeolocation(this._options.geolocation); await this.setGeolocation(this._options.geolocation);
if (this._options.offline) if (this._options.offline)
@ -302,7 +302,7 @@ export class CRBrowserContext extends BrowserContextBase {
await this._browser._session.send('Storage.clearCookies', { browserContextId: this._browserContextId || undefined }); await this._browser._session.send('Storage.clearCookies', { browserContextId: this._browserContextId || undefined });
} }
async setPermissions(origin: string, permissions: string[]): Promise<void> { async _doGrantPermissions(origin: string, permissions: string[]) {
const webPermissionToProtocol = new Map<string, Protocol.Browser.PermissionType>([ const webPermissionToProtocol = new Map<string, Protocol.Browser.PermissionType>([
['geolocation', 'geolocation'], ['geolocation', 'geolocation'],
['midi', 'midi'], ['midi', 'midi'],
@ -327,10 +327,10 @@ export class CRBrowserContext extends BrowserContextBase {
throw new Error('Unknown permission: ' + permission); throw new Error('Unknown permission: ' + permission);
return protocolPermission; return protocolPermission;
}); });
await this._browser._session.send('Browser.grantPermissions', { origin, browserContextId: this._browserContextId || undefined, permissions: filtered }); await this._browser._session.send('Browser.grantPermissions', { origin: origin === '*' ? undefined : origin, browserContextId: this._browserContextId || undefined, permissions: filtered });
} }
async clearPermissions() { async _doClearPermissions() {
await this._browser._session.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined }); await this._browser._session.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined });
} }

View file

@ -166,8 +166,8 @@ export class FFBrowserContext extends BrowserContextBase {
} }
async _initialize() { async _initialize() {
const entries = Object.entries(this._options.permissions || {}); if (this._options.permissions)
await Promise.all(entries.map(entry => this.setPermissions(entry[0], entry[1]))); await this.grantPermissions(this._options.permissions);
if (this._options.geolocation) if (this._options.geolocation)
await this.setGeolocation(this._options.geolocation); await this.setGeolocation(this._options.geolocation);
if (this._options.extraHTTPHeaders) if (this._options.extraHTTPHeaders)
@ -227,12 +227,12 @@ export class FFBrowserContext extends BrowserContextBase {
await this._browser._connection.send('Browser.clearCookies', { browserContextId: this._browserContextId || undefined }); await this._browser._connection.send('Browser.clearCookies', { browserContextId: this._browserContextId || undefined });
} }
async setPermissions(origin: string, permissions: string[]): Promise<void> { async _doGrantPermissions(origin: string, permissions: string[]) {
const webPermissionToProtocol = new Map<string, 'geo' | 'microphone' | 'camera' | 'desktop-notifications'>([ const webPermissionToProtocol = new Map<string, 'geo' | 'desktop-notification' | 'persistent-storage' | 'push'>([
['geolocation', 'geo'], ['geolocation', 'geo'],
['microphone', 'microphone'], ['persistent-storage', 'persistent-storage'],
['camera', 'camera'], ['push', 'push'],
['notifications', 'desktop-notifications'], ['notifications', 'desktop-notification'],
]); ]);
const filtered = permissions.map(permission => { const filtered = permissions.map(permission => {
const protocolPermission = webPermissionToProtocol.get(permission); const protocolPermission = webPermissionToProtocol.get(permission);
@ -240,10 +240,10 @@ export class FFBrowserContext extends BrowserContextBase {
throw new Error('Unknown permission: ' + permission); throw new Error('Unknown permission: ' + permission);
return protocolPermission; return protocolPermission;
}); });
await this._browser._connection.send('Browser.grantPermissions', {origin, browserContextId: this._browserContextId || undefined, permissions: filtered}); await this._browser._connection.send('Browser.grantPermissions', { origin: origin, browserContextId: this._browserContextId || undefined, permissions: filtered});
} }
async clearPermissions() { async _doClearPermissions() {
await this._browser._connection.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined }); await this._browser._connection.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined });
} }

View file

@ -190,8 +190,8 @@ export class WKBrowserContext extends BrowserContextBase {
await this._browser._browserSession.send('Playwright.setIgnoreCertificateErrors', { browserContextId: this._browserContextId, ignore: true }); await this._browser._browserSession.send('Playwright.setIgnoreCertificateErrors', { browserContextId: this._browserContextId, ignore: true });
if (this._options.locale) if (this._options.locale)
await this._browser._browserSession.send('Playwright.setLanguages', { browserContextId: this._browserContextId, languages: [this._options.locale] }); await this._browser._browserSession.send('Playwright.setLanguages', { browserContextId: this._browserContextId, languages: [this._options.locale] });
const entries = Object.entries(this._options.permissions || {}); if (this._options.permissions)
await Promise.all(entries.map(entry => this.setPermissions(entry[0], entry[1]))); await this.grantPermissions(this._options.permissions);
if (this._options.geolocation) if (this._options.geolocation)
await this.setGeolocation(this._options.geolocation); await this.setGeolocation(this._options.geolocation);
if (this._options.offline) if (this._options.offline)
@ -244,7 +244,7 @@ export class WKBrowserContext extends BrowserContextBase {
await this._browser._browserSession.send('Playwright.deleteAllCookies', { browserContextId: this._browserContextId }); await this._browser._browserSession.send('Playwright.deleteAllCookies', { browserContextId: this._browserContextId });
} }
async setPermissions(origin: string, permissions: string[]): Promise<void> { async _doGrantPermissions(origin: string, permissions: string[]) {
const webPermissionToProtocol = new Map<string, string>([ const webPermissionToProtocol = new Map<string, string>([
['geolocation', 'geolocation'], ['geolocation', 'geolocation'],
]); ]);
@ -257,7 +257,7 @@ export class WKBrowserContext extends BrowserContextBase {
await this._browser._browserSession.send('Playwright.grantPermissions', { origin, browserContextId: this._browserContextId, permissions: filtered }); await this._browser._browserSession.send('Playwright.grantPermissions', { origin, browserContextId: this._browserContextId, permissions: filtered });
} }
async clearPermissions() { async _doClearPermissions() {
await this._browser._browserSession.send('Playwright.resetPermissions', { browserContextId: this._browserContextId }); await this._browser._browserSession.send('Playwright.resetPermissions', { browserContextId: this._browserContextId });
} }

View file

@ -25,7 +25,7 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
describe.fail(FFOX)('Overrides.setGeolocation', function() { describe.fail(FFOX)('Overrides.setGeolocation', function() {
it('should work', async({page, server, context}) => { it('should work', async({page, server, context}) => {
await context.setPermissions(server.PREFIX, ['geolocation']); await context.grantPermissions(['geolocation']);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.setGeolocation({longitude: 10, latitude: 10}); await context.setGeolocation({longitude: 10, latitude: 10});
const geolocation = await page.evaluate(() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => { const geolocation = await page.evaluate(() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
@ -73,8 +73,7 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
expect(error.message).toContain('Invalid longitude "undefined"'); expect(error.message).toContain('Invalid longitude "undefined"');
}); });
it('should use context options', async({browser, server}) => { it('should use context options', async({browser, server}) => {
const options = { geolocation: { longitude: 10, latitude: 10 }, permissions: {} }; const options = { geolocation: { longitude: 10, latitude: 10 }, permissions: ['geolocation'] };
options.permissions[server.PREFIX] = ['geolocation'];
const context = await browser.newContext(options); const context = await browser.newContext(options);
const page = await context.newPage(); const page = await context.newPage();
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);

View file

@ -21,7 +21,7 @@ const path = require('path');
/** /**
* @type {PageTestSuite} * @type {PageTestSuite}
*/ */
module.exports.describe = function({testRunner, expect, WEBKIT}) { module.exports.describe = function({testRunner, expect, WEBKIT, FFOX}) {
const {describe, xdescribe, fdescribe} = testRunner; const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit, dit} = testRunner; const {it, fit, xit, dit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@ -38,23 +38,55 @@ module.exports.describe = function({testRunner, expect, WEBKIT}) {
}); });
it('should deny permission when not listed', async({page, server, context}) => { it('should deny permission when not listed', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.setPermissions(server.EMPTY_PAGE, []); await context.grantPermissions([], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'geolocation')).toBe('denied'); expect(await getPermission(page, 'geolocation')).toBe('denied');
}); });
it('should fail when bad permission is given', async({page, server, context}) => { it('should fail when bad permission is given', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
let error = {}; let error = {};
await context.setPermissions(server.EMPTY_PAGE, ['foo']).catch(e => error = e); await context.grantPermissions(['foo'], { origin: server.EMPTY_PAGE }).catch(e => error = e);
expect(error.message).toBe('Unknown permission: foo'); expect(error.message).toBe('Unknown permission: foo');
}); });
it('should grant permission when listed', async({page, server, context}) => { it('should grant geolocation permission when listed', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.setPermissions(server.EMPTY_PAGE, ['geolocation']); await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'geolocation')).toBe('granted'); expect(await getPermission(page, 'geolocation')).toBe('granted');
}); });
it.fail(FFOX)('should grant notifications permission when listed', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
await context.grantPermissions(['notifications'], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'notifications')).toBe('granted');
});
it.fail(FFOX)('should accumulate when adding', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
await context.grantPermissions(['geolocation']);
await context.grantPermissions(['notifications']);
expect(await getPermission(page, 'geolocation')).toBe('granted');
expect(await getPermission(page, 'notifications')).toBe('granted');
});
it.fail(FFOX)('should clear permissions', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
await context.grantPermissions(['geolocation']);
await context.clearPermissions();
await context.grantPermissions(['notifications']);
expect(await getPermission(page, 'geolocation')).not.toBe('granted');
expect(await getPermission(page, 'notifications')).toBe('granted');
});
it('should grant permission when listed for all domains', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE);
await context.grantPermissions(['geolocation']);
expect(await getPermission(page, 'geolocation')).toBe('granted');
});
it('should grant permission when creating context', async({server, browser}) => {
const context = await browser.newContext({ permissions: ['geolocation'] });
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'geolocation')).toBe('granted');
await context.close();
});
it('should reset permissions', async({page, server, context}) => { it('should reset permissions', async({page, server, context}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await context.setPermissions(server.EMPTY_PAGE, ['geolocation']); await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'geolocation')).toBe('granted'); expect(await getPermission(page, 'geolocation')).toBe('granted');
await context.clearPermissions(); await context.clearPermissions();
expect(await getPermission(page, 'geolocation')).toBe('prompt'); expect(await getPermission(page, 'geolocation')).toBe('prompt');
@ -71,9 +103,9 @@ module.exports.describe = function({testRunner, expect, WEBKIT}) {
}); });
}); });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt']); expect(await page.evaluate(() => window['events'])).toEqual(['prompt']);
await context.setPermissions(server.EMPTY_PAGE, []); await context.grantPermissions([], { origin: server.EMPTY_PAGE });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied']); expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied']);
await context.setPermissions(server.EMPTY_PAGE, ['geolocation']); await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted']); expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted']);
await context.clearPermissions(); await context.clearPermissions();
expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted', 'prompt']); expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted', 'prompt']);
@ -86,8 +118,8 @@ module.exports.describe = function({testRunner, expect, WEBKIT}) {
expect(await getPermission(page, 'geolocation')).toBe('prompt'); expect(await getPermission(page, 'geolocation')).toBe('prompt');
expect(await getPermission(otherPage, 'geolocation')).toBe('prompt'); expect(await getPermission(otherPage, 'geolocation')).toBe('prompt');
await context.setPermissions(server.EMPTY_PAGE, []); await context.grantPermissions([], { origin: server.EMPTY_PAGE });
await otherContext.setPermissions(server.EMPTY_PAGE, ['geolocation']); await otherContext.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE });
expect(await getPermission(page, 'geolocation')).toBe('denied'); expect(await getPermission(page, 'geolocation')).toBe('denied');
expect(await getPermission(otherPage, 'geolocation')).toBe('granted'); expect(await getPermission(otherPage, 'geolocation')).toBe('granted');