Skip to content

Commit

Permalink
[js][bidi] Add browsing context events support (#11905)
Browse files Browse the repository at this point in the history
* [js][bidi] Added browsing context events and test

* [js][bidi] code reduction and function name change

---------

Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com>
  • Loading branch information
TamsilAmani and harsha509 authored Apr 26, 2023
1 parent 940b183 commit ef69f1b
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 25 deletions.
26 changes: 1 addition & 25 deletions javascript/node/selenium-webdriver/bidi/browsingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

const { BrowsingContextInfo } = require('./browsingContextTypes')
class BrowsingContext {
constructor(driver) {
this._driver = driver
Expand Down Expand Up @@ -145,31 +146,6 @@ class NavigateResult {
}
}

class BrowsingContextInfo {
constructor(id, url, children, parentBrowsingContext) {
this._id = id
this._url = url
this._children = children
this._parentBrowsingContext = parentBrowsingContext
}

get id() {
return this._id
}

get url() {
return this._url
}

get children() {
return this._children
}

get parentBrowsingContext() {
return this._parentBrowsingContext
}
}

/**
* initiate browsing context instance and return
* @param driver
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

const {
BrowsingContextInfo,
NavigationInfo,
} = require('./browsingContextTypes')

class BrowsingContextInspector {
constructor(driver, browsingContextIds) {
this._driver = driver
this._browsingContextIds = browsingContextIds
}

async init() {
this.bidi = await this._driver.getBidi()
}

async onBrowsingContextCreated(callback) {
await this.subscribeAndHandleEvent(
'browsingContext.contextCreated',
callback
)
}

async onDomContentLoaded(callback) {
await this.subscribeAndHandleEvent(
'browsingContext.domContentLoaded',
callback
)
}

async onBrowsingContextLoaded(callback) {
await this.subscribeAndHandleEvent('browsingContext.load', callback)
}

async subscribeAndHandleEvent(eventType, callback) {
if (this._browsingContextIds != null) {
await this.bidi.subscribe(eventType, this._browsingContextIds)
} else {
await this.bidi.subscribe(eventType)
}
this._on(callback)
}

async _on(callback) {
this.ws = await this.bidi.socket
this.ws.on('message', (event) => {
const { params } = JSON.parse(Buffer.from(event.toString()))
if (params) {
let response = null
if ('navigation' in params) {
response = new NavigationInfo(
params.context,
params.navigation,
params.timestamp,
params.url
)
} else if ('accepted' in params) {
/* Needs to be updated when browsers implement other events */
} else {
response = new BrowsingContextInfo(
params.context,
params.url,
params.children,
params.parent
)
}
callback(response)
}
})
}
}

async function getBrowsingContextInstance(driver, browsingContextIds = null) {
let instance = new BrowsingContextInspector(driver, browsingContextIds)
await instance.init()
return instance
}

module.exports = getBrowsingContextInstance
52 changes: 52 additions & 0 deletions javascript/node/selenium-webdriver/bidi/browsingContextTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

class BrowsingContextInfo {
constructor(id, url, children, parentBrowsingContext) {
this._id = id
this._url = url
this._children = children
this._parentBrowsingContext = parentBrowsingContext
}

get id() {
return this._id
}

get url() {
return this._url
}

get children() {
return this._children
}

get parentBrowsingContext() {
return this._parentBrowsingContext
}
}

class NavigationInfo {
constructor(browsingContextId, navigationId, timestamp, url) {
this.browsingContextId = browsingContextId
this.navigationId = navigationId
this.timestamp = timestamp
this.url = url
}
}

module.exports = { BrowsingContextInfo, NavigationInfo }
2 changes: 2 additions & 0 deletions javascript/node/selenium-webdriver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const webdriver = require('./lib/webdriver')
const select = require('./lib/select')
const LogInspector = require('./bidi/logInspector')
const BrowsingContext = require('./bidi/browsingContext')
const BrowsingConextInspector = require('./bidi/browsingContextInspector')
const ScriptManager = require('./bidi/scriptManager')

const Browser = capabilities.Browser
Expand Down Expand Up @@ -800,4 +801,5 @@ exports.until = until
exports.Select = select.Select
exports.LogInspector = LogInspector
exports.BrowsingContext = BrowsingContext
exports.BrowsingConextInspector = BrowsingConextInspector
exports.ScriptManager = ScriptManager
66 changes: 66 additions & 0 deletions javascript/node/selenium-webdriver/test/bidi/bidi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const { Browser } = require('../../')
const { Pages, suite } = require('../../lib/test')
const logInspector = require('../../bidi/logInspector')
const BrowsingContext = require('../../bidi/browsingContext')
const BrowsingConextInspector = require('../../bidi/browsingContextInspector')
const filterBy = require('../../bidi/filterBy')
const until = require('../../lib/until')

Expand Down Expand Up @@ -398,6 +399,71 @@ suite(
})
})

describe('Browsing Context Inspector', function () {
it('can listen to window browsing context created event', async function () {
let contextInfo = null
const browsingConextInspector = await BrowsingConextInspector(driver)
await browsingConextInspector.onBrowsingContextCreated((entry) => {
contextInfo = entry
})

await driver.switchTo().newWindow('window')
const windowHandle = await driver.getWindowHandle()
assert.equal(contextInfo.id, windowHandle)
assert.equal(contextInfo.url, 'about:blank')
assert.equal(contextInfo.children, null)
assert.equal(contextInfo.parentBrowsingContext, null)
})

it('can listen to tab browsing context created event', async function () {
let contextInfo = null
const browsingConextInspector = await BrowsingConextInspector(driver)
await browsingConextInspector.onBrowsingContextCreated((entry) => {
contextInfo = entry
})

await driver.switchTo().newWindow('tab')
const tabHandle = await driver.getWindowHandle()

assert.equal(contextInfo.id, tabHandle)
assert.equal(contextInfo.url, 'about:blank')
assert.equal(contextInfo.children, null)
assert.equal(contextInfo.parentBrowsingContext, null)
})

it('can listen to dom content loaded event', async function () {
const browsingConextInspector = await BrowsingConextInspector(driver)
let navigationInfo = null
await browsingConextInspector.onDomContentLoaded((entry) => {
navigationInfo = entry
})

const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
})
await browsingContext.navigate(Pages.logEntryAdded, 'complete')

assert.equal(navigationInfo.browsingContextId, browsingContext.id)
assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))
})

it('can listen to browsing context loaded event', async function () {
let navigationInfo = null
const browsingConextInspector = await BrowsingConextInspector(driver)

await browsingConextInspector.onBrowsingContextLoaded((entry) => {
navigationInfo = entry
})
const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
})
await browsingContext.navigate(Pages.logEntryAdded, 'complete')

assert.equal(navigationInfo.browsingContextId, browsingContext.id)
assert(navigationInfo.url.includes('/bidi/logEntryAdded.html'))
})
})

describe('Local Value', function () {
it('can call function with undefined argument', async function () {
const id = await driver.getWindowHandle()
Expand Down

0 comments on commit ef69f1b

Please sign in to comment.