Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/browsing_context_test.js
11822 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 { suite, ignore, Pages } = require('../../../lib/test')
22
const { Browser, until } = require('selenium-webdriver')
23
24
// Generated BrowsingContext domain class produced by generate_bidi.mjs.
25
const { BrowsingContext } = require('selenium-webdriver/bidi/generated/browsing_context')
26
27
suite(
28
function (env) {
29
let driver
30
let browsingContext
31
32
beforeEach(async function () {
33
driver = await env.builder().build()
34
browsingContext = await BrowsingContext.create(driver)
35
})
36
37
afterEach(function () {
38
return driver.quit()
39
})
40
41
describe('create and close', function () {
42
it('can create a new tab', async function () {
43
const result = await browsingContext.create({ type: 'tab' })
44
45
assert.ok(result.context, 'context id should be present')
46
assert.strictEqual(typeof result.context, 'string')
47
48
await browsingContext.close({ context: result.context })
49
})
50
51
it('can create a new window', async function () {
52
const result = await browsingContext.create({ type: 'window' })
53
54
assert.ok(result.context)
55
assert.strictEqual(typeof result.context, 'string')
56
57
await browsingContext.close({ context: result.context })
58
})
59
60
it('can get browsing context tree', async function () {
61
const contextId = await driver.getWindowHandle()
62
const treeResult = await browsingContext.getTree({ root: contextId })
63
64
assert.ok(Array.isArray(treeResult.contexts))
65
assert.strictEqual(treeResult.contexts.length, 1)
66
assert.strictEqual(treeResult.contexts[0].context, contextId)
67
})
68
69
it('can get tree with max depth', async function () {
70
const contextId = await driver.getWindowHandle()
71
const result = await browsingContext.getTree({ root: contextId, maxDepth: 1 })
72
73
assert.ok(Array.isArray(result.contexts))
74
})
75
})
76
77
describe('navigation', function () {
78
it('can navigate to a url', async function () {
79
const contextId = await driver.getWindowHandle()
80
const result = await browsingContext.navigate({
81
context: contextId,
82
url: Pages.emptyPage,
83
wait: 'complete',
84
})
85
86
assert.ok(result.url.includes('emptyPage') || result.url.includes('empty'))
87
})
88
89
it('can reload a page', async function () {
90
const contextId = await driver.getWindowHandle()
91
await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })
92
93
const result = await browsingContext.reload({ context: contextId, wait: 'complete' })
94
95
assert.ok(result.url)
96
})
97
98
it('can traverse history back and forward', async function () {
99
const contextId = await driver.getWindowHandle()
100
await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })
101
await browsingContext.navigate({ context: contextId, url: Pages.logEntryAdded, wait: 'complete' })
102
103
// Navigate back
104
await browsingContext.traverseHistory({ context: contextId, delta: -1 })
105
await driver.wait(until.urlContains('emptyPage'), 5000)
106
107
const url = await driver.getCurrentUrl()
108
assert.ok(url.includes('emptyPage') || url.includes('empty'))
109
110
// Navigate forward
111
await browsingContext.traverseHistory({ context: contextId, delta: 1 })
112
await driver.wait(until.urlContains('logEntryAdded'), 5000)
113
})
114
})
115
116
describe('activate', function () {
117
it('can activate a browsing context', async function () {
118
const contextId = await driver.getWindowHandle()
119
await browsingContext.activate({ context: contextId })
120
})
121
})
122
123
describe('viewport and screenshot', function () {
124
it('can set viewport size', async function () {
125
const contextId = await driver.getWindowHandle()
126
await browsingContext.setViewport({
127
context: contextId,
128
viewport: { width: 800, height: 600 },
129
})
130
})
131
132
it('can capture a screenshot', async function () {
133
const contextId = await driver.getWindowHandle()
134
await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })
135
136
const result = await browsingContext.captureScreenshot({ context: contextId })
137
138
assert.ok(result.data, 'screenshot data should be present')
139
assert.strictEqual(typeof result.data, 'string')
140
assert.ok(result.data.length > 0)
141
})
142
143
it('can capture screenshot with viewport origin', async function () {
144
const contextId = await driver.getWindowHandle()
145
await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })
146
147
const result = await browsingContext.captureScreenshot({
148
context: contextId,
149
origin: 'viewport',
150
})
151
152
assert.ok(result.data)
153
assert.ok(result.data.length > 0)
154
})
155
})
156
157
describe('handleUserPrompt', function () {
158
it('can accept an alert', async function () {
159
const contextId = await driver.getWindowHandle()
160
await browsingContext.navigate({ context: contextId, url: Pages.alertsPage, wait: 'complete' })
161
162
await driver.findElement({ id: 'alert' }).click()
163
await driver.wait(until.alertIsPresent(), 5000)
164
165
// Dismiss the alert using the generated API
166
await browsingContext.handleUserPrompt({ context: contextId, accept: true })
167
})
168
169
it('can dismiss a confirm dialog', async function () {
170
const contextId = await driver.getWindowHandle()
171
await browsingContext.navigate({ context: contextId, url: Pages.alertsPage, wait: 'complete' })
172
173
await driver.findElement({ id: 'confirm' }).click()
174
await driver.wait(until.alertIsPresent(), 5000)
175
176
await browsingContext.handleUserPrompt({ context: contextId, accept: false })
177
})
178
})
179
180
describe('context lifecycle events', function () {
181
it('fires contextCreated when a new tab is opened', async function () {
182
let createdContext = null
183
184
await browsingContext.onContextCreated((params) => {
185
createdContext = params
186
})
187
188
const result = await browsingContext.create({ type: 'tab' })
189
190
await driver.wait(() => createdContext !== null, 5000)
191
assert.ok(createdContext, 'contextCreated event should have fired')
192
assert.ok(createdContext.context)
193
194
await browsingContext.close({ context: result.context })
195
})
196
197
it('fires contextDestroyed when a tab is closed', async function () {
198
const created = await browsingContext.create({ type: 'tab' })
199
200
let destroyedContext = null
201
await browsingContext.onContextDestroyed((params) => {
202
if (params.context === created.context) {
203
destroyedContext = params
204
}
205
})
206
207
await browsingContext.close({ context: created.context })
208
209
await driver.wait(() => destroyedContext !== null, 5000)
210
assert.ok(destroyedContext, 'contextDestroyed event should have fired')
211
})
212
})
213
214
describe('navigation events', function () {
215
it('fires navigationCommitted on page navigate', async function () {
216
const contextId = await driver.getWindowHandle()
217
let navEvent = null
218
219
await browsingContext.onNavigationCommitted((params) => {
220
if (params.context === contextId) {
221
navEvent = params
222
}
223
})
224
225
await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })
226
await driver.wait(() => navEvent !== null, 5000)
227
228
assert.ok(navEvent, 'navigationCommitted event should have fired')
229
assert.strictEqual(navEvent.context, contextId)
230
assert.ok(navEvent.url)
231
})
232
233
it('fires fragmentNavigated on hash change', async function () {
234
const contextId = await driver.getWindowHandle()
235
await browsingContext.navigate({ context: contextId, url: Pages.emptyPage, wait: 'complete' })
236
237
let fragmentEvent = null
238
await browsingContext.onFragmentNavigated((params) => {
239
if (params.context === contextId) {
240
fragmentEvent = params
241
}
242
})
243
244
// Trigger a hash navigation
245
await driver.executeScript('window.location.hash = "section1"')
246
await driver.wait(() => fragmentEvent !== null, 5000)
247
248
assert.ok(fragmentEvent, 'fragmentNavigated event should have fired')
249
})
250
})
251
252
describe('user prompt events', function () {
253
it('fires userPromptOpened when an alert appears', async function () {
254
const contextId = await driver.getWindowHandle()
255
let promptOpened = null
256
257
await browsingContext.onUserPromptOpened((params) => {
258
if (params.context === contextId) {
259
promptOpened = params
260
}
261
})
262
263
await browsingContext.navigate({ context: contextId, url: Pages.alertsPage, wait: 'complete' })
264
await driver.findElement({ id: 'alert' }).click()
265
await driver.wait(() => promptOpened !== null, 5000)
266
267
assert.ok(promptOpened, 'userPromptOpened should have fired')
268
assert.strictEqual(promptOpened.type, 'alert')
269
270
await browsingContext.handleUserPrompt({ context: contextId, accept: true })
271
})
272
273
it('fires userPromptClosed when an alert is handled', async function () {
274
const contextId = await driver.getWindowHandle()
275
let promptClosed = null
276
277
await browsingContext.onUserPromptClosed((params) => {
278
if (params.context === contextId) {
279
promptClosed = params
280
}
281
})
282
283
await browsingContext.navigate({ context: contextId, url: Pages.alertsPage, wait: 'complete' })
284
await driver.findElement({ id: 'alert' }).click()
285
await driver.wait(until.alertIsPresent(), 5000)
286
287
await browsingContext.handleUserPrompt({ context: contextId, accept: true })
288
await driver.wait(() => promptClosed !== null, 5000)
289
290
assert.ok(promptClosed, 'userPromptClosed should have fired')
291
assert.strictEqual(promptClosed.accepted, true)
292
})
293
})
294
295
describe('locateNodes', function () {
296
it('can locate nodes by css selector', async function () {
297
const contextId = await driver.getWindowHandle()
298
await browsingContext.navigate({ context: contextId, url: Pages.logEntryAdded, wait: 'complete' })
299
300
const result = await browsingContext.locateNodes({
301
context: contextId,
302
locator: { type: 'css', value: 'button' },
303
})
304
305
assert.ok(Array.isArray(result.nodes))
306
assert.ok(result.nodes.length > 0)
307
})
308
309
it('can locate nodes with maxNodeCount', async function () {
310
const contextId = await driver.getWindowHandle()
311
await browsingContext.navigate({ context: contextId, url: Pages.logEntryAdded, wait: 'complete' })
312
313
const result = await browsingContext.locateNodes({
314
context: contextId,
315
locator: { type: 'css', value: 'button' },
316
maxNodeCount: 1,
317
})
318
319
assert.ok(Array.isArray(result.nodes))
320
assert.strictEqual(result.nodes.length, 1)
321
})
322
})
323
},
324
{ browsers: [Browser.CHROME, Browser.FIREFOX] },
325
)
326
327