Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/storage_test.js
11822 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 { suite, Pages } = require('../../../lib/test')
22
const { Browser } = require('selenium-webdriver')
23
24
const { Storage } = require('selenium-webdriver/bidi/generated/storage')
25
26
suite(
27
function (env) {
28
let driver
29
let storage
30
31
beforeEach(async function () {
32
driver = await env.builder().build()
33
storage = await Storage.create(driver)
34
// Navigate to a page so cookie operations have a valid origin
35
await driver.get(Pages.ajaxyPage)
36
await driver.manage().deleteAllCookies()
37
})
38
39
afterEach(function () {
40
return driver.quit()
41
})
42
43
describe('setCookie and getCookies', function () {
44
it('can set and get a cookie by name', async function () {
45
const url = await driver.getCurrentUrl()
46
const domain = new URL(url).hostname
47
48
await storage.setCookie({
49
cookie: {
50
name: 'test-cookie',
51
value: { type: 'string', value: 'test-value' },
52
domain,
53
},
54
})
55
56
const result = await storage.getCookies({
57
filter: { name: 'test-cookie' },
58
})
59
60
assert.ok(result.cookies.length >= 1)
61
const found = result.cookies.find((c) => c.name === 'test-cookie')
62
assert.ok(found)
63
assert.strictEqual(found.value.value, 'test-value')
64
})
65
66
it('can set a cookie with path and httpOnly', async function () {
67
const url = await driver.getCurrentUrl()
68
const domain = new URL(url).hostname
69
70
await storage.setCookie({
71
cookie: {
72
name: 'secure-cookie',
73
value: { type: 'string', value: 'secure-val' },
74
domain,
75
path: '/',
76
httpOnly: true,
77
},
78
})
79
80
const result = await storage.getCookies({
81
filter: { name: 'secure-cookie' },
82
})
83
84
assert.ok(result.cookies.length >= 1)
85
const found = result.cookies.find((c) => c.name === 'secure-cookie')
86
assert.ok(found)
87
assert.strictEqual(found.httpOnly, true)
88
})
89
90
it('getCookies returns partition key', async function () {
91
const url = await driver.getCurrentUrl()
92
const domain = new URL(url).hostname
93
94
await storage.setCookie({
95
cookie: {
96
name: 'partition-test',
97
value: { type: 'string', value: 'pval' },
98
domain,
99
},
100
})
101
102
const contextId = await driver.getWindowHandle()
103
const result = await storage.getCookies({
104
filter: { name: 'partition-test' },
105
partition: { type: 'context', context: contextId },
106
})
107
108
assert.ok(result.partitionKey !== undefined)
109
assert.ok(result.cookies.length >= 1)
110
})
111
112
it('can filter cookies by domain', async function () {
113
const url = await driver.getCurrentUrl()
114
const domain = new URL(url).hostname
115
116
await storage.setCookie({
117
cookie: {
118
name: 'domain-cookie',
119
value: { type: 'string', value: 'domain-val' },
120
domain,
121
},
122
})
123
124
const result = await storage.getCookies({
125
filter: { domain },
126
})
127
128
assert.ok(result.cookies.length >= 1)
129
})
130
131
it('getCookies returns empty when no cookies match filter', async function () {
132
const result = await storage.getCookies({
133
filter: { name: 'nonexistent-cookie-xyz-123' },
134
})
135
136
assert.strictEqual(result.cookies.length, 0)
137
})
138
})
139
140
describe('deleteCookies', function () {
141
it('can delete a specific cookie by name', async function () {
142
const url = await driver.getCurrentUrl()
143
const domain = new URL(url).hostname
144
145
// Add two cookies
146
await storage.setCookie({
147
cookie: {
148
name: 'cookie-to-delete',
149
value: { type: 'string', value: 'delete-me' },
150
domain,
151
},
152
})
153
await storage.setCookie({
154
cookie: {
155
name: 'cookie-to-keep',
156
value: { type: 'string', value: 'keep-me' },
157
domain,
158
},
159
})
160
161
// Delete one
162
const deleteResult = await storage.deleteCookies({
163
filter: { name: 'cookie-to-delete' },
164
})
165
166
assert.ok(deleteResult.partitionKey !== undefined)
167
168
// Verify it's gone
169
const remaining = await storage.getCookies({
170
filter: { name: 'cookie-to-delete' },
171
})
172
assert.strictEqual(remaining.cookies.length, 0)
173
174
// Verify the other is still there
175
const kept = await storage.getCookies({
176
filter: { name: 'cookie-to-keep' },
177
})
178
assert.ok(kept.cookies.length >= 1)
179
})
180
181
it('can delete all cookies for a domain', async function () {
182
const url = await driver.getCurrentUrl()
183
const domain = new URL(url).hostname
184
185
await storage.setCookie({
186
cookie: {
187
name: 'c1',
188
value: { type: 'string', value: 'v1' },
189
domain,
190
},
191
})
192
await storage.setCookie({
193
cookie: {
194
name: 'c2',
195
value: { type: 'string', value: 'v2' },
196
domain,
197
},
198
})
199
200
await storage.deleteCookies({ filter: { domain } })
201
202
const result = await storage.getCookies({ filter: { domain } })
203
assert.strictEqual(result.cookies.length, 0)
204
})
205
206
it('deleteCookies with context partition', async function () {
207
const url = await driver.getCurrentUrl()
208
const domain = new URL(url).hostname
209
const contextId = await driver.getWindowHandle()
210
211
await storage.setCookie({
212
cookie: {
213
name: 'ctx-cookie',
214
value: { type: 'string', value: 'ctx-val' },
215
domain,
216
},
217
})
218
219
const result = await storage.deleteCookies({
220
filter: { name: 'ctx-cookie' },
221
partition: { type: 'context', context: contextId },
222
})
223
224
assert.ok(result.partitionKey !== undefined)
225
})
226
})
227
},
228
{ browsers: [Browser.CHROME, Browser.FIREFOX] },
229
)
230
231