Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/browsingContextInspector.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 { BrowsingContextInfo, NavigationInfo, UserPromptOpened, UserPromptClosed } = require('./browsingContextTypes')
19
20
/**
21
* Represents a browsing context related events.
22
* Described in https://w3c.github.io/webdriver-bidi/#module-contexts-events.
23
* While BrowsingContext class represents a browsing context lifecycle and related commands.
24
* This class is specific to listening to events. Events can be subscribed to multiple browsing contexts or all of them.
25
*/
26
class BrowsingContextInspector {
27
constructor(driver, browsingContextIds) {
28
this._driver = driver
29
this._browsingContextIds = browsingContextIds
30
}
31
32
async init() {
33
this.bidi = await this._driver.getBidi()
34
}
35
36
/**
37
* Subscribes to the 'browsingContext.contextCreated' event.
38
* @param {Function} callback - The callback function to handle the event.
39
* @returns {Promise<void>} - A promise that resolves when the event is emitted.
40
*/
41
async onBrowsingContextCreated(callback) {
42
await this.subscribeAndHandleEvent('browsingContext.contextCreated', callback)
43
}
44
45
/**
46
* Subscribes to the 'browsingContext.contextDestroyed' event.
47
* @param {Function} callback - The callback function to handle the event.
48
* @returns {Promise<void>} - A promise that resolves when the event is emitted.
49
*/
50
async onBrowsingContextDestroyed(callback) {
51
await this.subscribeAndHandleEvent('browsingContext.contextDestroyed', callback)
52
}
53
54
/**
55
* Subscribe to the 'browsingContext.navigationStarted' event.
56
* @param {Function} callback - The callback function to handle the event.
57
* @returns {Promise<void>} - A promise that resolves when the event is emitted.
58
*/
59
async onNavigationStarted(callback) {
60
await this.subscribeAndHandleEvent('browsingContext.navigationStarted', callback)
61
}
62
63
/**
64
* Subscribes to the 'browsingContext.fragmentNavigated' event.
65
*
66
* @param {Function} callback - The callback function to handle the event.
67
* @returns {Promise<void>} - A promise that resolves when the event is emitted.
68
*/
69
async onFragmentNavigated(callback) {
70
await this.subscribeAndHandleEvent('browsingContext.fragmentNavigated', callback)
71
}
72
73
/**
74
* Subscribes to the 'browsingContext.userPromptClosed' event.
75
*
76
* @param {Function} callback - The callback function to handle the event.
77
* @returns {Promise<void>} - A promise that resolves when the event is emitted.
78
*/
79
async onUserPromptClosed(callback) {
80
await this.subscribeAndHandleEvent('browsingContext.userPromptClosed', callback)
81
}
82
83
/**
84
* Subscribes to the 'browsingContext.userPromptOpened' event.
85
*
86
* @param {Function} callback - The callback function to handle the event.
87
* @returns {Promise<void>} - A promise that resolves when the event is emitted.
88
*/
89
async onUserPromptOpened(callback) {
90
await this.subscribeAndHandleEvent('browsingContext.userPromptOpened', callback)
91
}
92
93
/**
94
* Subscribes to the 'browsingContext.domContentLoaded' event.
95
*
96
* @param {Function} callback - The callback function to handle the event.
97
* @returns {Promise<void>} - A promise that resolves when the event is emitted.
98
*/
99
async onDomContentLoaded(callback) {
100
await this.subscribeAndHandleEvent('browsingContext.domContentLoaded', callback)
101
}
102
103
/**
104
* Subscribes to the 'browsingContext.load' event.
105
*
106
* @param {Function} callback - The callback function to handle the event.
107
* @returns {Promise<void>} - A promise that resolves when the event is emitted.
108
*/
109
async onBrowsingContextLoaded(callback) {
110
await this.subscribeAndHandleEvent('browsingContext.load', callback)
111
}
112
113
async subscribeAndHandleEvent(eventType, callback) {
114
if (this._browsingContextIds != null) {
115
await this.bidi.subscribe(eventType, this._browsingContextIds)
116
} else {
117
await this.bidi.subscribe(eventType)
118
}
119
await this._on(callback)
120
}
121
122
async _on(callback) {
123
this.ws = await this.bidi.socket
124
this.ws.on('message', (event) => {
125
const { params } = JSON.parse(Buffer.from(event.toString()))
126
if (params) {
127
let response = null
128
if ('navigation' in params) {
129
response = new NavigationInfo(params.context, params.navigation, params.timestamp, params.url)
130
} else if ('accepted' in params) {
131
response = new UserPromptClosed(params.context, params.accepted, params.userText)
132
} else if ('type' in params) {
133
response = new UserPromptOpened(params.context, params.type, params.message)
134
} else {
135
response = new BrowsingContextInfo(params.context, params.url, params.children, params.parent)
136
}
137
callback(response)
138
}
139
})
140
}
141
142
async close() {
143
if (
144
this._browsingContextIds !== null &&
145
this._browsingContextIds !== undefined &&
146
this._browsingContextIds.length > 0
147
) {
148
await this.bidi.unsubscribe(
149
'browsingContext.contextCreated',
150
'browsingContext.contextDestroyed',
151
'browsingContext.fragmentNavigated',
152
'browsingContext.userPromptClosed',
153
this._browsingContextIds,
154
)
155
} else {
156
await this.bidi.unsubscribe(
157
'browsingContext.contextCreated',
158
'browsingContext.contextDestroyed',
159
'browsingContext.fragmentNavigated',
160
'browsingContext.userPromptClosed',
161
)
162
}
163
}
164
}
165
166
async function getBrowsingContextInstance(driver, browsingContextIds = null) {
167
let instance = new BrowsingContextInspector(driver, browsingContextIds)
168
await instance.init()
169
return instance
170
}
171
172
module.exports = getBrowsingContextInstance
173
174