@@ -1326,6 +1326,91 @@ class Options {
13261326 } ) ;
13271327 }
13281328
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+
13291414 /**
13301415 * @return {!Logs } The interface for managing driver
13311416 * logs.
@@ -1336,6 +1421,7 @@ class Options {
13361421
13371422 /**
13381423 * @return {!Timeouts } The interface for managing driver timeouts.
1424+ * @deprecated Use {@link #setTimeouts()} instead.
13391425 */
13401426 timeouts ( ) {
13411427 return new Timeouts ( this . driver_ ) ;
@@ -1350,6 +1436,22 @@ class Options {
13501436}
13511437
13521438
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+
13531455/**
13541456 * A record object describing a browser cookie.
13551457 *
@@ -1432,6 +1534,9 @@ Options.Cookie.prototype.expiry;
14321534 *
14331535 * webdriver.manage().timeouts()
14341536 *
1537+ * @deprecated This has been deprecated in favor of
1538+ * {@link Options#setTimeouts()}, which supports setting multiple timeouts
1539+ * at once.
14351540 * @see WebDriver#manage()
14361541 * @see Options#timeouts()
14371542 */
@@ -1465,9 +1570,11 @@ class Timeouts {
14651570 * @param {number } ms The amount of time to wait, in milliseconds.
14661571 * @return {!promise.Thenable<void> } A promise that will be resolved
14671572 * when the implicit wait timeout has been set.
1573+ * @deprecated Use {@link Options#setTimeouts()
1574+ * driver.manage().setTimeouts({implicit: ms})}.
14681575 */
14691576 implicitlyWait ( ms ) {
1470- return this . _scheduleCommand ( ms , ' implicit' , 'implicitlyWait' ) ;
1577+ return this . driver_ . manage ( ) . setTimeouts ( { implicit : ms } ) ;
14711578 }
14721579
14731580 /**
@@ -1478,9 +1585,11 @@ class Timeouts {
14781585 * @param {number } ms The amount of time to wait, in milliseconds.
14791586 * @return {!promise.Thenable<void> } A promise that will be resolved
14801587 * when the script timeout has been set.
1588+ * @deprecated Use {@link Options#setTimeouts()
1589+ * driver.manage().setTimeouts({script: ms})}.
14811590 */
14821591 setScriptTimeout ( ms ) {
1483- return this . _scheduleCommand ( ms , ' script' , 'setScriptTimeout' ) ;
1592+ return this . driver_ . manage ( ) . setTimeouts ( { script : ms } ) ;
14841593 }
14851594
14861595 /**
@@ -1491,17 +1600,11 @@ class Timeouts {
14911600 * @param {number } ms The amount of time to wait, in milliseconds.
14921601 * @return {!promise.Thenable<void> } A promise that will be resolved
14931602 * when the timeout has been set.
1603+ * @deprecated Use {@link Options#setTimeouts()
1604+ * driver.manage().setTimeouts({pageLoad: ms})}.
14941605 */
14951606 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 } ) ;
15051608 }
15061609}
15071610
0 commit comments