Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/browser.js
2884 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 { WindowState, ClientWindowInfo } = require('./clientWindowInfo')
19
20
/**
21
* Represents the commands and events under Browser Module.
22
* Described in https://w3c.github.io/webdriver-bidi/#module-browser
23
*/
24
class Browser {
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
* Creates a new user context.
39
* @returns {Promise<string>} A promise that resolves to the user context id.
40
*/
41
async createUserContext() {
42
const command = {
43
method: 'browser.createUserContext',
44
params: {},
45
}
46
47
let response = await this.bidi.send(command)
48
49
return response.result.userContext
50
}
51
52
/**
53
* Gets the list of all user contexts.
54
* @returns {Promise<string[]>} A promise that resolves to an array of user context ids.
55
*/
56
async getUserContexts() {
57
const command = {
58
method: 'browser.getUserContexts',
59
params: {},
60
}
61
62
let response = await this.bidi.send(command)
63
64
let userContexts = []
65
66
let userContextsArray = response.result.userContexts
67
68
for (let userContextJson of userContextsArray) {
69
userContexts.push(userContextJson.userContext)
70
}
71
72
return userContexts
73
}
74
75
/**
76
* Removes a user context.
77
* @param {string} userContext The user context id to be removed.
78
* @returns {Promise<void>}
79
*/
80
async removeUserContext(userContext) {
81
const command = {
82
method: 'browser.removeUserContext',
83
params: { userContext: userContext },
84
}
85
86
await this.bidi.send(command)
87
}
88
89
/**
90
* Gets information about all client windows.
91
* @returns {Promise<ClientWindowInfo[]>} Array of client window information
92
*/
93
async getClientWindows() {
94
const command = {
95
method: 'browser.getClientWindows',
96
params: {},
97
}
98
99
const response = await this.bidi.send(command)
100
return response.result.clientWindows.map((window) => ClientWindowInfo.fromJson(window))
101
}
102
}
103
104
async function getBrowserInstance(driver) {
105
let instance = new Browser(driver)
106
await instance.init()
107
return instance
108
}
109
110
module.exports = getBrowserInstance
111
module.exports.WindowState = WindowState
112
113