Skip to content

Commit fbfc664

Browse files
committed
[bidi][js] Add create parameters and update the init method for it. Remove referenceContext param.
1 parent 8b5f33b commit fbfc664

3 files changed

Lines changed: 103 additions & 14 deletions

File tree

javascript/node/selenium-webdriver/bidi/browsingContext.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const { BrowsingContextInfo } = require('./browsingContextTypes')
2020
const { SerializationOptions, ReferenceValue, RemoteValue } = require('./protocolValue')
2121
const { WebElement } = require('../lib/webdriver')
2222
const { CaptureScreenshotParameters } = require('./captureScreenshotParameters')
23+
const { CreateContextParameters } = require('./createContextParameters')
2324

2425
class Locator {
2526
static Type = Object.freeze({
@@ -79,34 +80,53 @@ class BrowsingContext {
7980
return this._id
8081
}
8182

82-
async init({ browsingContextId, type, referenceContext }) {
83+
async init({ browsingContextId = undefined, type = undefined, createParameters = undefined }) {
8384
if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {
8485
throw Error('WebDriver instance must support BiDi protocol')
8586
}
8687

88+
if (browsingContextId === undefined && type === undefined && createParameters === undefined) {
89+
throw Error('Either BrowsingContextId or Type or CreateParameters must be provided')
90+
}
91+
92+
if (type === undefined && createParameters !== undefined) {
93+
throw Error('Type must be provided with CreateParameters')
94+
}
95+
8796
if (type !== undefined && !['window', 'tab'].includes(type)) {
8897
throw Error(`Valid types are 'window' & 'tab'. Received: ${type}`)
8998
}
9099

91100
this.bidi = await this._driver.getBidi()
92101
this._id =
93102
browsingContextId === undefined
94-
? (await this.create(type, referenceContext))['result']['context']
103+
? (await this.create(type, createParameters))['result']['context']
95104
: browsingContextId
96105
}
97106

98107
/**
99-
* Creates a browsing context for the given type and referenceContext
108+
* Creates a browsing context for the given type with the given parameters
100109
*/
101-
async create(type, referenceContext) {
110+
async create(type, createParameters = undefined) {
111+
if (createParameters !== undefined && (!createParameters) instanceof CreateContextParameters) {
112+
throw Error(`Pass in the instance of CreateContextParameters. Received: ${createParameters}`)
113+
}
114+
115+
let parameters = new Map()
116+
parameters.set('type', type)
117+
118+
if (createParameters !== undefined) {
119+
createParameters.asMap().forEach((value, key) => {
120+
parameters.set(key, value)
121+
})
122+
}
123+
102124
const params = {
103125
method: 'browsingContext.create',
104-
params: {
105-
type: type,
106-
referenceContext: referenceContext,
107-
},
126+
params: Object.fromEntries(parameters),
108127
}
109-
return await this.bidi.send(params)
128+
const res = await this.bidi.send(params)
129+
return res
110130
}
111131

112132
/**
@@ -484,12 +504,15 @@ class PrintResult {
484504
* @param driver
485505
* @param browsingContextId The browsing context of current window/tab
486506
* @param type "window" or "tab"
487-
* @param referenceContext To get a browsing context for this reference if passed
507+
* @param createParameters The parameters for creating a new browsing context
488508
* @returns {Promise<BrowsingContext>}
489509
*/
490-
async function getBrowsingContextInstance(driver, { browsingContextId, type, referenceContext }) {
510+
async function getBrowsingContextInstance(
511+
driver,
512+
{ browsingContextId = undefined, type = undefined, createParameters = undefined },
513+
) {
491514
let instance = new BrowsingContext(driver)
492-
await instance.init({ browsingContextId, type, referenceContext })
515+
await instance.init({ browsingContextId, type, createParameters })
493516
return instance
494517
}
495518

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
class CreateContextParameters {
19+
#map = new Map()
20+
21+
referenceContext(id) {
22+
if (typeof id !== 'string') {
23+
throw new Error(`ReferenceContext must be string. Received:'${id}'`)
24+
}
25+
this.#map.set('referenceContext', id)
26+
return this
27+
}
28+
29+
background(background) {
30+
if (typeof background !== 'boolean') {
31+
throw new Error(`Background must be boolean. Received:'${background}'`)
32+
}
33+
this.#map.set('background', background)
34+
return this
35+
}
36+
37+
userContext(userContext) {
38+
if (typeof userContext !== 'string') {
39+
throw new Error(`UserContext must be string. Received:'${userContext}'`)
40+
}
41+
this.#map.set('userContext', userContext)
42+
return this
43+
}
44+
45+
asMap() {
46+
return this.#map
47+
}
48+
}
49+
50+
module.exports = { CreateContextParameters }

javascript/node/selenium-webdriver/test/bidi/browsingcontext_test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const BrowsingContext = require('../../bidi/browsingContext')
2525
const until = require('../../lib/until')
2626
const { Origin, CaptureScreenshotParameters } = require('../../bidi/captureScreenshotParameters')
2727
const { BoxClipRectangle, ElementClipRectangle } = require('../../bidi/clipRectangle')
28+
const { CreateContextParameters } = require('../../bidi/createContextParameters')
29+
const BrowserBiDi = require('../../bidi/browser')
2830

2931
suite(
3032
function (env) {
@@ -62,11 +64,25 @@ suite(
6264
it('can create a window with a reference context', async function () {
6365
const browsingContext = await BrowsingContext(driver, {
6466
type: 'window',
65-
referenceContext: await driver.getWindowHandle(),
67+
createParameters: new CreateContextParameters().referenceContext(await driver.getWindowHandle()),
6668
})
6769
assert.notEqual(browsingContext.id, null)
6870
})
6971

72+
it('can create a tab with all parameters', async function () {
73+
const browser = await BrowserBiDi(driver)
74+
const userContext = await browser.createUserContext()
75+
const browsingContext = await BrowsingContext(driver, {
76+
type: 'window',
77+
createParameters: new CreateContextParameters()
78+
.referenceContext(await driver.getWindowHandle())
79+
.background(true)
80+
.userContext(userContext),
81+
})
82+
assert.notEqual(browsingContext.id, null)
83+
assert.notEqual(browsingContext.id, await driver.getWindowHandle())
84+
})
85+
7086
it('can create a tab', async function () {
7187
const browsingContext = await BrowsingContext(driver, {
7288
type: 'tab',
@@ -77,7 +93,7 @@ suite(
7793
it('can create a tab with a reference context', async function () {
7894
const browsingContext = await BrowsingContext(driver, {
7995
type: 'tab',
80-
referenceContext: await driver.getWindowHandle(),
96+
referenceContext: new CreateContextParameters().referenceContext(await driver.getWindowHandle()),
8197
})
8298
assert.notEqual(browsingContext.id, null)
8399
})

0 commit comments

Comments
 (0)