Path: blob/trunk/javascript/selenium-webdriver/test/cookie_test.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.1617'use strict'1819const assert = require('node:assert')20const { URL } = require('node:url')2122const { ignore, suite } = require('../lib/test')23const fileserver = require('../lib/test/fileserver')24const { Browser } = require('selenium-webdriver')2526suite(function (env) {27let driver2829before(async function () {30driver = await env.builder().build()31})3233after(function () {34return driver.quit()35})3637describe('Cookie Management;', function () {38beforeEach(async function () {39await driver.get(fileserver.Pages.ajaxyPage)40await driver.manage().deleteAllCookies()41return assertHasCookies()42})4344it('can add new cookies', async function () {45const cookie = createCookieSpec()4647await driver.manage().addCookie(cookie)48await driver49.manage()50.getCookie(cookie.name)51.then(function (actual) {52assert.strictEqual(actual.value, cookie.value)53})54})5556it('Get Cookie: throw error if name is null', async function () {57const cookie = createCookieSpec()58await driver.manage().addCookie(cookie)59await assert.rejects(async () => await driver.manage().getCookie(null), {60name: 'InvalidArgumentError',61message: `Cookie name cannot be empty`,62})63})6465it('Delete cookie: throw error if name is null', async function () {66const cookie = createCookieSpec()67await driver.manage().addCookie(cookie)68await assert.rejects(async () => await driver.manage().deleteCookie(null), {69name: 'InvalidArgumentError',70message: `Cookie name cannot be empty`,71})72})7374it('can get all cookies', async function () {75const cookie1 = createCookieSpec()76const cookie2 = createCookieSpec()7778await driver.manage().addCookie(cookie1)79await driver.manage().addCookie(cookie2)8081return assertHasCookies(cookie1, cookie2)82})8384ignore(env.browsers(Browser.INTERNET_EXPLORER)).it(85'only returns cookies visible to the current page',86async function () {87const cookie1 = createCookieSpec()8889await driver.manage().addCookie(cookie1)9091const pageUrl = fileserver.whereIs('page/1')92const cookie2 = createCookieSpec({93path: new URL(pageUrl).pathname,94})95await driver.get(pageUrl)96await driver.manage().addCookie(cookie2)97await assertHasCookies(cookie1, cookie2)9899await driver.get(fileserver.Pages.ajaxyPage)100await assertHasCookies(cookie1)101102await driver.get(pageUrl)103await assertHasCookies(cookie1, cookie2)104},105)106107it('can delete all cookies', async function () {108const cookie1 = createCookieSpec()109const cookie2 = createCookieSpec()110111await driver.executeScript(112'document.cookie = arguments[0] + "=" + arguments[1];' + 'document.cookie = arguments[2] + "=" + arguments[3];',113cookie1.name,114cookie1.value,115cookie2.name,116cookie2.value,117)118await assertHasCookies(cookie1, cookie2)119120await driver.manage().deleteAllCookies()121await assertHasCookies()122})123124it('can delete cookies by name', async function () {125const cookie1 = createCookieSpec()126const cookie2 = createCookieSpec()127128await driver.executeScript(129'document.cookie = arguments[0] + "=" + arguments[1];' + 'document.cookie = arguments[2] + "=" + arguments[3];',130cookie1.name,131cookie1.value,132cookie2.name,133cookie2.value,134)135await assertHasCookies(cookie1, cookie2)136137await driver.manage().deleteCookie(cookie1.name)138await assertHasCookies(cookie2)139})140141it('should only delete cookie with exact name', async function () {142const cookie1 = createCookieSpec()143const cookie2 = createCookieSpec()144const cookie3 = { name: cookie1.name + 'xx', value: cookie1.value }145146await driver.executeScript(147'document.cookie = arguments[0] + "=" + arguments[1];' +148'document.cookie = arguments[2] + "=" + arguments[3];' +149'document.cookie = arguments[4] + "=" + arguments[5];',150cookie1.name,151cookie1.value,152cookie2.name,153cookie2.value,154cookie3.name,155cookie3.value,156)157await assertHasCookies(cookie1, cookie2, cookie3)158159await driver.manage().deleteCookie(cookie1.name)160await assertHasCookies(cookie2, cookie3)161})162163it('can delete cookies set higher in the path', async function () {164const cookie = createCookieSpec()165const childUrl = fileserver.whereIs('child/childPage.html')166const grandchildUrl = fileserver.whereIs('child/grandchild/grandchildPage.html')167168await driver.get(childUrl)169await driver.manage().addCookie(cookie)170await assertHasCookies(cookie)171172await driver.get(grandchildUrl)173await assertHasCookies(cookie)174175await driver.manage().deleteCookie(cookie.name)176await assertHasCookies()177178await driver.get(childUrl)179await assertHasCookies()180})181182ignore(env.browsers(Browser.FIREFOX, Browser.INTERNET_EXPLORER)).it(183'should retain cookie expiry',184async function () {185let expirationDelay = 5 * 1000186let expiry = new Date(Date.now() + expirationDelay)187let cookie = createCookieSpec({ expiry })188189await driver.manage().addCookie(cookie)190await driver191.manage()192.getCookie(cookie.name)193.then(function (actual) {194assert.strictEqual(actual.value, cookie.value)195196// expiry times should be in seconds since January 1, 1970 UTC197assert.strictEqual(actual.expiry, Math.floor(expiry.getTime() / 1000))198})199200await driver.sleep(expirationDelay)201await assertHasCookies()202},203)204205ignore(env.browsers(Browser.FIREFOX, Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(206'can add same site cookie property to `Strict`',207async function () {208let cookie = createSameSiteCookieSpec('Strict')209let childUrl = fileserver.whereIs('child/childPage.html')210await driver.get(childUrl)211await driver.manage().addCookie(cookie)212const actual = await driver.manage().getCookie(cookie.name)213assert.strictEqual(actual.sameSite, 'Strict')214},215)216217ignore(env.browsers(Browser.FIREFOX, Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(218'can add same site cookie property to `Lax`',219async function () {220let cookie = createSameSiteCookieSpec('Lax')221let childUrl = fileserver.whereIs('child/childPage.html')222await driver.get(childUrl)223await driver.manage().addCookie(cookie)224const actualCookie = await driver.manage().getCookie(cookie.name)225assert.strictEqual(actualCookie.sameSite, 'Lax')226},227)228229ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(230'can add same site cookie property to `None` when cookie is Secure',231async function () {232let cookie = createSameSiteCookieSpec('None', {233secure: true,234})235let childUrl = fileserver.whereIs('child/childPage.html')236await driver.get(childUrl)237await driver.manage().addCookie(cookie)238await assert.doesNotReject(async () => await driver.manage().addCookie(cookie))239},240)241242ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(243'throws an error if same site is set to `None` and the cookie is not Secure',244async function () {245let cookie = createSameSiteCookieSpec('None')246let childUrl = fileserver.whereIs('child/childPage.html')247await driver.get(childUrl)248await assert.rejects(async () => await driver.manage().addCookie(cookie), {249name: 'InvalidArgumentError',250message: `Invalid cookie configuration: SameSite=None must be Secure`,251})252},253)254255ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI)).it(256'throws an error if same site cookie property is invalid',257async function () {258let cookie = createSameSiteCookieSpec('Foo')259let childUrl = fileserver.whereIs('child/childPage.html')260await driver.get(childUrl)261await assert.rejects(async () => await driver.manage().addCookie(cookie), {262name: 'InvalidArgumentError',263message: `Invalid sameSite cookie value 'Foo'. It should be one of "Lax", "Strict" or "None"`,264})265},266)267})268269function createCookieSpec(opt_options) {270let spec = {271name: getRandomString(),272value: getRandomString(),273}274if (opt_options) {275spec = Object.assign(spec, opt_options)276}277return spec278}279280function createSameSiteCookieSpec(sameSiteVal, extraProps) {281return {282name: getRandomString(),283value: getRandomString(),284sameSite: sameSiteVal,285...extraProps,286}287}288289function buildCookieMap(cookies) {290const map = {}291cookies.forEach(function (cookie) {292map[cookie.name] = cookie293})294return map295}296297function assertHasCookies(...expected) {298return driver299.manage()300.getCookies()301.then(function (cookies) {302assert.strictEqual(303cookies.length,304expected.length,305'Wrong # of cookies.' +306'\n Expected: ' +307JSON.stringify(expected) +308'\n Was : ' +309JSON.stringify(cookies),310)311312const map = buildCookieMap(cookies)313for (let i = 0; i < expected.length; ++i) {314assert.strictEqual(expected[i].value, map[expected[i].name].value)315}316})317}318319function getRandomString() {320const x = 1234567890321return Math.floor(Math.random() * x).toString(36)322}323})324325326