Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/browsingcontext_inspector_test.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
'use strict'
19
20
const assert = require('node:assert')
21
const { Browser, By } = require('selenium-webdriver')
22
const { Pages, suite, ignore } = require('../../lib/test')
23
const BrowsingContext = require('selenium-webdriver/bidi/browsingContext')
24
const BrowsingContextInspector = require('selenium-webdriver/bidi/browsingContextInspector')
25
const until = require('selenium-webdriver/lib/until')
26
27
suite(
28
function (env) {
29
let driver
30
let browsingcontextInspector
31
32
beforeEach(async function () {
33
driver = await env.builder().build()
34
})
35
36
afterEach(async function () {
37
await browsingcontextInspector.close()
38
await driver.quit()
39
})
40
41
describe('Browsing Context Inspector', function () {
42
it('can listen to window browsing context created event', async function () {
43
let contextInfo = null
44
browsingcontextInspector = await BrowsingContextInspector(driver)
45
await browsingcontextInspector.onBrowsingContextCreated((entry) => {
46
contextInfo = entry
47
})
48
49
await driver.switchTo().newWindow('window')
50
const windowHandle = await driver.getWindowHandle()
51
assert.equal(contextInfo.id, windowHandle)
52
assert.equal(contextInfo.url, 'about:blank')
53
assert.equal(contextInfo.children, null)
54
assert.equal(contextInfo.parentBrowsingContext, null)
55
})
56
57
it('can listen to browsing context destroyed event', async function () {
58
let contextInfo = null
59
browsingcontextInspector = await BrowsingContextInspector(driver)
60
await browsingcontextInspector.onBrowsingContextDestroyed((entry) => {
61
contextInfo = entry
62
})
63
64
await driver.switchTo().newWindow('window')
65
66
const windowHandle = await driver.getWindowHandle()
67
await driver.close()
68
69
assert.equal(contextInfo.id, windowHandle)
70
assert.equal(contextInfo.url, 'about:blank')
71
72
assert(
73
Array.isArray(contextInfo.children) && contextInfo.children.length === 0,
74
'children should be an empty array',
75
)
76
assert.equal(contextInfo.parentBrowsingContext, null)
77
})
78
79
it('can listen to tab browsing context created event', async function () {
80
let contextInfo = null
81
browsingcontextInspector = await BrowsingContextInspector(driver)
82
await browsingcontextInspector.onBrowsingContextCreated((entry) => {
83
contextInfo = entry
84
})
85
86
await driver.switchTo().newWindow('tab')
87
const tabHandle = await driver.getWindowHandle()
88
89
assert.equal(contextInfo.id, tabHandle)
90
assert.equal(contextInfo.url, 'about:blank')
91
assert.equal(contextInfo.children, null)
92
assert.equal(contextInfo.parentBrowsingContext, null)
93
})
94
95
it('can listen to dom content loaded event', async function () {
96
browsingcontextInspector = await BrowsingContextInspector(driver)
97
let navigationInfo = null
98
await browsingcontextInspector.onDomContentLoaded((entry) => {
99
navigationInfo = entry
100
})
101
102
const browsingContext = await BrowsingContext(driver, {
103
browsingContextId: await driver.getWindowHandle(),
104
})
105
await browsingContext.navigate(Pages.logEntryAdded, 'complete')
106
107
assert.equal(navigationInfo.browsingContextId, browsingContext.id)
108
assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))
109
})
110
111
it('can listen to browsing context loaded event', async function () {
112
let navigationInfo = null
113
browsingcontextInspector = await BrowsingContextInspector(driver)
114
115
await browsingcontextInspector.onBrowsingContextLoaded((entry) => {
116
navigationInfo = entry
117
})
118
const browsingContext = await BrowsingContext(driver, {
119
browsingContextId: await driver.getWindowHandle(),
120
})
121
await browsingContext.navigate(Pages.logEntryAdded, 'complete')
122
123
assert.equal(navigationInfo.browsingContextId, browsingContext.id)
124
assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))
125
})
126
127
ignore(env.browsers(Browser.CHROME, Browser.EDGE)).it(
128
'can listen to navigation started event',
129
async function () {
130
let navigationInfo = null
131
const browsingConextInspector = await BrowsingContextInspector(driver)
132
133
await browsingConextInspector.onNavigationStarted((entry) => {
134
navigationInfo = entry
135
})
136
137
const browsingContext = await BrowsingContext(driver, {
138
browsingContextId: await driver.getWindowHandle(),
139
})
140
141
await browsingContext.navigate(Pages.logEntryAdded, 'complete')
142
143
assert.equal(navigationInfo.browsingContextId, browsingContext.id)
144
assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))
145
},
146
)
147
148
ignore(env.browsers(Browser.EDGE, Browser.CHROME)).it(
149
'can listen to fragment navigated event',
150
async function () {
151
let navigationInfo = null
152
const browsingConextInspector = await BrowsingContextInspector(driver)
153
154
const browsingContext = await BrowsingContext(driver, {
155
browsingContextId: await driver.getWindowHandle(),
156
})
157
await browsingContext.navigate(Pages.linkedImage, 'complete')
158
159
await browsingConextInspector.onFragmentNavigated((entry) => {
160
navigationInfo = entry
161
})
162
163
await browsingContext.navigate(Pages.linkedImage + '#linkToAnchorOnThisPage', 'complete')
164
165
// Chrome/Edge do not return the window's browsing context id as per the spec.
166
// This assertion fails.
167
assert.equal(navigationInfo.browsingContextId, browsingContext.id)
168
assert(navigationInfo.url.includes('linkToAnchorOnThisPage'))
169
},
170
)
171
172
ignore(env.browsers(Browser.EDGE, Browser.CHROME)).it(
173
'can listen to user prompt opened event',
174
async function () {
175
let userpromptOpened = null
176
browsingcontextInspector = await BrowsingContextInspector(driver)
177
178
const browsingContext = await BrowsingContext(driver, {
179
browsingContextId: await driver.getWindowHandle(),
180
})
181
182
await browsingcontextInspector.onUserPromptOpened((entry) => {
183
userpromptOpened = entry
184
})
185
186
await driver.get(Pages.alertsPage)
187
188
await driver.findElement(By.id('alert')).click()
189
190
await driver.wait(until.alertIsPresent())
191
192
await browsingContext.handleUserPrompt(true)
193
194
// Chrome/Edge do not return the window's browsing context id as per the spec.
195
// This assertion fails.
196
// It is probably a bug in the Chrome/Edge driver.
197
assert.equal(userpromptOpened.browsingContextId, browsingContext.id)
198
assert.equal(userpromptOpened.type, 'alert')
199
},
200
)
201
202
ignore(env.browsers(Browser.EDGE, Browser.CHROME)).it(
203
'can listen to user prompt closed event',
204
async function () {
205
const windowHandle = await driver.getWindowHandle()
206
let userpromptClosed = null
207
browsingcontextInspector = await BrowsingContextInspector(driver, windowHandle)
208
209
const browsingContext = await BrowsingContext(driver, {
210
browsingContextId: windowHandle,
211
})
212
213
await driver.get(Pages.alertsPage)
214
215
await driver.findElement(By.id('prompt')).click()
216
217
await driver.wait(until.alertIsPresent())
218
219
await browsingcontextInspector.onUserPromptClosed((entry) => {
220
userpromptClosed = entry
221
})
222
223
await browsingContext.handleUserPrompt(true, 'selenium')
224
225
// Chrome/Edge do not return the window's browsing context id as per the spec.
226
// This assertion fails.
227
// It is probably a bug in the Chrome/Edge driver.
228
assert.equal(userpromptClosed.browsingContextId, browsingContext.id)
229
assert.equal(userpromptClosed.accepted, true)
230
assert.equal(userpromptClosed.userText, 'selenium')
231
},
232
)
233
})
234
},
235
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
236
)
237
238