Skip to content

Commit ef69f1b

Browse files
[js][bidi] Add browsing context events support (#11905)
* [js][bidi] Added browsing context events and test * [js][bidi] code reduction and function name change --------- Co-authored-by: Sri Harsha <[email protected]>
1 parent 940b183 commit ef69f1b

File tree

5 files changed

+216
-25
lines changed

5 files changed

+216
-25
lines changed

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
const { BrowsingContextInfo } = require('./browsingContextTypes')
1819
class BrowsingContext {
1920
constructor(driver) {
2021
this._driver = driver
@@ -145,31 +146,6 @@ class NavigateResult {
145146
}
146147
}
147148

148-
class BrowsingContextInfo {
149-
constructor(id, url, children, parentBrowsingContext) {
150-
this._id = id
151-
this._url = url
152-
this._children = children
153-
this._parentBrowsingContext = parentBrowsingContext
154-
}
155-
156-
get id() {
157-
return this._id
158-
}
159-
160-
get url() {
161-
return this._url
162-
}
163-
164-
get children() {
165-
return this._children
166-
}
167-
168-
get parentBrowsingContext() {
169-
return this._parentBrowsingContext
170-
}
171-
}
172-
173149
/**
174150
* initiate browsing context instance and return
175151
* @param driver
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 {
19+
BrowsingContextInfo,
20+
NavigationInfo,
21+
} = require('./browsingContextTypes')
22+
23+
class BrowsingContextInspector {
24+
constructor(driver, browsingContextIds) {
25+
this._driver = driver
26+
this._browsingContextIds = browsingContextIds
27+
}
28+
29+
async init() {
30+
this.bidi = await this._driver.getBidi()
31+
}
32+
33+
async onBrowsingContextCreated(callback) {
34+
await this.subscribeAndHandleEvent(
35+
'browsingContext.contextCreated',
36+
callback
37+
)
38+
}
39+
40+
async onDomContentLoaded(callback) {
41+
await this.subscribeAndHandleEvent(
42+
'browsingContext.domContentLoaded',
43+
callback
44+
)
45+
}
46+
47+
async onBrowsingContextLoaded(callback) {
48+
await this.subscribeAndHandleEvent('browsingContext.load', callback)
49+
}
50+
51+
async subscribeAndHandleEvent(eventType, callback) {
52+
if (this._browsingContextIds != null) {
53+
await this.bidi.subscribe(eventType, this._browsingContextIds)
54+
} else {
55+
await this.bidi.subscribe(eventType)
56+
}
57+
this._on(callback)
58+
}
59+
60+
async _on(callback) {
61+
this.ws = await this.bidi.socket
62+
this.ws.on('message', (event) => {
63+
const { params } = JSON.parse(Buffer.from(event.toString()))
64+
if (params) {
65+
let response = null
66+
if ('navigation' in params) {
67+
response = new NavigationInfo(
68+
params.context,
69+
params.navigation,
70+
params.timestamp,
71+
params.url
72+
)
73+
} else if ('accepted' in params) {
74+
/* Needs to be updated when browsers implement other events */
75+
} else {
76+
response = new BrowsingContextInfo(
77+
params.context,
78+
params.url,
79+
params.children,
80+
params.parent
81+
)
82+
}
83+
callback(response)
84+
}
85+
})
86+
}
87+
}
88+
89+
async function getBrowsingContextInstance(driver, browsingContextIds = null) {
90+
let instance = new BrowsingContextInspector(driver, browsingContextIds)
91+
await instance.init()
92+
return instance
93+
}
94+
95+
module.exports = getBrowsingContextInstance
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 BrowsingContextInfo {
19+
constructor(id, url, children, parentBrowsingContext) {
20+
this._id = id
21+
this._url = url
22+
this._children = children
23+
this._parentBrowsingContext = parentBrowsingContext
24+
}
25+
26+
get id() {
27+
return this._id
28+
}
29+
30+
get url() {
31+
return this._url
32+
}
33+
34+
get children() {
35+
return this._children
36+
}
37+
38+
get parentBrowsingContext() {
39+
return this._parentBrowsingContext
40+
}
41+
}
42+
43+
class NavigationInfo {
44+
constructor(browsingContextId, navigationId, timestamp, url) {
45+
this.browsingContextId = browsingContextId
46+
this.navigationId = navigationId
47+
this.timestamp = timestamp
48+
this.url = url
49+
}
50+
}
51+
52+
module.exports = { BrowsingContextInfo, NavigationInfo }

