@@ -1043,11 +1043,7 @@ class Navigation {
1043
1043
* Provides methods for managing browser and driver state.
1044
1044
*
1045
1045
* This class should never be instantiated directly. Insead, obtain an instance
1046
- * with
1047
- *
1048
- * webdriver.manage()
1049
- *
1050
- * @see WebDriver#manage()
1046
+ * with {@linkplain WebDriver#manage() webdriver.manage()}.
1051
1047
*/
1052
1048
class Options {
1053
1049
/**
@@ -1061,57 +1057,72 @@ class Options {
1061
1057
1062
1058
/**
1063
1059
* Schedules a command to add a cookie.
1064
- * @param {string } name The cookie name.
1065
- * @param {string } value The cookie value.
1066
- * @param {string= } opt_path The cookie path.
1067
- * @param {string= } opt_domain The cookie domain.
1068
- * @param {boolean= } opt_isSecure Whether the cookie is secure.
1069
- * @param {(number|!Date)= } opt_expiry When the cookie expires. If specified
1070
- * as a number, should be in milliseconds since midnight,
1071
- * January 1, 1970 UTC.
1060
+ *
1061
+ * __Sample Usage:__
1062
+ *
1063
+ * // Set a basic cookie.
1064
+ * driver.options().addCookie({name: 'foo', value: 'bar'});
1065
+ *
1066
+ * // Set a cookie that expires in 10 minutes.
1067
+ * let expiry = new Date(Date.now() + (10 * 60 * 1000));
1068
+ * driver.options().addCookie({name: 'foo', value: 'bar', expiry});
1069
+ *
1070
+ * // The cookie expiration may also be specified in seconds since epoch.
1071
+ * driver.options().addCookie({
1072
+ * name: 'foo',
1073
+ * value: 'bar',
1074
+ * expiry: Math.floor(Date.now() / 1000)
1075
+ * });
1076
+ *
1077
+ * @param {!Options.Cookie } spec Defines the cookie to add.
1072
1078
* @return {!promise.Promise<void> } A promise that will be resolved
1073
1079
* when the cookie has been added to the page.
1080
+ * @throws {error.InvalidArgumentError } if any of the cookie parameters are
1081
+ * invalid.
1082
+ * @throws {TypeError } if `spec` is not a cookie object.
1074
1083
*/
1075
- addCookie ( name , value , opt_path , opt_domain , opt_isSecure , opt_expiry ) {
1084
+ addCookie ( spec ) {
1085
+ if ( ! spec || typeof spec !== 'object' ) {
1086
+ throw TypeError ( 'addCookie called with non-cookie parameter' ) ;
1087
+ }
1088
+
1076
1089
// We do not allow '=' or ';' in the name.
1090
+ let name = spec . name ;
1077
1091
if ( / [ ; = ] / . test ( name ) ) {
1078
1092
throw new error . InvalidArgumentError (
1079
1093
'Invalid cookie name "' + name + '"' ) ;
1080
1094
}
1081
1095
1082
1096
// We do not allow ';' in value.
1097
+ let value = spec . value ;
1083
1098
if ( / ; / . test ( value ) ) {
1084
1099
throw new error . InvalidArgumentError (
1085
1100
'Invalid cookie value "' + value + '"' ) ;
1086
1101
}
1087
1102
1088
- var cookieString = name + '=' + value +
1089
- ( opt_domain ? ';domain=' + opt_domain : '' ) +
1090
- ( opt_path ? ';path=' + opt_path : '' ) +
1091
- ( opt_isSecure ? ';secure' : '' ) ;
1092
-
1093
- var expiry ;
1094
- if ( opt_expiry !== void ( 0 ) ) {
1095
- var expiryDate ;
1096
- if ( typeof opt_expiry === 'number' ) {
1097
- expiryDate = new Date ( opt_expiry ) ;
1098
- } else {
1099
- expiryDate = /** @type {!Date } */ ( opt_expiry ) ;
1100
- opt_expiry = expiryDate . getTime ( ) ;
1101
- }
1102
- cookieString += ';expires=' + expiryDate . toUTCString ( ) ;
1103
- // Convert from milliseconds to seconds.
1104
- expiry = Math . floor ( /** @type {number } */ ( opt_expiry ) / 1000 ) ;
1103
+ let cookieString = name + '=' + value +
1104
+ ( spec . domain ? ';domain=' + spec . domain : '' ) +
1105
+ ( spec . path ? ';path=' + spec . path : '' ) +
1106
+ ( spec . secure ? ';secure' : '' ) ;
1107
+
1108
+ let expiry ;
1109
+ if ( typeof spec . expiry === 'number' ) {
1110
+ spec . expiry = Math . floor ( spec . expiry ) ;
1111
+ cookieString += ';expires=' + new Date ( spec . expiry * 1000 ) . toUTCString ( ) ;
1112
+ } else if ( spec . expiry instanceof Date ) {
1113
+ let date = /** @type {!Date } */ ( spec . expiry ) ;
1114
+ expiry = Math . floor ( date . getTime ( ) / 1000 ) ;
1115
+ cookieString += ';expires=' + date . toUTCString ( ) ;
1105
1116
}
1106
1117
1107
1118
return this . driver_ . schedule (
1108
1119
new command . Command ( command . Name . ADD_COOKIE ) .
1109
1120
setParameter ( 'cookie' , {
1110
1121
'name' : name ,
1111
1122
'value' : value ,
1112
- 'path' : opt_path ,
1113
- 'domain' : opt_domain ,
1114
- 'secure' : ! ! opt_isSecure ,
1123
+ 'path' : spec . path ,
1124
+ 'domain' : spec . domain ,
1125
+ 'secure' : ! ! spec . secure ,
1115
1126
'expiry' : expiry
1116
1127
} ) ,
1117
1128
'WebDriver.manage().addCookie(' + cookieString + ')' ) ;
@@ -1129,8 +1140,8 @@ class Options {
1129
1140
}
1130
1141
1131
1142
/**
1132
- * Schedules a command to delete the cookie with the given name. This command is
1133
- * a no-op if there is no cookie with the given name visible to the current
1143
+ * Schedules a command to delete the cookie with the given name. This command
1144
+ * is a no-op if there is no cookie with the given name visible to the current
1134
1145
* page.
1135
1146
* @param {string } name The name of the cookie to delete.
1136
1147
* @return {!promise.Promise<void> } A promise that will be resolved
@@ -1147,8 +1158,8 @@ class Options {
1147
1158
* Schedules a command to retrieve all cookies visible to the current page.
1148
1159
* Each cookie will be returned as a JSON object as described by the WebDriver
1149
1160
* wire protocol.
1150
- * @return {!promise.Promise<!Array<WebDriver. Options.Cookie>> } A
1151
- * promise that will be resolved with the cookies visible to the current page .
1161
+ * @return {!promise.Promise<!Array<! Options.Cookie>> } A promise that will be
1162
+ * resolved with the cookies visible to the current browsing context .
1152
1163
*/
1153
1164
getCookies ( ) {
1154
1165
return this . driver_ . schedule (
@@ -1162,9 +1173,8 @@ class Options {
1162
1173
* described by the WebDriver wire protocol.
1163
1174
*
1164
1175
* @param {string } name The name of the cookie to retrieve.
1165
- * @return {!promise.Promise<?WebDriver.Options.Cookie> } A promise
1166
- * that will be resolved with the named cookie, or `null` if there is no
1167
- * such cookie.
1176
+ * @return {!promise.Promise<?Options.Cookie> } A promise that will be resolved
1177
+ * with the named cookie, or `null` if there is no such cookie.
1168
1178
*/
1169
1179
getCookie ( name ) {
1170
1180
return this . getCookies ( ) . then ( function ( cookies ) {
@@ -1202,17 +1212,77 @@ class Options {
1202
1212
1203
1213
1204
1214
/**
1205
- * A JSON description of a browser cookie.
1206
- * @typedef {{
1207
- * name: string,
1208
- * value: string,
1209
- * path: (string|undefined),
1210
- * domain: (string|undefined),
1211
- * secure: (boolean|undefined),
1212
- * expiry: (number|undefined)
1213
- * }}
1215
+ * A record object describing a browser cookie.
1216
+ *
1217
+ * @record
1218
+ */
1219
+ Options . Cookie = function ( ) { } ;
1220
+
1221
+
1222
+ /**
1223
+ * The name of the cookie.
1224
+ *
1225
+ * @type {string }
1226
+ */
1227
+ Options . Cookie . prototype . name ;
1228
+
1229
+
1230
+ /**
1231
+ * The cookie value.
1232
+ *
1233
+ * @type {string }
1234
+ */
1235
+ Options . Cookie . prototype . value ;
1236
+
1237
+
1238
+ /**
1239
+ * The cookie path. Defaults to "/" when adding a cookie.
1240
+ *
1241
+ * @type {(string|undefined) }
1242
+ */
1243
+ Options . Cookie . prototype . path ;
1244
+
1245
+
1246
+ /**
1247
+ * The domain the cookie is visible to. Defaults to the current browsing
1248
+ * context's document's URL when adding a cookie.
1249
+ *
1250
+ * @type {(string|undefined) }
1251
+ */
1252
+ Options . Cookie . prototype . domain ;
1253
+
1254
+
1255
+ /**
1256
+ * Whether the cookie is a secure cookie. Defaults to false when adding a new
1257
+ * cookie.
1258
+ *
1259
+ * @type {(boolean|undefined) }
1260
+ */
1261
+ Options . Cookie . prototype . secure ;
1262
+
1263
+
1264
+ /**
1265
+ * Whether the cookie is an HTTP only cookie. Defaults to false when adding a
1266
+ * new cookie.
1267
+ *
1268
+ * @type {(boolean|undefined) }
1269
+ */
1270
+ Options . Cookie . prototype . httpOnly ;
1271
+
1272
+
1273
+ /**
1274
+ * When the cookie expires.
1275
+ *
1276
+ * When {@linkplain Options#addCookie() adding a cookie}, this may be specified
1277
+ * in _seconds_ since Unix epoch (January 1, 1970). The expiry will default to
1278
+ * 20 years in the future if omitted.
1279
+ *
1280
+ * The expiry is always returned in seconds since epoch when
1281
+ * {@linkplain Options#getCookies() retrieving cookies} from the browser.
1282
+ *
1283
+ * @type {(!Date|number|undefined) }
1214
1284
*/
1215
- Options . Cookie ;
1285
+ Options . Cookie . prototype . expiry ;
1216
1286
1217
1287
1218
1288
/**
0 commit comments