feat(firefox): implement browserContext.overridePermissions (#4060)

This commit is contained in:
Andrey Lushnikov 2019-02-22 23:59:32 -08:00 committed by GitHub
parent f1a14fec5b
commit 3bea5d6017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 4 deletions

View File

@ -283,6 +283,30 @@ class BrowserContext extends EventEmitter {
this._browserContextId = browserContextId;
}
/**
* @param {string} origin
* @param {!Array<string>} permissions
*/
async overridePermissions(origin, permissions) {
const webPermissionToProtocol = new Map([
['geolocation', 'geo'],
['microphone', 'microphone'],
['camera', 'camera'],
['notifications', 'desktop-notifications'],
]);
permissions = permissions.map(permission => {
const protocolPermission = webPermissionToProtocol.get(permission);
if (!protocolPermission)
throw new Error('Unknown permission: ' + permission);
return protocolPermission;
});
await this._connection.send('Browser.grantPermissions', {origin, browserContextId: this._browserContextId || undefined, permissions});
}
async clearPermissionOverrides() {
await this._connection.send('Browser.resetPermissions', {browserContextId: this._browserContextId || undefined});
}
/**
* @return {Array<Target>}
*/

View File

@ -9,7 +9,7 @@
"node": ">=8.9.4"
},
"puppeteer": {
"firefox_revision": "e0433ecec0642eca12cba778b0587ab2ae06e833"
"firefox_revision": "f7b25713dd00f0deda7032dc25a72d4c7b42446e"
},
"scripts": {
"install": "node install.js",

View File

@ -163,7 +163,7 @@ module.exports.addTests = function({testRunner, expect, headless, Errors, Device
});
});
describe_fails_ffox('BrowserContext.overridePermissions', function() {
describe('BrowserContext.overridePermissions', function() {
function getPermission(page, name) {
return page.evaluate(name => navigator.permissions.query({name}).then(result => result.state), name);
}
@ -199,7 +199,7 @@ module.exports.addTests = function({testRunner, expect, headless, Errors, Device
await page.goto(server.EMPTY_PAGE);
await page.evaluate(() => {
window.events = [];
return navigator.permissions.query({name: 'clipboard-read'}).then(function(result) {
return navigator.permissions.query({name: 'geolocation'}).then(function(result) {
window.events.push(result.state);
result.onchange = function() {
window.events.push(result.state);
@ -209,11 +209,30 @@ module.exports.addTests = function({testRunner, expect, headless, Errors, Device
expect(await page.evaluate(() => window.events)).toEqual(['prompt']);
await context.overridePermissions(server.EMPTY_PAGE, []);
expect(await page.evaluate(() => window.events)).toEqual(['prompt', 'denied']);
await context.overridePermissions(server.EMPTY_PAGE, ['clipboard-read']);
await context.overridePermissions(server.EMPTY_PAGE, ['geolocation']);
expect(await page.evaluate(() => window.events)).toEqual(['prompt', 'denied', 'granted']);
await context.clearPermissionOverrides();
expect(await page.evaluate(() => window.events)).toEqual(['prompt', 'denied', 'granted', 'prompt']);
});
it('should isolate permissions between browser contexs', async({page, server, context, browser}) => {
await page.goto(server.EMPTY_PAGE);
const otherContext = await browser.createIncognitoBrowserContext();
const otherPage = await otherContext.newPage();
await otherPage.goto(server.EMPTY_PAGE);
expect(await getPermission(page, 'geolocation')).toBe('prompt');
expect(await getPermission(otherPage, 'geolocation')).toBe('prompt');
await context.overridePermissions(server.EMPTY_PAGE, []);
await otherContext.overridePermissions(server.EMPTY_PAGE, ['geolocation']);
expect(await getPermission(page, 'geolocation')).toBe('denied');
expect(await getPermission(otherPage, 'geolocation')).toBe('granted');
await context.clearPermissionOverrides();
expect(await getPermission(page, 'geolocation')).toBe('prompt');
expect(await getPermission(otherPage, 'geolocation')).toBe('granted');
await otherContext.close();
});
});
describe_fails_ffox('Page.setGeolocation', function() {