@@ -1326,6 +1326,91 @@ class Options {
1326
1326
} ) ;
1327
1327
}
1328
1328
1329
+ /**
1330
+ * Schedules a command to fetch the timeouts currently configured for the
1331
+ * current session.
1332
+ *
1333
+ * @return {!promise.Thenable<{script: number,
1334
+ * pageLoad: number,
1335
+ * implicit: number}>} A promise that will be
1336
+ * resolved with the timeouts currently configured for the current
1337
+ * session.
1338
+ * @see #setTimeouts()
1339
+ */
1340
+ getTimeouts ( ) {
1341
+ return this . driver_ . schedule (
1342
+ new command . Command ( command . Name . GET_TIMEOUT ) ,
1343
+ `WebDriver.manage().getTimeouts()` )
1344
+ }
1345
+
1346
+ /**
1347
+ * Schedules a command to set timeout durations associated with the current
1348
+ * session.
1349
+ *
1350
+ * The following timeouts are supported (all timeouts are specified in
1351
+ * milliseconds):
1352
+ *
1353
+ * - `implicit` specifies the maximum amount of time to wait for an element
1354
+ * locator to succeed when {@linkplain WebDriver#findElement locating}
1355
+ * {@linkplain WebDriver#findElements elements} on the page.
1356
+ * Defaults to 0 milliseconds.
1357
+ *
1358
+ * - `pageLoad` specifies the maximum amount of time to wait for a page to
1359
+ * finishing loading. Defaults to 300000 milliseconds.
1360
+ *
1361
+ * - `script` specifies the maximum amount of time to wait for an
1362
+ * {@linkplain WebDriver#executeScript evaluated script} to run. If set to
1363
+ * `null`, the script timeout will be indefinite.
1364
+ * Defaults to 30000 milliseconds.
1365
+ *
1366
+ * @param {{script: (number|null|undefined),
1367
+ * pageLoad: (number|null|undefined),
1368
+ * implicit: (number|null|undefined)}} conf
1369
+ * The desired timeout configuration.
1370
+ * @return {!promise.Thenable<void> } A promise that will be resolved when the
1371
+ * timeouts have been set.
1372
+ * @throws {!TypeError } if an invalid options object is provided.
1373
+ * @see #getTimeouts()
1374
+ * @see <https://w3c.github.io/webdriver/webdriver-spec.html#dfn-set-timeouts>
1375
+ */
1376
+ setTimeouts ( { script, pageLoad, implicit} = { } ) {
1377
+ let cmd = new command . Command ( command . Name . SET_TIMEOUT ) ;
1378
+
1379
+ let valid = false ;
1380
+ function setParam ( key , value ) {
1381
+ if ( value === null || typeof value === 'number' ) {
1382
+ valid = true ;
1383
+ cmd . setParameter ( key , value ) ;
1384
+ } else if ( typeof value !== 'undefined' ) {
1385
+ throw TypeError (
1386
+ 'invalid timeouts configuration:'
1387
+ + ` expected "${ key } " to be a number, got ${ typeof value } ` ) ;
1388
+ }
1389
+ }
1390
+ setParam ( 'implicit' , implicit ) ;
1391
+ setParam ( 'pageLoad' , pageLoad ) ;
1392
+ setParam ( 'script' , script ) ;
1393
+
1394
+ if ( valid ) {
1395
+ return this . driver_ . schedule ( cmd , `WebDriver.manage().setTimeouts()` )
1396
+ . catch ( ( ) => {
1397
+ // Fallback to the legacy method.
1398
+ let cmds = [ ] ;
1399
+ if ( typeof script === 'number' ) {
1400
+ cmds . push ( legacyTimeout ( this . driver_ , 'script' , script ) ) ;
1401
+ }
1402
+ if ( typeof implicit === 'number' ) {
1403
+ cmds . push ( legacyTimeout ( this . driver_ , 'implicit' , implicit ) ) ;
1404
+ }
1405
+ if ( typeof pageLoad === 'number' ) {
1406
+ cmds . push ( legacyTimeout ( this . driver_ , 'page load' , pageLoad ) ) ;
1407
+ }
1408
+ return Promise . all ( cmds ) ;
1409
+ } ) ;
1410
+ }
1411
+ throw TypeError ( 'no timeouts specified' ) ;
1412
+ }
1413
+
1329
1414
/**
1330
1415
* @return {!Logs } The interface for managing driver
1331
1416
* logs.
@@ -1336,6 +1421,7 @@ class Options {
1336
1421
1337
1422
/**
1338
1423
* @return {!Timeouts } The interface for managing driver timeouts.
1424
+ * @deprecated Use {@link #setTimeouts()} instead.
1339
1425
*/
1340
1426
timeouts ( ) {
1341
1427
return new Timeouts ( this . driver_ ) ;
@@ -1350,6 +1436,22 @@ class Options {
1350
1436
}
1351
1437
1352
1438
1439
+ /**
1440
+ * @param {!WebDriver } driver
1441
+ * @param {string } type
1442
+ * @param {number } ms
1443
+ * @return {!promise.Thenable<void> }
1444
+ */
1445
+ function legacyTimeout ( driver , type , ms ) {
1446
+ return driver . schedule (
1447
+ new command . Command ( command . Name . SET_TIMEOUT )
1448
+ . setParameter ( 'type' , type )
1449
+ . setParameter ( 'ms' , ms ) ,
1450
+ `WebDriver.manage().setTimeouts({${ type } : ${ ms } })` ) ;
1451
+ }
1452
+
1453
+
1454
+
1353
1455
/**
1354
1456
* A record object describing a browser cookie.
1355
1457
*
@@ -1432,6 +1534,9 @@ Options.Cookie.prototype.expiry;
1432
1534
*
1433
1535
* webdriver.manage().timeouts()
1434
1536
*
1537
+ * @deprecated This has been deprecated in favor of
1538
+ * {@link Options#setTimeouts()}, which supports setting multiple timeouts
1539
+ * at once.
1435
1540
* @see WebDriver#manage()
1436
1541
* @see Options#timeouts()
1437
1542
*/
@@ -1465,9 +1570,11 @@ class Timeouts {
1465
1570
* @param {number } ms The amount of time to wait, in milliseconds.
1466
1571
* @return {!promise.Thenable<void> } A promise that will be resolved
1467
1572
* when the implicit wait timeout has been set.
1573
+ * @deprecated Use {@link Options#setTimeouts()
1574
+ * driver.manage().setTimeouts({implicit: ms})}.
1468
1575
*/
1469
1576
implicitlyWait ( ms ) {
1470
- return this . _scheduleCommand ( ms , ' implicit' , 'implicitlyWait' ) ;
1577
+ return this . driver_ . manage ( ) . setTimeouts ( { implicit : ms } ) ;
1471
1578
}
1472
1579
1473
1580
/**
@@ -1478,9 +1585,11 @@ class Timeouts {
1478
1585
* @param {number } ms The amount of time to wait, in milliseconds.
1479
1586
* @return {!promise.Thenable<void> } A promise that will be resolved
1480
1587
* when the script timeout has been set.
1588
+ * @deprecated Use {@link Options#setTimeouts()
1589
+ * driver.manage().setTimeouts({script: ms})}.
1481
1590
*/
1482
1591
setScriptTimeout ( ms ) {
1483
- return this . _scheduleCommand ( ms , ' script' , 'setScriptTimeout' ) ;
1592
+ return this . driver_ . manage ( ) . setTimeouts ( { script : ms } ) ;
1484
1593
}
1485
1594
1486
1595
/**
@@ -1491,17 +1600,11 @@ class Timeouts {
1491
1600
* @param {number } ms The amount of time to wait, in milliseconds.
1492
1601
* @return {!promise.Thenable<void> } A promise that will be resolved
1493
1602
* when the timeout has been set.
1603
+ * @deprecated Use {@link Options#setTimeouts()
1604
+ * driver.manage().setTimeouts({pageLoad: ms})}.
1494
1605
*/
1495
1606
pageLoadTimeout ( ms ) {
1496
- return this . _scheduleCommand ( ms , 'page load' , 'pageLoadTimeout' ) ;
1497
- }
1498
-
1499
- _scheduleCommand ( ms , timeoutIdentifier , timeoutName ) {
1500
- return this . driver_ . schedule (
1501
- new command . Command ( command . Name . SET_TIMEOUT ) .
1502
- setParameter ( 'type' , timeoutIdentifier ) .
1503
- setParameter ( 'ms' , ms ) ,
1504
- `WebDriver.manage().timeouts().${ timeoutName } (${ ms } )` ) ;
1607
+ return this . driver_ . manage ( ) . setTimeouts ( { pageLoad : ms } ) ;
1505
1608
}
1506
1609
}
1507
1610
0 commit comments