@@ -21,6 +21,10 @@ const { SerializationOptions, ReferenceValue, RemoteValue } = require('./protoco
21
21
const { WebElement } = require ( '../lib/webdriver' )
22
22
const { CaptureScreenshotParameters } = require ( './captureScreenshotParameters' )
23
23
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
+ */
24
28
class Locator {
25
29
static Type = Object . freeze ( {
26
30
CSS : 'css' ,
@@ -42,14 +46,35 @@ class Locator {
42
46
this . #maxDepth = maxDepth
43
47
}
44
48
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
+ */
45
55
static css ( value ) {
46
56
return new Locator ( Locator . Type . CSS , value )
47
57
}
48
58
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
+ */
49
65
static xpath ( value ) {
50
66
return new Locator ( Locator . Type . XPATH , value )
51
67
}
52
68
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
+ */
53
78
static innerText ( value , ignoreCase = undefined , matchType = undefined , maxDepth = undefined ) {
54
79
return new Locator ( Locator . Type . INNER_TEXT , value , ignoreCase , matchType , maxDepth )
55
80
}
@@ -67,6 +92,12 @@ class Locator {
67
92
}
68
93
}
69
94
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
+ */
70
101
class BrowsingContext {
71
102
constructor ( driver ) {
72
103
this . _driver = driver
@@ -225,6 +256,13 @@ class BrowsingContext {
225
256
return new PrintResult ( response . result . data )
226
257
}
227
258
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
+ */
228
266
async captureScreenshot ( captureScreenshotParameters = undefined ) {
229
267
if (
230
268
captureScreenshotParameters !== undefined &&
@@ -273,6 +311,12 @@ class BrowsingContext {
273
311
return response [ 'result' ] [ 'data' ]
274
312
}
275
313
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
+ */
276
320
async captureElementScreenshot ( sharedId , handle = undefined ) {
277
321
let params = {
278
322
method : 'browsingContext.captureScreenshot' ,
@@ -307,6 +351,11 @@ class BrowsingContext {
307
351
}
308
352
}
309
353
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
+ */
310
359
async activate ( ) {
311
360
const params = {
312
361
method : 'browsingContext.activate' ,
@@ -321,6 +370,13 @@ class BrowsingContext {
321
370
}
322
371
}
323
372
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
+ */
324
380
async handleUserPrompt ( accept = undefined , userText = undefined ) {
325
381
const params = {
326
382
method : 'browsingContext.handleUserPrompt' ,
@@ -337,6 +393,15 @@ class BrowsingContext {
337
393
}
338
394
}
339
395
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
+ */
340
405
async reload ( ignoreCache = undefined , readinessState = undefined ) {
341
406
if ( readinessState !== undefined && ! [ 'none' , 'interactive' , 'complete' ] . includes ( readinessState ) ) {
342
407
throw Error ( `Valid readiness states are 'none', 'interactive' & 'complete'. Received: ${ readinessState } ` )
@@ -355,6 +420,13 @@ class BrowsingContext {
355
420
return new NavigateResult ( navigateResult [ 'url' ] , navigateResult [ 'navigation' ] )
356
421
}
357
422
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
+ */
358
430
async setViewport ( width , height , devicePixelRatio = undefined ) {
359
431
const params = {
360
432
method : 'browsingContext.setViewport' ,
@@ -370,6 +442,12 @@ class BrowsingContext {
370
442
}
371
443
}
372
444
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
+ */
373
451
async traverseHistory ( delta ) {
374
452
const params = {
375
453
method : 'browsingContext.traverseHistory' ,
@@ -381,14 +459,38 @@ class BrowsingContext {
381
459
await this . bidi . send ( params )
382
460
}
383
461
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
+ */
384
466
async forward ( ) {
385
467
await this . traverseHistory ( 1 )
386
468
}
387
469
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
+ */
388
474
async back ( ) {
389
475
await this . traverseHistory ( - 1 )
390
476
}
391
477
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
+ */
392
494
async locateNodes (
393
495
locator ,
394
496
maxNodeCount = undefined ,
@@ -448,6 +550,16 @@ class BrowsingContext {
448
550
return remoteValues
449
551
}
450
552
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
+ */
451
563
async locateNode (
452
564
locator ,
453
565
ownership = undefined ,
@@ -475,26 +587,44 @@ class BrowsingContext {
475
587
}
476
588
}
477
589
590
+ /**
591
+ * Represents the result of a navigation operation.
592
+ */
478
593
class NavigateResult {
479
594
constructor ( url , navigationId ) {
480
595
this . _url = url
481
596
this . _navigationId = navigationId
482
597
}
483
598
599
+ /**
600
+ * Gets the URL of the navigated page.
601
+ * @returns {string } The URL of the navigated page.
602
+ */
484
603
get url ( ) {
485
604
return this . _url
486
605
}
487
606
607
+ /**
608
+ * Gets the ID of the navigation operation.
609
+ * @returns {number } The ID of the navigation operation.
610
+ */
488
611
get navigationId ( ) {
489
612
return this . _navigationId
490
613
}
491
614
}
492
615
616
+ /**
617
+ * Represents a print result.
618
+ */
493
619
class PrintResult {
494
620
constructor ( data ) {
495
621
this . _data = data
496
622
}
497
623
624
+ /**
625
+ * Gets the data associated with the print result.
626
+ * @returns {any } The data associated with the print result.
627
+ */
498
628
get data ( ) {
499
629
return this . _data
500
630
}
@@ -514,9 +644,5 @@ async function getBrowsingContextInstance(driver, { browsingContextId, type, ref
514
644
return instance
515
645
}
516
646
517
- /**
518
- * API
519
- * @type {function(*, {*,*,*}): Promise<BrowsingContext> }
520
- */
521
647
module . exports = getBrowsingContextInstance
522
648
module . exports . Locator = Locator
0 commit comments