-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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]>
- Loading branch information
1 parent
940b183
commit ef69f1b
Showing
5 changed files
with
216 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
javascript/node/selenium-webdriver/bidi/browsingContextInspector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
javascript/node/selenium-webdriver/bidi/browsingContextTypes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters