Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/storage.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
const { CookieFilter } = require('./cookieFilter')
19
const { BrowsingContextPartitionDescriptor, StorageKeyPartitionDescriptor } = require('./partitionDescriptor')
20
const { PartitionKey } = require('./partitionKey')
21
const { PartialCookie } = require('./partialCookie')
22
const { Cookie, BytesValue } = require('./networkTypes')
23
24
/**
25
* Represents commands of Storage module.
26
* Described in https://w3c.github.io/webdriver-bidi/#module-storage.
27
* @class
28
*/
29
class Storage {
30
constructor(driver) {
31
this._driver = driver
32
}
33
34
async init() {
35
if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {
36
throw Error('WebDriver instance must support BiDi protocol')
37
}
38
39
this.bidi = await this._driver.getBidi()
40
}
41
42
/**
43
* Retrieves cookies based on the provided filter and partition.
44
*
45
* @param {CookieFilter} [filter] - The filter to apply to the cookies.
46
* @param {(BrowsingContextPartitionDescriptor|StorageKeyPartitionDescriptor)} [partition] - The partition to retrieve cookies from.
47
* @returns {Promise<{ cookies: Cookie[], partitionKey?: PartitionKey }>} - A promise that resolves to an object containing the retrieved cookies and an optional partition key.
48
* @throws {Error} If the filter parameter is provided but is not an instance of CookieFilter.
49
* @throws {Error} If the partition parameter is provided but is not an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor.
50
*/
51
async getCookies(filter = undefined, partition = undefined) {
52
if (filter !== undefined && !(filter instanceof CookieFilter)) {
53
throw new Error(`Params must be an instance of CookieFilter. Received:'${filter}'`)
54
}
55
56
if (
57
partition !== undefined &&
58
!(partition instanceof BrowsingContextPartitionDescriptor || partition instanceof StorageKeyPartitionDescriptor)
59
) {
60
throw new Error(
61
`Params must be an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor. Received:'${partition}'`,
62
)
63
}
64
65
const command = {
66
method: 'storage.getCookies',
67
params: {
68
filter: filter ? Object.fromEntries(filter.asMap()) : undefined,
69
partition: partition ? Object.fromEntries(partition.asMap()) : undefined,
70
},
71
}
72
73
let response = await this.bidi.send(command)
74
75
let cookies = []
76
response.result.cookies.forEach((cookie) => {
77
cookies.push(
78
new Cookie(
79
cookie.name,
80
new BytesValue(cookie.value.type, cookie.value.value),
81
cookie.domain,
82
cookie.path,
83
cookie.size,
84
cookie.httpOnly,
85
cookie.secure,
86
cookie.sameSite,
87
cookie.expiry,
88
),
89
)
90
})
91
92
if (Object.prototype.hasOwnProperty.call(response.result, 'partitionKey')) {
93
if (
94
Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'userContext') &&
95
Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'sourceOrigin')
96
) {
97
let partitionKey = new PartitionKey(
98
response.result.partitionKey.userContext,
99
response.result.partitionKey.sourceOrigin,
100
)
101
return { cookies, partitionKey }
102
}
103
104
return { cookies }
105
}
106
}
107
108
/**
109
* Sets a cookie using the provided cookie object and partition.
110
*
111
* @param {PartialCookie} cookie - The cookie object to set.
112
* @param {(BrowsingContextPartitionDescriptor|StorageKeyPartitionDescriptor)} [partition] - The partition to use for the cookie.
113
* @returns {PartitionKey} The partition key of the set cookie.
114
* @throws {Error} If the cookie parameter is not an instance of PartialCookie or if the partition parameter is not an instance of PartitionDescriptor.
115
*/
116
async setCookie(cookie, partition = undefined) {
117
if (!(cookie instanceof PartialCookie)) {
118
throw new Error(`Params must be an instance of PartialCookie. Received:'${cookie}'`)
119
}
120
121
if (
122
partition !== undefined &&
123
!(partition instanceof BrowsingContextPartitionDescriptor || partition instanceof StorageKeyPartitionDescriptor)
124
) {
125
throw new Error(
126
`Params must be an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor. Received:'${partition}'`,
127
)
128
}
129
130
const command = {
131
method: 'storage.setCookie',
132
params: {
133
cookie: cookie ? Object.fromEntries(cookie.asMap()) : undefined,
134
partition: partition ? Object.fromEntries(partition.asMap()) : undefined,
135
},
136
}
137
138
let response = await this.bidi.send(command)
139
140
if (Object.prototype.hasOwnProperty.call(response.result, 'partitionKey')) {
141
if (
142
Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'userContext') &&
143
Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'sourceOrigin')
144
) {
145
return new PartitionKey(response.result.partitionKey.userContext, response.result.partitionKey.sourceOrigin)
146
}
147
}
148
}
149
150
/**
151
* Deletes cookies based on the provided filter and partition.
152
*
153
* @param {CookieFilter} [cookieFilter] - The filter to apply to the cookies. Must be an instance of CookieFilter.
154
* @param {(BrowsingContextPartitionDescriptor|StorageKeyPartitionDescriptor)} [partition] - The partition to delete cookies from. Must be an instance of either BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor.
155
* @returns {PartitionKey} - The partition key of the deleted cookies, if available.
156
* @throws {Error} - If the provided parameters are not of the correct type.
157
*/
158
async deleteCookies(cookieFilter = undefined, partition = undefined) {
159
if (cookieFilter !== undefined && !(cookieFilter instanceof CookieFilter)) {
160
throw new Error(`Params must be an instance of CookieFilter. Received:'${cookieFilter}'`)
161
}
162
163
if (
164
partition !== undefined &&
165
!(partition instanceof BrowsingContextPartitionDescriptor || partition instanceof StorageKeyPartitionDescriptor)
166
) {
167
throw new Error(
168
`Params must be an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor. Received:'${partition}'`,
169
)
170
}
171
172
const command = {
173
method: 'storage.deleteCookies',
174
params: {
175
filter: cookieFilter ? Object.fromEntries(cookieFilter.asMap()) : undefined,
176
partition: partition ? Object.fromEntries(partition.asMap()) : undefined,
177
},
178
}
179
180
let response = await this.bidi.send(command)
181
182
if (Object.prototype.hasOwnProperty.call(response.result, 'partitionKey')) {
183
if (
184
Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'userContext') &&
185
Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'sourceOrigin')
186
) {
187
return new PartitionKey(response.result.partitionKey.userContext, response.result.partitionKey.sourceOrigin)
188
}
189
}
190
}
191
}
192
193
async function getStorageInstance(driver) {
194
let instance = new Storage(driver)
195
await instance.init()
196
return instance
197
}
198
199
module.exports = getStorageInstance
200
201