Path: blob/trunk/javascript/selenium-webdriver/bidi/storage.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617const { CookieFilter } = require('./cookieFilter')18const { BrowsingContextPartitionDescriptor, StorageKeyPartitionDescriptor } = require('./partitionDescriptor')19const { PartitionKey } = require('./partitionKey')20const { PartialCookie } = require('./partialCookie')21const { Cookie, BytesValue } = require('./networkTypes')2223/**24* Represents commands of Storage module.25* Described in https://w3c.github.io/webdriver-bidi/#module-storage.26* @class27*/28class Storage {29constructor(driver) {30this._driver = driver31}3233async init() {34if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {35throw Error('WebDriver instance must support BiDi protocol')36}3738this.bidi = await this._driver.getBidi()39}4041/**42* Retrieves cookies based on the provided filter and partition.43*44* @param {CookieFilter} [filter] - The filter to apply to the cookies.45* @param {(BrowsingContextPartitionDescriptor|StorageKeyPartitionDescriptor)} [partition] - The partition to retrieve cookies from.46* @returns {Promise<{ cookies: Cookie[], partitionKey?: PartitionKey }>} - A promise that resolves to an object containing the retrieved cookies and an optional partition key.47* @throws {Error} If the filter parameter is provided but is not an instance of CookieFilter.48* @throws {Error} If the partition parameter is provided but is not an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor.49*/50async getCookies(filter = undefined, partition = undefined) {51if (filter !== undefined && !(filter instanceof CookieFilter)) {52throw new Error(`Params must be an instance of CookieFilter. Received:'${filter}'`)53}5455if (56partition !== undefined &&57!(partition instanceof BrowsingContextPartitionDescriptor || partition instanceof StorageKeyPartitionDescriptor)58) {59throw new Error(60`Params must be an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor. Received:'${partition}'`,61)62}6364const command = {65method: 'storage.getCookies',66params: {67filter: filter ? Object.fromEntries(filter.asMap()) : undefined,68partition: partition ? Object.fromEntries(partition.asMap()) : undefined,69},70}7172let response = await this.bidi.send(command)7374let cookies = []75response.result.cookies.forEach((cookie) => {76cookies.push(77new Cookie(78cookie.name,79new BytesValue(cookie.value.type, cookie.value.value),80cookie.domain,81cookie.path,82cookie.size,83cookie.httpOnly,84cookie.secure,85cookie.sameSite,86cookie.expiry,87),88)89})9091if (Object.prototype.hasOwnProperty.call(response.result, 'partitionKey')) {92if (93Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'userContext') &&94Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'sourceOrigin')95) {96let partitionKey = new PartitionKey(97response.result.partitionKey.userContext,98response.result.partitionKey.sourceOrigin,99)100return { cookies, partitionKey }101}102103return { cookies }104}105}106107/**108* Sets a cookie using the provided cookie object and partition.109*110* @param {PartialCookie} cookie - The cookie object to set.111* @param {(BrowsingContextPartitionDescriptor|StorageKeyPartitionDescriptor)} [partition] - The partition to use for the cookie.112* @returns {PartitionKey} The partition key of the set cookie.113* @throws {Error} If the cookie parameter is not an instance of PartialCookie or if the partition parameter is not an instance of PartitionDescriptor.114*/115async setCookie(cookie, partition = undefined) {116if (!(cookie instanceof PartialCookie)) {117throw new Error(`Params must be an instance of PartialCookie. Received:'${cookie}'`)118}119120if (121partition !== undefined &&122!(partition instanceof BrowsingContextPartitionDescriptor || partition instanceof StorageKeyPartitionDescriptor)123) {124throw new Error(125`Params must be an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor. Received:'${partition}'`,126)127}128129const command = {130method: 'storage.setCookie',131params: {132cookie: cookie ? Object.fromEntries(cookie.asMap()) : undefined,133partition: partition ? Object.fromEntries(partition.asMap()) : undefined,134},135}136137let response = await this.bidi.send(command)138139if (Object.prototype.hasOwnProperty.call(response.result, 'partitionKey')) {140if (141Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'userContext') &&142Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'sourceOrigin')143) {144return new PartitionKey(response.result.partitionKey.userContext, response.result.partitionKey.sourceOrigin)145}146}147}148149/**150* Deletes cookies based on the provided filter and partition.151*152* @param {CookieFilter} [cookieFilter] - The filter to apply to the cookies. Must be an instance of CookieFilter.153* @param {(BrowsingContextPartitionDescriptor|StorageKeyPartitionDescriptor)} [partition] - The partition to delete cookies from. Must be an instance of either BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor.154* @returns {PartitionKey} - The partition key of the deleted cookies, if available.155* @throws {Error} - If the provided parameters are not of the correct type.156*/157async deleteCookies(cookieFilter = undefined, partition = undefined) {158if (cookieFilter !== undefined && !(cookieFilter instanceof CookieFilter)) {159throw new Error(`Params must be an instance of CookieFilter. Received:'${cookieFilter}'`)160}161162if (163partition !== undefined &&164!(partition instanceof BrowsingContextPartitionDescriptor || partition instanceof StorageKeyPartitionDescriptor)165) {166throw new Error(167`Params must be an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor. Received:'${partition}'`,168)169}170171const command = {172method: 'storage.deleteCookies',173params: {174filter: cookieFilter ? Object.fromEntries(cookieFilter.asMap()) : undefined,175partition: partition ? Object.fromEntries(partition.asMap()) : undefined,176},177}178179let response = await this.bidi.send(command)180181if (Object.prototype.hasOwnProperty.call(response.result, 'partitionKey')) {182if (183Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'userContext') &&184Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'sourceOrigin')185) {186return new PartitionKey(response.result.partitionKey.userContext, response.result.partitionKey.sourceOrigin)187}188}189}190}191192async function getStorageInstance(driver) {193let instance = new Storage(driver)194await instance.init()195return instance196}197198module.exports = getStorageInstance199200201