Skip to content

Commit 911b312

Browse files
authored
[js] Add JS comments for BiDi related files (#13763)
1 parent 37565af commit 911b312

28 files changed

+1565
-9
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class AddInterceptParameters {
2929
}
3030
}
3131

32+
/**
33+
* Adds a URL pattern to intercept.
34+
*
35+
* @param {UrlPattern} pattern - The URL pattern to add.
36+
* @returns {AddInterceptParameters} - Returns the current instance of the class AddInterceptParameters for chaining.
37+
* @throws {Error} - Throws an error if the pattern is not an instance of UrlPattern.
38+
*/
3239
urlPattern(pattern) {
3340
if (!(pattern instanceof UrlPattern)) {
3441
throw new Error(`Pattern must be an instance of UrlPattern. Received: '${pattern})'`)
@@ -37,6 +44,13 @@ class AddInterceptParameters {
3744
return this
3845
}
3946

47+
/**
48+
* Adds array of URL patterns to intercept.
49+
*
50+
* @param {UrlPattern[]} patterns - An array of UrlPattern instances representing the URL patterns to intercept.
51+
* @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining.
52+
* @throws {Error} - Throws an error if the pattern is not an instance of UrlPattern.
53+
*/
4054
urlPatterns(patterns) {
4155
patterns.forEach((pattern) => {
4256
if (!(pattern instanceof UrlPattern)) {
@@ -47,6 +61,13 @@ class AddInterceptParameters {
4761
return this
4862
}
4963

64+
/**
65+
* Adds string URL to intercept.
66+
*
67+
* @param {string} pattern - The URL pattern to be added.
68+
* @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining..
69+
* @throws {Error} - If the pattern is not an instance of String.
70+
*/
5071
urlStringPattern(pattern) {
5172
if (!(pattern instanceof String)) {
5273
throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)
@@ -56,6 +77,11 @@ class AddInterceptParameters {
5677
return this
5778
}
5879

80+
/**
81+
* Adds array of string URLs to intercept.
82+
* @param {string[]} patterns - An array of URL string patterns.
83+
* @returns {this} - Returns the instance of AddInterceptParameters for chaining.
84+
*/
5985
urlStringPatterns(patterns) {
6086
patterns.forEach((pattern) => {
6187
if (!(pattern instanceof String)) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
/**
19+
* Represents the commands and events under Browser Module.
20+
* Described in https://w3c.github.io/webdriver-bidi/#module-browser
21+
*/
1822
class Browser {
1923
constructor(driver) {
2024
this._driver = driver
@@ -28,6 +32,10 @@ class Browser {
2832
this.bidi = await this._driver.getBidi()
2933
}
3034

35+
/**
36+
* Creates a new user context.
37+
* @returns {Promise<string>} A promise that resolves to the user context id.
38+
*/
3139
async createUserContext() {
3240
const command = {
3341
method: 'browser.createUserContext',
@@ -39,6 +47,10 @@ class Browser {
3947
return response.result.userContext
4048
}
4149

50+
/**
51+
* Gets the list of all user contexts.
52+
* @returns {Promise<string[]>} A promise that resolves to an array of user context ids.
53+
*/
4254
async getUserContexts() {
4355
const command = {
4456
method: 'browser.getUserContexts',
@@ -58,6 +70,11 @@ class Browser {
5870
return userContexts
5971
}
6072

73+
/**
74+
* Removes a user context.
75+
* @param {string} userContext The user context id to be removed.
76+
* @returns {Promise<void>}
77+
*/
6178
async removeUserContext(userContext) {
6279
const command = {
6380
method: 'browser.removeUserContext',

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

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const { SerializationOptions, ReferenceValue, RemoteValue } = require('./protoco
2121
const { WebElement } = require('../lib/webdriver')
2222
const { CaptureScreenshotParameters } = require('./captureScreenshotParameters')
2323

24+
/**
25+
* Represents the locator to locate nodes in the browsing context.
26+
* Described in https://w3c.github.io/webdriver-bidi/#type-browsingContext-Locator.
27+
*/
2428
class Locator {
2529
static Type = Object.freeze({
2630
CSS: 'css',
@@ -42,14 +46,35 @@ class Locator {
4246
this.#maxDepth = maxDepth
4347
}
4448

49+
/**
50+
* Creates a new Locator object with CSS selector type.
51+
*
52+
* @param {string} value - The CSS selector value.
53+
* @returns {Locator} A new Locator object with CSS selector type.
54+
*/
4555
static css(value) {
4656
return new Locator(Locator.Type.CSS, value)
4757
}
4858

59+
/**
60+
* Creates a new Locator object with the given XPath value.
61+
*
62+
* @param {string} value - The XPath value.
63+
* @returns {Locator} A new Locator object.
64+
*/
4965
static xpath(value) {
5066
return new Locator(Locator.Type.XPATH, value)
5167
}
5268

69+
/**
70+
* Creates a new Locator object with the specified inner text value.
71+
*
72+
* @param {string} value - The inner text value to locate.
73+
* @param {boolean|undefined} [ignoreCase] - Whether to ignore the case when matching the inner text value.
74+
* @param {string|undefined} [matchType] - The type of matching to perform (full or partial).
75+
* @param {number|undefined} [maxDepth] - The maximum depth to search for the inner text value.
76+
* @returns {Locator} A new Locator object with the specified inner text value.
77+
*/
5378
static innerText(value, ignoreCase = undefined, matchType = undefined, maxDepth = undefined) {
5479
return new Locator(Locator.Type.INNER_TEXT, value, ignoreCase, matchType, maxDepth)
5580
}
@@ -67,6 +92,12 @@ class Locator {
6792
}
6893
}
6994

95+
/**
96+
* Represents the contains under BrowsingContext module commands.
97+
* Described in https://w3c.github.io/webdriver-bidi/#module-browsingContext
98+
* Each browsing context command requires a browsing context id.
99+
* Hence, this class represent browsing context lifecycle.
100+
*/
70101
class BrowsingContext {
71102
constructor(driver) {
72103
this._driver = driver
@@ -225,6 +256,13 @@ class BrowsingContext {
225256
return new PrintResult(response.result.data)
226257
}
227258

259+
/**
260+
* Captures a screenshot of the browsing context.
261+
*
262+
* @param {CaptureScreenshotParameters|undefined} [captureScreenshotParameters] - Optional parameters for capturing the screenshot.
263+
* @returns {Promise<string>} - A promise that resolves to the base64-encoded string representation of the captured screenshot.
264+
* @throws {InvalidArgumentError} - If the provided captureScreenshotParameters is not an instance of CaptureScreenshotParameters.
265+
*/
228266
async captureScreenshot(captureScreenshotParameters = undefined) {
229267
if (
230268
captureScreenshotParameters !== undefined &&
@@ -273,6 +311,12 @@ class BrowsingContext {
273311
return response['result']['data']
274312
}
275313

314+
/**
315+
* Captures a screenshot of a specific element within the browsing context.
316+
* @param {string} sharedId - The shared ID of the element to capture.
317+
* @param {string} [handle] - The handle of the element to capture (optional).
318+
* @returns {Promise<string>} A promise that resolves to the base64-encoded screenshot data.
319+
*/
276320
async captureElementScreenshot(sharedId, handle = undefined) {
277321
let params = {
278322
method: 'browsingContext.captureScreenshot',
@@ -307,6 +351,11 @@ class BrowsingContext {
307351
}
308352
}
309353

354+
/**
355+
* Activates and focuses the top-level browsing context.
356+
* @returns {Promise<void>} A promise that resolves when the browsing context is activated.
357+
* @throws {Error} If there is an error while activating the browsing context.
358+
*/
310359
async activate() {
311360
const params = {
312361
method: 'browsingContext.activate',
@@ -321,6 +370,13 @@ class BrowsingContext {
321370
}
322371
}
323372

373+
/**
374+
* Handles a user prompt in the browsing context.
375+
*
376+
* @param {boolean} [accept] - Optional. Indicates whether to accept or dismiss the prompt.
377+
* @param {string} [userText] - Optional. The text to enter.
378+
* @throws {Error} If an error occurs while handling the user prompt.
379+
*/
324380
async handleUserPrompt(accept = undefined, userText = undefined) {
325381
const params = {
326382
method: 'browsingContext.handleUserPrompt',
@@ -337,6 +393,15 @@ class BrowsingContext {
337393
}
338394
}
339395

396+
/**
397+
* Reloads the current browsing context.
398+
*
399+
* @param {boolean} [ignoreCache] - Whether to ignore the cache when reloading.
400+
* @param {string} [readinessState] - The readiness state to wait for before returning.
401+
* Valid readiness states are 'none', 'interactive', and 'complete'.
402+
* @returns {Promise<NavigateResult>} - A promise that resolves to the result of the reload operation.
403+
* @throws {Error} - If an invalid readiness state is provided.
404+
*/
340405
async reload(ignoreCache = undefined, readinessState = undefined) {
341406
if (readinessState !== undefined && !['none', 'interactive', 'complete'].includes(readinessState)) {
342407
throw Error(`Valid readiness states are 'none', 'interactive' & 'complete'. Received: ${readinessState}`)
@@ -355,6 +420,13 @@ class BrowsingContext {
355420
return new NavigateResult(navigateResult['url'], navigateResult['navigation'])
356421
}
357422

423+
/**
424+
* Sets the viewport size and device pixel ratio for the browsing context.
425+
* @param {number} width - The width of the viewport.
426+
* @param {number} height - The height of the viewport.
427+
* @param {number} [devicePixelRatio] - The device pixel ratio (optional)
428+
* @throws {Error} If an error occurs while setting the viewport.
429+
*/
358430
async setViewport(width, height, devicePixelRatio = undefined) {
359431
const params = {
360432
method: 'browsingContext.setViewport',
@@ -370,6 +442,12 @@ class BrowsingContext {
370442
}
371443
}
372444

445+
/**
446+
* Traverses the browsing context history by a given delta.
447+
*
448+
* @param {number} delta - The delta value to traverse the history. A positive value moves forward, while a negative value moves backward.
449+
* @returns {Promise<void>} - A promise that resolves when the history traversal is complete.
450+
*/
373451
async traverseHistory(delta) {
374452
const params = {
375453
method: 'browsingContext.traverseHistory',
@@ -381,14 +459,38 @@ class BrowsingContext {
381459
await this.bidi.send(params)
382460
}
383461

462+
/**
463+
* Moves the browsing context forward by one step in the history.
464+
* @returns {Promise<void>} A promise that resolves when the browsing context has moved forward.
465+
*/
384466
async forward() {
385467
await this.traverseHistory(1)
386468
}
387469

470+
/**
471+
* Navigates the browsing context to the previous page in the history.
472+
* @returns {Promise<void>} A promise that resolves when the navigation is complete.
473+
*/
388474
async back() {
389475
await this.traverseHistory(-1)
390476
}
391477

478+
/**
479+
* Locates nodes in the browsing context.
480+
*
481+
* @param {Locator} locator - The locator object used to locate the nodes.
482+
* @param {number} [maxNodeCount] - The maximum number of nodes to locate (optional).
483+
* @param {string} [ownership] - The ownership type of the nodes (optional).
484+
* @param {string} [sandbox] - The sandbox name for locating nodes (optional).
485+
* @param {SerializationOptions} [serializationOptions] - The serialization options for locating nodes (optional).
486+
* @param {ReferenceValue[]} [startNodes] - The array of start nodes for locating nodes (optional).
487+
* @returns {Promise<RemoteValue[]>} - A promise that resolves to the arrays of located nodes.
488+
* @throws {Error} - If the locator is not an instance of Locator.
489+
* @throws {Error} - If the serializationOptions is provided but not an instance of SerializationOptions.
490+
* @throws {Error} - If the ownership is provided but not 'root' or 'none'.
491+
* @throws {Error} - If the startNodes is provided but not an array of ReferenceValue objects.
492+
* @throws {Error} - If any of the startNodes is not an instance of ReferenceValue.
493+
*/
392494
async locateNodes(
393495
locator,
394496
maxNodeCount = undefined,
@@ -448,6 +550,16 @@ class BrowsingContext {
448550
return remoteValues
449551
}
450552

553+
/**
554+
* Locates a single node in the browsing context.
555+
*
556+
* @param {Locator} locator - The locator used to find the node.
557+
* @param {string} [ownership] - The ownership of the node (optional).
558+
* @param {string} [sandbox] - The sandbox of the node (optional).
559+
* @param {SerializationOptions} [serializationOptions] - The serialization options for the node (optional).
560+
* @param {Array} [startNodes] - The starting nodes for the search (optional).
561+
* @returns {Promise<RemoteValue>} - A promise that resolves to the located node.
562+
*/
451563
async locateNode(
452564
locator,
453565
ownership = undefined,
@@ -475,26 +587,44 @@ class BrowsingContext {
475587
}
476588
}
477589

590+
/**
591+
* Represents the result of a navigation operation.
592+
*/
478593
class NavigateResult {
479594
constructor(url, navigationId) {
480595
this._url = url
481596
this._navigationId = navigationId
482597
}
483598

599+
/**
600+
* Gets the URL of the navigated page.
601+
* @returns {string} The URL of the navigated page.
602+
*/
484603
get url() {
485604
return this._url
486605
}
487606

607+
/**
608+
* Gets the ID of the navigation operation.
609+
* @returns {number} The ID of the navigation operation.
610+
*/
488611
get navigationId() {
489612
return this._navigationId
490613
}
491614
}
492615

616+
/**
617+
* Represents a print result.
618+
*/
493619
class PrintResult {
494620
constructor(data) {
495621
this._data = data
496622
}
497623

624+
/**
625+
* Gets the data associated with the print result.
626+
* @returns {any} The data associated with the print result.
627+
*/
498628
get data() {
499629
return this._data
500630
}
@@ -514,9 +644,5 @@ async function getBrowsingContextInstance(driver, { browsingContextId, type, ref
514644
return instance
515645
}
516646

517-
/**
518-
* API
519-
* @type {function(*, {*,*,*}): Promise<BrowsingContext>}
520-
*/
521647
module.exports = getBrowsingContextInstance
522648
module.exports.Locator = Locator

0 commit comments

Comments
 (0)