javascript/node/selenium-webdriver/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const webdriver = require('./lib/webdriver')
4141
const select = require('./lib/select')
4242
const LogInspector = require('./bidi/logInspector')
4343
const BrowsingContext = require('./bidi/browsingContext')
44+
const BrowsingConextInspector = require('./bidi/browsingContextInspector')
4445
const ScriptManager = require('./bidi/scriptManager')
4546

4647
const Browser = capabilities.Browser
@@ -800,4 +801,5 @@ exports.until = until
800801
exports.Select = select.Select
801802
exports.LogInspector = LogInspector
802803
exports.BrowsingContext = BrowsingContext
804+
exports.BrowsingConextInspector = BrowsingConextInspector
803805
exports.ScriptManager = ScriptManager

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const { Browser } = require('../../')
2424
const { Pages, suite } = require('../../lib/test')
2525
const logInspector = require('../../bidi/logInspector')
2626
const BrowsingContext = require('../../bidi/browsingContext')
27+
const BrowsingConextInspector = require('../../bidi/browsingContextInspector')
2728
const filterBy = require('../../bidi/filterBy')
2829
const until = require('../../lib/until')
2930

@@ -398,6 +399,71 @@ suite(
398399
})
399400
})
400401

402+
describe('Browsing Context Inspector', function () {
403+
it('can listen to window browsing context created event', async function () {
404+
let contextInfo = null
405+
const browsingConextInspector = await BrowsingConextInspector(driver)
406+
await browsingConextInspector.onBrowsingContextCreated((entry) => {
407+
contextInfo = entry
408+
})
409+
410+
await driver.switchTo().newWindow('window')
411+
const windowHandle = await driver.getWindowHandle()
412+
assert.equal(contextInfo.id, windowHandle)
413+
assert.equal(contextInfo.url, 'about:blank')
414+
assert.equal(contextInfo.children, null)
415+
assert.equal(contextInfo.parentBrowsingContext, null)
416+
})
417+
418+
it('can listen to tab browsing context created event', async function () {
419+
let contextInfo = null
420+
const browsingConextInspector = await BrowsingConextInspector(driver)
421+
await browsingConextInspector.onBrowsingContextCreated((entry) => {
422+
contextInfo = entry
423+
})
424+
425+
await driver.switchTo().newWindow('tab')
426+
const tabHandle = await driver.getWindowHandle()
427+
428+
assert.equal(contextInfo.id, tabHandle)
429+
assert.equal(contextInfo.url, 'about:blank')
430+
assert.equal(contextInfo.children, null)
431+
assert.equal(contextInfo.parentBrowsingContext, null)
432+
})
433+
434+
it('can listen to dom content loaded event', async function () {
435+
const browsingConextInspector = await BrowsingConextInspector(driver)
436+
let navigationInfo = null
437+
await browsingConextInspector.onDomContentLoaded((entry) => {
438+
navigationInfo = entry
439+
})
440+
441+
const browsingContext = await BrowsingContext(driver, {
442+
browsingContextId: await driver.getWindowHandle(),
443+
})
444+
await browsingContext.navigate(Pages.logEntryAdded, 'complete')
445+
446+
assert.equal(navigationInfo.browsingContextId, browsingContext.id)
447+
assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))
448+
})
449+
450+
it('can listen to browsing context loaded event', async function () {
451+
let navigationInfo = null
452+
const browsingConextInspector = await BrowsingConextInspector(driver)
453+
454+
await browsingConextInspector.onBrowsingContextLoaded((entry) => {
455+
navigationInfo = entry
456+
})
457+
const browsingContext = await BrowsingContext(driver, {
458+
browsingContextId: await driver.getWindowHandle(),
459+
})
460+
await browsingContext.navigate(Pages.logEntryAdded, 'complete')
461+
462+
assert.equal(navigationInfo.browsingContextId, browsingContext.id)
463+
assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))
464+
})
465+
})
466+
401467
describe('Local Value', function () {
402468
it('can call function with undefined argument', async function () {
403469
const id = await driver.getWindowHandle()

0 commit comments

Comments
 (0)