Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/storage_test.js
11822 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.1617'use strict'1819const assert = require('node:assert')20const { suite, Pages } = require('../../../lib/test')21const { Browser } = require('selenium-webdriver')2223const { Storage } = require('selenium-webdriver/bidi/generated/storage')2425suite(26function (env) {27let driver28let storage2930beforeEach(async function () {31driver = await env.builder().build()32storage = await Storage.create(driver)33// Navigate to a page so cookie operations have a valid origin34await driver.get(Pages.ajaxyPage)35await driver.manage().deleteAllCookies()36})3738afterEach(function () {39return driver.quit()40})4142describe('setCookie and getCookies', function () {43it('can set and get a cookie by name', async function () {44const url = await driver.getCurrentUrl()45const domain = new URL(url).hostname4647await storage.setCookie({48cookie: {49name: 'test-cookie',50value: { type: 'string', value: 'test-value' },51domain,52},53})5455const result = await storage.getCookies({56filter: { name: 'test-cookie' },57})5859assert.ok(result.cookies.length >= 1)60const found = result.cookies.find((c) => c.name === 'test-cookie')61assert.ok(found)62assert.strictEqual(found.value.value, 'test-value')63})6465it('can set a cookie with path and httpOnly', async function () {66const url = await driver.getCurrentUrl()67const domain = new URL(url).hostname6869await storage.setCookie({70cookie: {71name: 'secure-cookie',72value: { type: 'string', value: 'secure-val' },73domain,74path: '/',75httpOnly: true,76},77})7879const result = await storage.getCookies({80filter: { name: 'secure-cookie' },81})8283assert.ok(result.cookies.length >= 1)84const found = result.cookies.find((c) => c.name === 'secure-cookie')85assert.ok(found)86assert.strictEqual(found.httpOnly, true)87})8889it('getCookies returns partition key', async function () {90const url = await driver.getCurrentUrl()91const domain = new URL(url).hostname9293await storage.setCookie({94cookie: {95name: 'partition-test',96value: { type: 'string', value: 'pval' },97domain,98},99})100101const contextId = await driver.getWindowHandle()102const result = await storage.getCookies({103filter: { name: 'partition-test' },104partition: { type: 'context', context: contextId },105})106107assert.ok(result.partitionKey !== undefined)108assert.ok(result.cookies.length >= 1)109})110111it('can filter cookies by domain', async function () {112const url = await driver.getCurrentUrl()113const domain = new URL(url).hostname114115await storage.setCookie({116cookie: {117name: 'domain-cookie',118value: { type: 'string', value: 'domain-val' },119domain,120},121})122123const result = await storage.getCookies({124filter: { domain },125})126127assert.ok(result.cookies.length >= 1)128})129130it('getCookies returns empty when no cookies match filter', async function () {131const result = await storage.getCookies({132filter: { name: 'nonexistent-cookie-xyz-123' },133})134135assert.strictEqual(result.cookies.length, 0)136})137})138139describe('deleteCookies', function () {140it('can delete a specific cookie by name', async function () {141const url = await driver.getCurrentUrl()142const domain = new URL(url).hostname143144// Add two cookies145await storage.setCookie({146cookie: {147name: 'cookie-to-delete',148value: { type: 'string', value: 'delete-me' },149domain,150},151})152await storage.setCookie({153cookie: {154name: 'cookie-to-keep',155value: { type: 'string', value: 'keep-me' },156domain,157},158})159160// Delete one161const deleteResult = await storage.deleteCookies({162filter: { name: 'cookie-to-delete' },163})164165assert.ok(deleteResult.partitionKey !== undefined)166167// Verify it's gone168const remaining = await storage.getCookies({169filter: { name: 'cookie-to-delete' },170})171assert.strictEqual(remaining.cookies.length, 0)172173// Verify the other is still there174const kept = await storage.getCookies({175filter: { name: 'cookie-to-keep' },176})177assert.ok(kept.cookies.length >= 1)178})179180it('can delete all cookies for a domain', async function () {181const url = await driver.getCurrentUrl()182const domain = new URL(url).hostname183184await storage.setCookie({185cookie: {186name: 'c1',187value: { type: 'string', value: 'v1' },188domain,189},190})191await storage.setCookie({192cookie: {193name: 'c2',194value: { type: 'string', value: 'v2' },195domain,196},197})198199await storage.deleteCookies({ filter: { domain } })200201const result = await storage.getCookies({ filter: { domain } })202assert.strictEqual(result.cookies.length, 0)203})204205it('deleteCookies with context partition', async function () {206const url = await driver.getCurrentUrl()207const domain = new URL(url).hostname208const contextId = await driver.getWindowHandle()209210await storage.setCookie({211cookie: {212name: 'ctx-cookie',213value: { type: 'string', value: 'ctx-val' },214domain,215},216})217218const result = await storage.deleteCookies({219filter: { name: 'ctx-cookie' },220partition: { type: 'context', context: contextId },221})222223assert.ok(result.partitionKey !== undefined)224})225})226},227{ browsers: [Browser.CHROME, Browser.FIREFOX] },228)229230231