Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/browser_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 } = require('../../../lib/test')
22
const { Browser } = require('selenium-webdriver')
23
24
// Generated Browser domain class — produced by generate_bidi.mjs from the
25
// WebDriver BiDi CDDL spec. This test exercises the generated API to verify
26
// that the auto-generated code works correctly against a real browser.
27
const { Browser: GeneratedBrowser } = require('selenium-webdriver/bidi/generated/browser')
28
29
suite(
30
function (env) {
31
let driver
32
33
beforeEach(async function () {
34
driver = await env.builder().build()
35
})
36
37
afterEach(function () {
38
return driver.quit()
39
})
40
41
describe('Generated BiDi Browser domain', function () {
42
// The generated Browser class uses a static factory method Browser.create(driver)
43
// instead of the hand-written getBrowserInstance(driver) helper function.
44
45
it('can create a user context', async function () {
46
const browser = await GeneratedBrowser.create(driver)
47
48
// createUserContext({}) returns BrowserCreateUserContextResult = { userContext: string }
49
// All params fields are optional per the spec so an empty object is valid.
50
const result = await browser.createUserContext({})
51
52
assert.notEqual(result.userContext, null)
53
assert.strictEqual(typeof result.userContext, 'string')
54
55
await browser.removeUserContext({ userContext: result.userContext })
56
})
57
58
it('can get user contexts', async function () {
59
const browser = await GeneratedBrowser.create(driver)
60
61
const result1 = await browser.createUserContext({})
62
const result2 = await browser.createUserContext({})
63
64
// getUserContexts() returns BrowserGetUserContextsResult = { userContexts: BrowserUserContextInfo[] }
65
// Each BrowserUserContextInfo has a { userContext: string } field.
66
const { userContexts } = await browser.getUserContexts()
67
68
assert.strictEqual(userContexts.length >= 2, true)
69
70
const ids = userContexts.map((ctx) => ctx.userContext)
71
assert.ok(ids.includes(result1.userContext))
72
assert.ok(ids.includes(result2.userContext))
73
74
await browser.removeUserContext({ userContext: result1.userContext })
75
await browser.removeUserContext({ userContext: result2.userContext })
76
})
77
78
it('can remove a user context', async function () {
79
const browser = await GeneratedBrowser.create(driver)
80
81
const result1 = await browser.createUserContext({})
82
const result2 = await browser.createUserContext({})
83
84
const before = await browser.getUserContexts()
85
const beforeIds = before.userContexts.map((ctx) => ctx.userContext)
86
assert.ok(beforeIds.includes(result1.userContext))
87
assert.ok(beforeIds.includes(result2.userContext))
88
89
// removeUserContext takes { userContext: string } — an object, not a bare string
90
await browser.removeUserContext({ userContext: result2.userContext })
91
92
const after = await browser.getUserContexts()
93
const afterIds = after.userContexts.map((ctx) => ctx.userContext)
94
assert.ok(afterIds.includes(result1.userContext))
95
assert.ok(!afterIds.includes(result2.userContext))
96
97
await browser.removeUserContext({ userContext: result1.userContext })
98
})
99
100
it('can get client windows', async function () {
101
const browser = await GeneratedBrowser.create(driver)
102
103
// getClientWindows() returns BrowserGetClientWindowsResult = { clientWindows: BrowserClientWindowInfo[] }
104
const { clientWindows } = await browser.getClientWindows()
105
106
assert.ok(Array.isArray(clientWindows))
107
assert.ok(clientWindows.length > 0)
108
109
const window = clientWindows[0]
110
assert.strictEqual(typeof window.clientWindow, 'string')
111
assert.ok(['fullscreen', 'maximized', 'minimized', 'normal'].includes(window.state))
112
assert.ok(Number.isInteger(window.width) && window.width > 0)
113
assert.ok(Number.isInteger(window.height) && window.height > 0)
114
assert.ok(Number.isInteger(window.x))
115
assert.ok(Number.isInteger(window.y))
116
assert.strictEqual(typeof window.active, 'boolean')
117
})
118
})
119
},
120
{ browsers: [Browser.CHROME, Browser.FIREFOX] },
121
)
122
123