Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/cookie_test.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const assert = require('node:assert')
21
const { URL } = require('node:url')
22
23
const { ignore, suite } = require('../lib/test')
24
const fileserver = require('../lib/test/fileserver')
25
const { Browser } = require('selenium-webdriver')
26
27
suite(function (env) {
28
let driver
29
30
before(async function () {
31
driver = await env.builder().build()
32
})
33
34
after(function () {
35
return driver.quit()
36
})
37
38
describe('Cookie Management;', function () {
39
beforeEach(async function () {
40
await driver.get(fileserver.Pages.ajaxyPage)
41
await driver.manage().deleteAllCookies()
42
return assertHasCookies()
43
})
44
45
it('can add new cookies', async function () {
46
const cookie = createCookieSpec()
47
48
await driver.manage().addCookie(cookie)
49
await driver
50
.manage()
51
.getCookie(cookie.name)
52
.then(function (actual) {
53
assert.strictEqual(actual.value, cookie.value)
54
})
55
})
56
57
it('Get Cookie: throw error if name is null', async function () {
58
const cookie = createCookieSpec()
59
await driver.manage().addCookie(cookie)
60
await assert.rejects(async () => await driver.manage().getCookie(null), {
61
name: 'InvalidArgumentError',
62
message: `Cookie name cannot be empty`,
63
})
64
})
65
66
it('Delete cookie: throw error if name is null', async function () {
67
const cookie = createCookieSpec()
68
await driver.manage().addCookie(cookie)
69
await assert.rejects(async () => await driver.manage().deleteCookie(null), {
70
name: 'InvalidArgumentError',
71
message: `Cookie name cannot be empty`,
72
})
73
})
74
75
it('can get all cookies', async function () {
76
const cookie1 = createCookieSpec()
77
const cookie2 = createCookieSpec()
78
79
await driver.manage().addCookie(cookie1)
80
await driver.manage().addCookie(cookie2)
81
82
return assertHasCookies(cookie1, cookie2)
83
})
84
85
ignore(env.browsers(Browser.INTERNET_EXPLORER)).it(
86
'only returns cookies visible to the current page',
87
async function () {
88
const cookie1 = createCookieSpec()
89
90
await driver.manage().addCookie(cookie1)
91
92
const pageUrl = fileserver.whereIs('page/1')
93
const cookie2 = createCookieSpec({
94
path: new URL(pageUrl).pathname,
95
})
96
await driver.get(pageUrl)
97
await driver.manage().addCookie(cookie2)
98
await assertHasCookies(cookie1, cookie2)
99
100
await driver.get(fileserver.Pages.ajaxyPage)
101
await assertHasCookies(cookie1)
102
103
await driver.get(pageUrl)
104
await assertHasCookies(cookie1, cookie2)
105
},
106
)
107
108
it('can delete all cookies', async function () {
109
const cookie1 = createCookieSpec()
110
const cookie2 = createCookieSpec()
111
112
await driver.executeScript(
113
'document.cookie = arguments[0] + "=" + arguments[1];' + 'document.cookie = arguments[2] + "=" + arguments[3];',
114
cookie1.name,
115
cookie1.value,
116
cookie2.name,
117
cookie2.value,
118
)
119
await assertHasCookies(cookie1, cookie2)
120
121
await driver.manage().deleteAllCookies()
122
await assertHasCookies()
123
})
124
125
it('can delete cookies by name', async function () {
126
const cookie1 = createCookieSpec()
127
const cookie2 = createCookieSpec()
128
129
await driver.executeScript(
130
'document.cookie = arguments[0] + "=" + arguments[1];' + 'document.cookie = arguments[2] + "=" + arguments[3];',
131
cookie1.name,
132
cookie1.value,
133
cookie2.name,
134
cookie2.value,
135
)
136
await assertHasCookies(cookie1, cookie2)
137
138
await driver.manage().deleteCookie(cookie1.name)
139
await assertHasCookies(cookie2)
140
})
141
142
it('should only delete cookie with exact name', async function () {
143
const cookie1 = createCookieSpec()
144
const cookie2 = createCookieSpec()
145
const cookie3 = { name: cookie1.name + 'xx', value: cookie1.value }
146
147
await driver.executeScript(
148
'document.cookie = arguments[0] + "=" + arguments[1];' +
149
'document.cookie = arguments[2] + "=" + arguments[3];' +
150
'document.cookie = arguments[4] + "=" + arguments[5];',
151
cookie1.name,
152
cookie1.value,
153
cookie2.name,
154
cookie2.value,
155
cookie3.name,
156
cookie3.value,
157
)
158
await assertHasCookies(cookie1, cookie2, cookie3)
159
160
await driver.manage().deleteCookie(cookie1.name)
161
await assertHasCookies(cookie2, cookie3)
162
})
163
164
it('can delete cookies set higher in the path', async function () {
165
const cookie = createCookieSpec()
166
const childUrl = fileserver.whereIs('child/childPage.html')
167
const grandchildUrl = fileserver.whereIs('child/grandchild/grandchildPage.html')
168
169
await driver.get(childUrl)
170
await driver.manage().addCookie(cookie)
171
await assertHasCookies(cookie)
172
173
await driver.get(grandchildUrl)
174
await assertHasCookies(cookie)
175
176
await driver.manage().deleteCookie(cookie.name)
177
await assertHasCookies()
178
179
await driver.get(childUrl)
180
await assertHasCookies()
181
})
182
183
ignore(env.browsers(Browser.FIREFOX, Browser.INTERNET_EXPLORER)).it(
184
'should retain cookie expiry',
185
async function () {
186
let expirationDelay = 5 * 1000
187
let expiry = new Date(Date.now() + expirationDelay)
188
let cookie = createCookieSpec({ expiry })
189
190
await driver.manage().addCookie(cookie)
191
await driver
192
.manage()
193
.getCookie(cookie.name)
194
.then(function (actual) {
195
assert.strictEqual(actual.value, cookie.value)
196
197
// expiry times should be in seconds since January 1, 1970 UTC
198
assert.strictEqual(actual.expiry, Math.floor(expiry.getTime() / 1000))
199
})
200
201
await driver.sleep(expirationDelay)
202
await assertHasCookies()
203
},
204
)
205
206
ignore(env.browsers(Browser.FIREFOX, Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(
207
'can add same site cookie property to `Strict`',
208
async function () {
209
let cookie = createSameSiteCookieSpec('Strict')
210
let childUrl = fileserver.whereIs('child/childPage.html')
211
await driver.get(childUrl)
212
await driver.manage().addCookie(cookie)
213
const actual = await driver.manage().getCookie(cookie.name)
214
assert.strictEqual(actual.sameSite, 'Strict')
215
},
216
)
217
218
ignore(env.browsers(Browser.FIREFOX, Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(
219
'can add same site cookie property to `Lax`',
220
async function () {
221
let cookie = createSameSiteCookieSpec('Lax')
222
let childUrl = fileserver.whereIs('child/childPage.html')
223
await driver.get(childUrl)
224
await driver.manage().addCookie(cookie)
225
const actualCookie = await driver.manage().getCookie(cookie.name)
226
assert.strictEqual(actualCookie.sameSite, 'Lax')
227
},
228
)
229
230
ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(
231
'can add same site cookie property to `None` when cookie is Secure',
232
async function () {
233
let cookie = createSameSiteCookieSpec('None', {
234
secure: true,
235
})
236
let childUrl = fileserver.whereIs('child/childPage.html')
237
await driver.get(childUrl)
238
await driver.manage().addCookie(cookie)
239
await assert.doesNotReject(async () => await driver.manage().addCookie(cookie))
240
},
241
)
242
243
ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(
244
'throws an error if same site is set to `None` and the cookie is not Secure',
245
async function () {
246
let cookie = createSameSiteCookieSpec('None')
247
let childUrl = fileserver.whereIs('child/childPage.html')
248
await driver.get(childUrl)
249
await assert.rejects(async () => await driver.manage().addCookie(cookie), {
250
name: 'InvalidArgumentError',
251
message: `Invalid cookie configuration: SameSite=None must be Secure`,
252
})
253
},
254
)
255
256
ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(
257
'throws an error if same site cookie property is invalid',
258
async function () {
259
let cookie = createSameSiteCookieSpec('Foo')
260
let childUrl = fileserver.whereIs('child/childPage.html')
261
await driver.get(childUrl)
262
await assert.rejects(async () => await driver.manage().addCookie(cookie), {
263
name: 'InvalidArgumentError',
264
message: `Invalid sameSite cookie value 'Foo'. It should be one of "Lax", "Strict" or "None"`,
265
})
266
},
267
)
268
})
269
270
function createCookieSpec(opt_options) {
271
let spec = {
272
name: getRandomString(),
273
value: getRandomString(),
274
}
275
if (opt_options) {
276
spec = Object.assign(spec, opt_options)
277
}
278
return spec
279
}
280
281
function createSameSiteCookieSpec(sameSiteVal, extraProps) {
282
return {
283
name: getRandomString(),
284
value: getRandomString(),
285
sameSite: sameSiteVal,
286
...extraProps,
287
}
288
}
289
290
function buildCookieMap(cookies) {
291
const map = {}
292
cookies.forEach(function (cookie) {
293
map[cookie.name] = cookie
294
})
295
return map
296
}
297
298
function assertHasCookies(...expected) {
299
return driver
300
.manage()
301
.getCookies()
302
.then(function (cookies) {
303
assert.strictEqual(
304
cookies.length,
305
expected.length,
306
'Wrong # of cookies.' +
307
'\n Expected: ' +
308
JSON.stringify(expected) +
309
'\n Was : ' +
310
JSON.stringify(cookies),
311
)
312
313
const map = buildCookieMap(cookies)
314
for (let i = 0; i < expected.length; ++i) {
315
assert.strictEqual(expected[i].value, map[expected[i].name].value)
316
}
317
})
318
}
319
320
function getRandomString() {
321
const x = 1234567890
322
return Math.floor(Math.random() * x).toString(36)
323
}
324
})
325
326