Path: blob/trunk/javascript/selenium-webdriver/bidi/external/permissions.js
2885 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617const PermissionState = Object.freeze({18GRANTED: 'granted',19DENIED: 'denied',20PROMPT: 'prompt',21})2223class Permission {24constructor(driver) {25this._driver = driver26}2728async init() {29if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {30throw Error('WebDriver instance must support BiDi protocol')31}3233this.bidi = await this._driver.getBidi()34}3536/**37* Sets a permission state for a given permission descriptor.38* @param {Object} permissionDescriptor The permission descriptor.39* @param {string} state The permission state (granted, denied, prompt).40* @param {string} origin The origin for which the permission is set.41* @param {string} [userContext] The user context id (optional).42* @returns {Promise<void>}43*/44async setPermission(permissionDescriptor, state, origin, userContext = null) {45if (!Object.values(PermissionState).includes(state)) {46throw new Error(`Invalid permission state. Must be one of: ${Object.values(PermissionState).join(', ')}`)47}4849const command = {50method: 'permissions.setPermission',51params: {52descriptor: permissionDescriptor,53state: state,54origin: origin,55},56}5758if (userContext) {59command.params.userContext = userContext60}6162await this.bidi.send(command)63}64}6566async function getPermissionInstance(driver) {67let instance = new Permission(driver)68await instance.init()69return instance70}7172module.exports = { getPermissionInstance, PermissionState }737475