Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/external/permissions.js
2885 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
const PermissionState = Object.freeze({
19
GRANTED: 'granted',
20
DENIED: 'denied',
21
PROMPT: 'prompt',
22
})
23
24
class Permission {
25
constructor(driver) {
26
this._driver = driver
27
}
28
29
async init() {
30
if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {
31
throw Error('WebDriver instance must support BiDi protocol')
32
}
33
34
this.bidi = await this._driver.getBidi()
35
}
36
37
/**
38
* Sets a permission state for a given permission descriptor.
39
* @param {Object} permissionDescriptor The permission descriptor.
40
* @param {string} state The permission state (granted, denied, prompt).
41
* @param {string} origin The origin for which the permission is set.
42
* @param {string} [userContext] The user context id (optional).
43
* @returns {Promise<void>}
44
*/
45
async setPermission(permissionDescriptor, state, origin, userContext = null) {
46
if (!Object.values(PermissionState).includes(state)) {
47
throw new Error(`Invalid permission state. Must be one of: ${Object.values(PermissionState).join(', ')}`)
48
}
49
50
const command = {
51
method: 'permissions.setPermission',
52
params: {
53
descriptor: permissionDescriptor,
54
state: state,
55
origin: origin,
56
},
57
}
58
59
if (userContext) {
60
command.params.userContext = userContext
61
}
62
63
await this.bidi.send(command)
64
}
65
}
66
67
async function getPermissionInstance(driver) {
68
let instance = new Permission(driver)
69
await instance.init()
70
return instance
71
}
72
73
module.exports = { getPermissionInstance, PermissionState }
74
75